12 ms·
Why I like Clojure
- marmaduke 7y ago> homoiconicity, meaning code can be represented as a data structure inside the same language > generating strings and passing them into eval like for example in Python Python has become a pinata for language enthusiasts? Python lets you pin a decorator on a function, get the AST (a data structure AFAIK), manipulate it in a sane fashion in Python, compile it, return the result. No string munching eval require I'm all for Lisp but this just isn't convincing
- vaylian 7y ago> get the AST (a data structure AFAIK) how?
- masklinn 7y agoTBF I don't know how common it is as Python's AST API is pretty gnarly (not to mention really hard to work with / debug as it will usually segfault if you look at it funny). And IIRC the error messages coming from there are significantly worse than those coming from source. Third-party packages do make that somewhat better, but still not great. IIRC Jinja compiles templates to Python source code then compiles that to bytecode using the relevant builtins because it's simpler & more reliable than doing straight AST manipulations.
- pritambaral 7y ago> get the AST (a data structure AFAIK), manipulate it in a sane fashion in Python, compile it, return the result. No string munching eval require I use both Lisps and Python, and especially after learning to use Lisps I've wanted to do this in Python, so I looked for the ability to do what you describe here. While it is technically possible to do this (like most things) in Python, it is quite the effort and nowhere near ergonomic. Among all the cases where I could really use this, I found it worth the effort to instead: 1. Manipulate & generate code statically (from ASTs), as a build-time / dev-time step, using redbaron etc.; or 2. Just use a Lisp (specifically, Hy). Both methods involved writing quite a lot of code. In the former: to load, parse, match, and transform ASTs; in the latter: to port existing code (the code I originally wanted to manipulate) from Python to Hy. ---- The reason for all this is plainly the fact that Lisps are designed for this, while Python is not. Before learning Lisp, I used to wonder what could be so different in the language itself that could possibly make it that much easier. Now I use a Lisp-in-Python in production precisely because of this.
- mark_l_watson 7y agoThank you, interesting comments, especially switching over to the Hy language for production. I got very excited about Hy, then an update took away a built in “let” and that turned me off a bit. Are there any good directions for patching in the old code supporting “let”?
- marmaduke 7y agoThis is true, but I wanted to correct factual errors in the article. If you or your team are Lisp users then Hy or Clojure would be good choices, but if you are just trying to create a transformation for existing Python codebases, the decorator approach is a good shoehorn. On the other hand, you can also interpret the difficulty of AST transforms as a signal from the language designers (had van Rossum really never heard of Lisp?) that they hurt readability and their use should require a little ceremony.
- pritambaral 7y agoI'm sorry, but I don't agree at all. > if you are just trying to create a transformation for existing Python codebases, the decorator approach is a good shoehorn. For transformations that need to modify the code, decorators are insufficient. Decorators have their value, and I have built good interfaces using decorators, which would have been rather difficult without them, but all of that was because decorators help _wrap_, not _transform_ code. When I had a need to transform code, I looked for the right tool, and the takeaways of my searches are mentioned in my original comment. > you can also interpret the difficulty of AST transforms as a signal from the language designers (had van Rossum really never heard of Lisp?) that they hurt readability and their use should require a little ceremony. Nobody's claiming Guido had never heard of Lisp, or any non-lispy-language designer for that matter. Language designers make languages for varied reasons. Guido's reasons actually happen to be documented, and the only reason Python isn't homoiconic is because Guido didn't have a need for it, not because he explicitly wanted AST transforms to "require a little ceremony". In fact, Python AST transforms do not merely "require a little ceremony". They are actually quite cumbersome. The standard library actually provides no help at all in transforms, making it necessary for libraries like Rope, RedBaron, and Astor to be written.
- ertucetin 7y agoPython is not as flexible as Lisp in a manner of AST manipulation
- smitty1e 7y agoPython's approach seems to be: expose functionality like the AST for the 31337 ninjas that really want to go there, while maintaining intense focus on producing a KISS tool for the other 95% of its audience.
- jacobobryant 7y agoA lot of (most?) of the benefit of macros comes not from writing them yourself, but from using macros that others have written. Even if only 5% of the people are writing macros, that 5% likely includes many library authors. Making it harder for them impacts their downstream users too, even if those people aren't actually writing macros. IMO the macros in Clojure make it easier for library authors to produce KISS, high-level interfaces for the rest of us.
- TeMPOraL 7y agoThis ultimately impacts code quality negatively. Macros aren't a tool "for the 31337 ninjas", not more than class inheritance or generic types, or hell, even procedures. Macros solve a particular class of problems - they eliminate bloat and boilerplate, allowing you to express your code closer to the way you think about it. Much like classes can be used as data transfer objects or to build out a large inheritance tree, macros can be used tactically - to DRY up things you otherwise couldn't, or introduce some readability improvement - or strategically, to lift the code up another layer of abstraction, or create DSLs that let you write your code much closer to the problem domain. Because Lisp supports them in an ergonomic way, macros are not seen as something special, but just another tool in the toolbox.
- lenkite 7y agoYou might enjoy this talk- Live Coding a Compiler using Clojure at https://youtu.be/_lMltUNB6Yw?list=PLhYmIiHOMWoEgJEvgkmUe8D0agxy_T2vR https://youtu.be/_lMltUNB6Yw?list=PLhYmIiHOMWoEgJEvgkmUe8D0a...
- kopos 7y agoA python decorator is just a syntactic sugar for : f = decorator(f) Afaik, the decorator deals with the internal function as a black box. But with lisp being a lisp, the macro writer can choose the level of blackboxiness they want to deal with. Note: I love both Python and Clojure and use them extensively.
- dangerbird2 7y agoAs others have hinted at, higher-order functions like decorators can inspect and manipulate the internals of a "black box" function object with the ast module[1]. This would be comparable to a lisp function taking a quoted form, expanding and evaling it at runtime as an ad-hoc macro funcitonality as is common in more bare-bones lisp implementations. Aside from the ease of manipulating quoted forms (i.e. asts) in Lisp and Clojure, a big part of the power of defmacro and kin is that Lisps distinguish (depending on implementation) between read-time, macroexpand-time, compile time, and eval time, giving the macro author control over when and where the macro is executed as well as how. As an example, this makes things like lazy evaluation (without requiring users to explicitly "thunk" values into functions) trivial to implement in lisp [1] https://docs.python.org/3/library/ast.html https://docs.python.org/3/library/ast.html
- kopos 7y agoThank you, that was very informative. I do remember that Python had the ast module for a rules engine implementation, but I felt it was too cumbersome.. Thank you.
- duckerude 7y agoWhile you can manipulate Python's AST, you can't do it in a way that looks like adding new syntax. In Clojure you can write a list comprehension as `(for [x (range 10)] x)`. In Python you can write it as `[x for x in range(10)]`. Imagine these didn't exist, and you wanted to implement them yourself. In Clojure this is possible. Its list comprehensions are implemented as a Clojure macro: https://github.com/clojure/clojure/blob/b98ba84/src/clj/clojure/core.clj#L4590 https://github.com/clojure/clojure/blob/b98ba84/src/clj/cloj... In Python there are several problems. The first is that you can't easily pull the decorator trick on arbitrary expressions. Not even with a lambda function, at least without doing a lot of work - inspect.getsource doesn't handle those properly. But let's lower the bar, and replicate `l = [x for x in range(10)]` instead. You can do that with a decorator and an ordinary function. Then the problem becomes that you can only use existing syntax in the function body. Syntax that already means something else. If you put an ordinary list comprehension in the function body then you get a syntax error when the file is parsed, long before the decorator has a chance to do anything. So you have to make something up yourself. You might end up with something like this: @list_comp def l(): x: range(10) x Or, of course, this: l = list(map(lambda x: x, range(10))) Neither of these are as convenient or readable as real list comprehensions. But if you want those, you're at the whims of the language. You can't just add them yourself. I don't know how big of a deal this actually is. I've never used Clojure, and I've barely used other Lisps. Expressing what I want in Python is easy enough in practice (but maybe I just don't know any better?). But there's certainly something significant there that Python can't do.
- zcid 7y agoI heard somebody make a similar argument one time in regards to Common Lisp. He said that with Java you would have to wait several years sometimes to get a feature (assuming the language developers actually cared about the feature), but with Lisp, you could just implement the feature yourself. I've always felt this is one of the strongest arguments for using a lisp-style language. Edit: this is possible too in languages like Elixer and Ruby using macros, but eventually the readability suffers and debugging complex macros in those languages can be a nightmare.
- deleted 7y ago
- ken 7y agoPython has become one of the most popular languages. I think they're just using it as an example of "here's a language you probably know which doesn't support this" -- or, as you note, kind of supports it now but makes it a real pain because it's not native to the syntax.
- amelius 7y agoPython's biggest problem wrt functional programming: multiline inline lambdas are not possible.
- didibus 7y agoIt's more that code and data have the same representation. For example, where is the Json of Python? Where is the Python object notation which is both how you represent Python Objects in code as well as when serialized to data over a wire? [1 2 3] is both the serialized form of a vector as well as valid code to create a vector of itself. Now in JS, only object data is homoiconic. In Lisps, even code expressions are homoiconic, thus (+ 1 2) is the serialized form of the list of function + and args 1 and 2, as well as valid code to add 1 and 2 together. You do not need macros for homoiconicity. Macros is not what makes a language homoiconic. But once your language is homoiconic, it is very easy to add macros to it, and macros become much simpler to write. Which is why Lisps have macros as well.
- iLemming 7y ago> I'm all for Lisp but this just isn't convincing Nothing will convince you until you give it a heartfelt try. The joy from using Clojure is a real thing. Unfortunately, there are not too many languages out there you can say the same thing about (including Python). Clojure is not absolutely frustration-free PL, but it maintains a good balance between "fun" and "profit".
- mruts 7y agoOne (stupid?) thing that annoys me about closure is that native clojure libraries use snake case while java libraries use camel case. You could obviously write a new defn macro to take of all this and make it consistent, but it might have just been a better decision to just make everything camel case, in in Lisp-like fashion. Also I find Clojure a little dogmatic in regards to mutation, more so than even Scheme. I find Racket to be better designed and more elegant, though currently the performance isn’t as good.
- keymone 7y agoClojure being dogmatic is the reason I love it.
- dkersten 7y agoAgreed. While I don’t always agree with its dogma, without it I don’t think I’d still be using Clojure, yet here I am ten years after first learning it. Clojure is great because it takes a strong stance on how certain things should be done.
- keymone 7y agoyep. also, i think learning how clojure's dogmas synergize with each other is the determining factor in whether one is a clojurist for life or not.
- whalesalad 7y agoThe macro solution is only solving for the aesthetics of the code and is opening the door to infinite edge cases where it wouldn’t work or could have unintended side effects. I don’t disagree with you that it’s ugly and sometimes tedious to use Java in Clojure... but that is one of the foundational strengths of the language. It’s also something that you’re hopefully not doing a lot of... so I think the trade off of seeing ugly Java syntax sprinkled in your project is worthwhile. You’re standing on the shoulders of giants with zero to no penalty as far as interop. I might reframe your mentality to see this as a huge benefit and not something stupid!
- 7y ago
- BlueTemplar 7y ago> While OO seems to be out, and FP the new hotness Funny that. While I prefer FP, and did since I've learned Python, my teachers are really hot about OO, even pushing us to use it in Python projects. (Looking at projects from previous years, they mostly seemed to use Java. Then also because of browser integration and obviously because of the OO <-> UML <-> Project links ?)
- nafeydev 7y agoIs there any reason to believe FP is the "new hotness". Each year in the Stack overflow language trends report the FP languages are barely making an impact.
- vimslayer 7y agoIt's the new hotness if you measure hotness by the amount of Medium posts written about it.
- flixic 7y agoFP languages are not that big. FP architecture and practices are very popular. React, Immutability and more have become mainstream. A recent example is SwiftUI. While Swift is not a FP language, SwiftUI (FP UI framework) and Combine (a FP toolkit) is being pushed by Apple as the future UI architecture for all its OSes.
- iLemming 7y agoI don't know if you have noticed, but FP is not "new" anymore. Almost every programming language today either has some FP-flavored features or keeps adding them. And there are plenty of good reasons why many [smart] programmers think that OOP has failed. Complex static type systems on the other hand - are kinda "new kid on the block". And if we are not careful that may become a "new OOP" - doctrine so complex and convoluted that could become an impediment in building software. I know, static type systems are cool and all but it's easy to get from something elegant and simple like Elm into say Haskell, Idris, Agda etc. and into the weeds of homotopy type theory.
- nautilus12 7y agoNeed someone to explain why I should use clojure over scala because typelevel seems pretty well developed
- justinhj 7y agoSpeaking as a professional Scala developer I can’t explain why you’d use one over the other. They are very different yet I know people that successfully develop and ship software using one or the other. I think it comes down to personal preference
- serpix 7y agoClojure: live repl into running code Scala: Clojure: Explore the problem you are facing with quick prototyping in the repl and see the blind spots, the unknowns, the shape of data received from flaky integrations. Mold and shape your functions as the problem becomes clearer. Scala: Best it can do is write unit tests and the debugger to look at what exactly is happening, devise a bunch of classes to handle them, rinse repeat and of course wait hours for recompiles. Refactor when new unknowns surface and curse at the fucking compiler not knowing what type it should be inferring. Clojure programmers are done before Scala has a test environment up.
- joneholland 7y agoScala also has a live repl.
- serpix 7y agoPlease provide link as I need one. Ammonite is not it though.
- bobbyi_settv 7y agoI use jupyter notebook for Scala. I like having the same repl software for multiple languages. The kernel I use (almond) is based on Ammonite, but I believe there are others out there.
- dominotw 7y ago> While the primary platform is the JVM, superbly uncool but stable and relatively performant My impression was that java has the reputation of being "uncool" but JVM is highly regarded as modern marvel. I am wrong about this ?
- Insanity 7y agoIt just depends who you ask of course, but that is the idea you get. The JVM is pretty cool tech with many languages running on it that are considered cooler than Java.
- nafeydev 7y agoMy impression is that this perception is because of the slow garbage collection on JVM which eats up lots of cycles. Also all JVM languages tend to consume a lot of memory.
- eternalban 7y agoIT Java is uncool, but then IT is uncool even if written in Clojure (though the psychological aspect of using a "cool" language will likely dampen the existential pain of writing yet another book keeping CRUD app since now you are using s-expressions!) In my experience, people who say silly things about Java -- the technology, which certainly includes the Java Language Semantics, and JVM -- actually do not know what you can do with Java (the language). So yes, you Lisp nerds have you homoiconic 'mash' of syntax and thus AST manipulation, and we have first class Class Loaders and Byte Code Engineering and the sky is the limit there. Magic. But the sad reality of programmers' lives is the disconnect between the required cognitive capabilities of workers and the actual (domain) task at hand. tldr;/ its your job that is uncool not the tech.
- 0x445442 7y ago> But the sad reality of programmers' lives is the disconnect between the required cognitive capabilities of workers and the actual (domain) task at hand. Yeah, I just don't understand the less lines of code argument. This is not the limiting factor in software development, it's just not. The limiting factor is having enough information for the immediate Use Case and enough business domain knowledge to come up with a solution that delivers value to the business. The time spent on delivering that value dwarfs the time it takes to write some extra code. Let's look at Uncle Bob's example in the linked article... (println (take 25 (map #(* % %) (range)))) While fancy, that code is very Perlish and I think we all know the common criticisms of Perl. Also, fancy doesn't pay. What I've found to have a much larger negative impact on a project than the language is overly complicated architectures. To the extent Clojure can help with that I'm all for it. But from what I've seen, Java 8 with some clear conventions and constraining source code to the implementation of the Function interface can get you a lot of the same benefits.
- itwy 7y agoClojure/Lisp is super nice until you have a bunch of transformations to make then you've to keep everything in mind without having a named reference in front of you (unless you omit the threading operators and abuse the let statement). It becomes exhausting after a while and makes you question if you are working for the machine or the machine is working for you. Yes, it's more succinct and elegant but is it worth it?
- vemv 7y agoYou are absolutely free to decompose computations into more `defn`s, `let` statements, or threading operators. Or mix and match those.
- itwy 7y agoYou are 100% right but I am not writing all the code. My job is mostly maintaining other developers' code. And turns out, languages can have great effect on making not-so-good developer write reasonably good code (Go, for example).
- vemv 7y agoLikewise my Clojure jobs tend to involve reviewing multiple devs' code. It's certainly possible to converge towards some shared notion of what constitutes maintainable code. We additionally have an internal styleguide plus some formatter-related tooling.
- kccqzy 7y agoOne thing I've come to realize is that it is not enough to write code that's merely compilable. That's a low bar. A team needs a consistent style and that has to be enforced automatically. Automatic formatting is a good first step but that's also not enough. Say you don't want too many nested s-expressions. Then write a tool to reject code that has too many of them. Run the tool together with the compiler. Prevent merging if the tool complains unless a special exception is noted. Tooling is important.
- mmargerum 7y ago
- rrosen326 7y agoClojure Help: This is super apropos since I am JUST learning Clojure via the Clojure Koans. Any tips from clojurists on getting the repl (and readline) working fluently? I'm using Leiningen but it's not responding as I'd expect. I would think this would all set up more effortlessly. Tips on getting a new Clojure setup working would be appreciated.
- mping 7y agoI recommend emacs, try braveclojure.com - it's a pain to set up properly but well worth it.
- iLemming 7y agoAs an avid Emacsen, I would usually recommend Clojure-mode and CIDER only if you are already familiar with Emacs. Learning both - Clojure and Emacs can be really frustrating and may potentially become a "deal-breaking" impediment. Almost every [popular] editor/IDE today supports Clojure (not many mainstream langs can proudly say so)
- orestis 7y agoJoin the beginners channel in Clojurians slack, there are many patient people there volunteering to help with things like this. Try out Calva for VS code, read the official REPL guide here https://clojure.org/guides/repl/introduction https://clojure.org/guides/repl/introduction
- uxcolumbo 7y agoIf you're willing to pay a monthly fee, you might like this REPL driven development course: https://purelyfunctional.tv/courses/repl-driven-development-in-clojure/ https://purelyfunctional.tv/courses/repl-driven-development-... EDIT: and you might also want to check out https://clojureverse.org/ https://clojureverse.org/
- kccqzy 7y agoI use Leiningen as well and I have no issues with it. REPL startup is a bit slow, as is expected from most JVM programs but a bit more so for Leiningen.
- ngcc_hk 7y agoIt is a good language. Python is easier for quick code.
- simongray 7y agoI'll take simple over easy ;-)
- iLemming 7y agoI disagree. It's the matter of familiarity. Clojure is extremely productive and the best PL I have tried (out of many) to quickly build prototypes. Whenever I have a requirement to solve a problem in a different language (e.g. interview challenge, etc.) I would still do it first in Clojure and then convert it to whatever language is required. Yes, I have to do the same work twice, but even then - I am still faster doing it that way.
- dev_dull 7y ago> Clojure is explicitly designed as a hosted language, which I think was a very good move. I’m not personally a fan of a VM such as JVM. Sure you get some easy benefits, but now you have two things that need tuning. Two things that need periodic updating. Personally the total encapsulation of first-class static linking (such as Golang) is my preferred future. It must be first class and baked into the language in its design.
- iLemming 7y agoYou're missing the point. Because Clojure is hosted you basically can share code between JVM and Javascript. It is extremely nice when you actually can do that. Even in NodeJS, essentially running on different implementations of the same platform - that is hard. As it turns out - not so hard in Clojure. Now, imagine also having access to thousand of libraries written for JVM and Nodejs and Browsers? There are attempts to port Clojure to other ecosystems, making it possible to use them there. What if someday you can write Clojure code that can be shared between say Rust runtime, NodeJS and a browser? Wouldn't be that cool?
- peatmoss 7y agoI love the language. I’ve never found a language where I found the learning curve so shallow for being able to do so much. That said, I do find myself pining for the ability to easily compile a static executable (maybe with a small embedded runtime). Some time ago there was an attempt to port Clojure to Gambit Scheme, but that appears to have fizzled. More recently, I’ve seen Ferret, which compiles a subset of Clojure down to C++. Also Joker, which is a Clojure-ish interpreter in Go. Then there’s GraalVM, which I don’t trust, because it’s Oracle. I feel like the relative lack of traction for non-Java / non-Javascript implementations is part of the brilliance and sadness of Clojure. Namely, the JVM is a rocket ship and a boat anchor. Racket (modulo the uncertainty cast by the Racket2 discussions) and Gerbil Scheme both have a pretty good “distribute an executable” story while trying to introduce some creature comforts beyond plain Scheme. I suppose my ideal, would be something a little more like Clojure in terms of language, and a little more like Scheme in terms of deployment options.
- pmoriarty 7y agoWhy not Chicken Scheme? It compiles down to C.
- peatmoss 7y agoOh, definitely Chicken as well. The library story with Chicken is also pretty decent.
- walkingolof 7y agoGraalVM CE is licensed under GPLv2 what’s not to trust ?
- ypcx 7y agoCheck out Carp: https://github.com/carp-lang/Carp https://github.com/carp-lang/Carp
- FPGAhacker 7y agomost of the “port clojure to X” that I’ve seen are just top level syntax and don’t port the functional data structures over. Without those, I don’t see the point. Beyond that, the land of libraries has sneaky little java dependencies scattered throughout. Clojure without the libraries isn’t very compelling. As someone else mentioned, graal does a good job of making an “executable”
- vnorilo 7y agoMy favorite thing about Clojure: You compose libraries, instead of totally buy into framework A or B. Some people prefer the opposite. However, Composition works in Clojure because everyone is using the staples: functions, maps and seqs. Pretty much everything in the language can work as one (or all!) of them. While I also like static types, this is my argument for a well designed dynanic language like Clojure or its Lisp relatives: stuff just snaps together like magic lego bricks.
- Kalium 7y agoComposing libraries instead of buying into a framework is a wonderful thing. It allows a developer to use just the parts they want, integrate them with just what the developer needs, and get on with whatever they're doing. It's a beautiful idea, and it can enable amazing levels of productivity. With that said, might it also be possible that there could be some drawbacks? Having worked in and with Clojure, I found the ecosystem incredibly immature. It's somewhere beyond arrogant - reckless comes to mind - for most engineers to do all their own plumbing for a web service. The odds that they'll manage to design someone easily long-term maintainable are slim. The chances of authorization, authentication, potential SQL injection, and a thousand other security things being handled well is effectively none - the Clojure ecosystem often seems wildly ignorant of such things. The core philosophy of just composing the libraries you need with Ring means that anything you don't actively think of will be likely entirely neglected. This is a dangerous way to ship code that will be facing the real internet. Clojure requires you to fit absolutely every aspect of everything you're doing into your head and be handled by you, because nothing is going to do it for you. The lovely magical LEGO feeling is, sadly, a lie. This stands in stark contrast to the level of consideration around maintenance and security that has gone into a modern, mature framework. Or a modern language where SAST is an option. Doing low-level stuff feels absolutely amazing! The sense of control, of having your hands in the guts and the ability to do what you need without wasting time on bullshit magic, is heady and glorious. It's just perhaps worth considering that that magic can sometimes be incredibly valuable, well worth the tradeoff against the feeling of freedom.
- vnorilo 7y ago
- Kalium 7y agoI love the idea of Clojure. I find the ecosystem immature and full of ideas about "libraries, not frameworks" that leave developers incredibly vulnerable to their own ignorance and second-order ignorance. The number of developers I know - or even know of - that can be trusted to develop a useful and secure web application from the ground up with this kind of tooling approaches zero. Clojure is a cool language. I can only hope the ecosystem soon matures enough to make it one that's suitable for serious use.
- dominotw 7y agothere is luminus that assembles all the parts for you.
- Kalium 7y agoWhich would be wonderful! Except for the responses of visceral disgust I get from Clojure developers when I suggest using a framework. The culture seems hostile to actually using such things.
- dmix 7y agoWhich libraries are people expected to plug together the security parts? I'm not pushing for clojure but a lot of other languages and frameworks are taking this approach too including Node and Go. Wouldn't the security stuff be a part of the individual packages? Ie authentication package handles sessions and tokens, an http server/middleware with built in csp and ssl, orm, the data stuff handling serialization and data parsing? Technically rails is composed of a bunch of smaller gems with a similar responsibility tree although without the centralized CVE distribution and pressure that creates. When I used Clojure it was always mini frameworks which created a base set of important packages with a pluggable middleware system on top and flexible data back ends. The individual packages need to have domain experts for their particular attack surfaces and exposures. If anything its about a lack of eyes or community size on the various wholes, rather than expecting the end user to do the individual hardening or plumbing.
- Kalium 7y ago
- amelius 7y agoI tried to learn (get to know) Clojure by writing a library for cacheing (memoizing) function calls. I thought it would be a simple exercise, but it turned out to be not so easy at all, with lots of special cases.
- dmix 7y agoThat seems like an unusual first project. Any reason you chose it? Were you more interested in the language semantics?
- amelius 7y agoIt was because (1) a project I was thinking of would lean heavily on memoized calls, and (2) it would allow me to learn about less basic features like introspection and (3) to quickly see how cleanly the language was designed to not be disappointed later on.
- yayitswei 7y agoYou could check out Clojure's built-in memoize (https://clojuredocs.org/clojure.core/memoize https://clojuredocs.org/clojure.core/memoize)
- harperlee 7y agoCan you give more details? Shouldn’t that be a matter of a macro redifining defn (o create a defn variant a la schema’s defn-spec, or tufte’s defnp ) to capture the call?
- jkxyz 7y agoClojure's implementation of `memoize` is here: https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#L6343 https://github.com/clojure/clojure/blob/master/src/clj/cloju... What were the special cases for your use case?
- swyx 7y agoas i was talking to a friend about clojure i came up with an analogy i cant shake: clojure is what happens when you just make your programmer directly write an AST. a bit reductionist, but anyone care to debate this?
- _hardwaregeek 7y agoIsn't that the point of lisp and homoiconicity in general? That you're writing an AST that you can also treat as a data structure?
- swyx 7y agook so... why is writing an AST more ergonomic than having some syntax?
- _hardwaregeek 7y agoIt’s really not. That’s part of the problem with lisp. But you can treat your code as a data structure which offers a lot of benefits. For instance, it’s basically impossible to offer control flow as a library in normal languages. That is, if you wanted to implement an unless statement like in Ruby, you’d have to use thunks or some other form of lazy evaluation. Macros allow you to just transform the unless statement into an if statement quite easily by just operating on the AST. Of course, you can do this without homoiconicity but it’s a lot less ergonomic. Also part of the reason Babel exists is to take the place of a macro system. Instead of transpiling array spread into a function polyfill, macros would just output the pollyfill into the AST. What’s nice about this is that since it’s userland, people can test drive new features. Rust did that for async/await. And the pattern matching proposal in JS is based off a SweetJS macro. (Obviously not all language features can be implemented as macros but you get my point, right)
- swyx 7y agoi think so. thanks for the explainer!
- 7y ago
- synthc 7y agoWhat I like most about Clojure is the interactivity: I can quickly test and iterate on ideas in my editor connected to a REPL. To me, this is the main advantage of Clojure. Clojure's focus on the REPL, immutability and simple datastructures make it possible to inspect data and redefine functions in running systems. In other languages I spend a lot of time making small edits, setting breakpoints, starting the debugger, waiting for the program to start, etc, where in Clojure I can just redefine and inspect stuff immediately, allowing very short feedback loops when programming. A lot of Clojure tooling emphasizes short feedback loops: component with it's reload function to restart systems, figwheel/shadowclj with live reload of browser apps, which is a huge productivity boost. Once I got used to the quick feedback loops that Clojure provides, all the other languages feel bulky and slow to develop in.