5 ms·
Clojure's immutable data structures (lists, vectors, maps and sets) are actually not copy on write, they're based on red-black trees such that when you "mutate"
by keymone 7y ago
Clojure's immutable data structures (lists, vectors, maps and sets) are actually not copy on write, they're based on red-black trees such that when you "mutate" the value, a new one is created that shares everything but the mutated part with the original.
Clojure is amazing and i can't recommend it enough. I wish more people looked past the unfamiliar syntax and understood why it exists and how it makes you achieve more with simpler code.
- vnorilo 7y agoTo nitpick a little, Clojure maps are hash array mapped tries and vectors are trees with a highish radix and some push/shift optimization. As you said, they are collections with value semantics which is pretty great.
- lukifer 7y agoI'm pretty sold on Clojure's value, I've read up on its structure/syntax, but generally struggle with learning the standard lib and trying to read/write actual code. Any resources you can recommend for gradually ramping up, and/or "lightbulb moments" for a dev steeped in C-like languages?
- vnorilo 7y agoHaving written C++ for 25 years, I found it very enjoyable to write small toy client-side web apps with clojurescript, figwheel and reagent. Reagent's Hiccup-like syntax for html is lightweight and gets you interactive stuff on screen quickly with hot reloading. Iterating quickly helps me learn, and getting dynamic graphics on screen provides that dopamine drip feed I need to stay motivated.
- ex3xu 7y agoOutside of standard recommended resources like the ones offered here [0], I found that doing the problems on 4clojure.com and then comparing with the answers of top users was helpful for experiencing some of those kind of lightbulb moments in Clojure. [0]: https://github.com/razum2um/awesome-clojure#guides https://github.com/razum2um/awesome-clojure#guides
- heartofgold 7y agoI second this. Struggle with each problem and then view other answers to see how elegantly they solved the same problems. This worked wonders for me as well.
- JamesLeonis 7y agoOne extremely useful resource is the Clojure Cheatsheet [1]. It organizes the standard library by all kinds of uses (data types, use cases, etc). It then links to the documentation of that function in Clojure.org, which often have examples of the function's use. Likewise, each function page (if you scroll down) has a "See Also" which lists similar functions, which helps discover other functions in the standard lib. Beyond the Standard Library, I find the Luminus Project [2] an excellent source of real-world code and documents for problems around web development. Further along these lines is the Clojure Cookbook [3] as well. [1]: https://clojure.org/api/cheatsheet https://clojure.org/api/cheatsheet [2]: http://www.luminusweb.net/docs http://www.luminusweb.net/docs [3]: https://github.com/clojure-cookbook/clojure-cookbook https://github.com/clojure-cookbook/clojure-cookbook > "lightbulb moments" for a dev steeped in C-like languages? I'm from C++, so I feel what you mean when moving from Imperative to Functional. I have two insights that helped me: * Despite the huge type systems, languages like C have "Raw Memory" as the universal data structure. Think about pointer magic or the `union` type. Clojure does something similar; Despite the different data structures, the universal structure is `seq` which allows iteration over a data structure in the same way pointers iterate over any memory. * Clojure's immutable data structures eliminate the Pass-By-Reference and Pass-By-Value dichotomy. As such, you aren't so much (with functions) altering memory as transforming data. It feels much more like math (especially Linear Algebra) where you are transforming data. Seeing my code as a series of transforms greatly helps my understanding of a piece of Clojure code.
- rafd 7y agoIf you're new to functional programming, I recently gave a talk on applying FP concepts at ClojureNorth: https://youtu.be/vK1DazRK_a0 https://youtu.be/vK1DazRK_a0 The main example I work through is in JavaScript so that Clojure's syntax doesn't get in the way (I love s-expressions, but they're not the point of the talk).
- pdsouza 7y agoCame across this talk a few weeks ago and I have to say this is one of the best pragmatic talks comparing non-FP vs. FP paradigms I've ever seen. I really like your example problem, how you explore state and mutation in the different paradigms, and how you refactor step-by-step towards a functional design. Thank you!
- rafd 7y agoI'm very glad you liked it! Thank you for your kind words.
- jonahbenton 7y agoAs someone who came from a similar background, the ice broke when I went from trying to write code the C way- in whole files, running the nascent program from the command line, struggling with compiler errors, adding printlns for debugging- to working at the repl. The repl is the whole thing.
- finalfantasia 7y agoIt seems to me what you’re struggling with is functional programming (the paradigm) instead of Clojure (the language or grammar) itself. This is pretty common for people who are used to the C-like languages. I strongly recommend reading “Clojure for the Brave and True” [1] (especially, Part 2) to anyone who is getting started on either functional programming and/or Clojure. [1] https://www.braveclojure.com/clojure-for-the-brave-and-true/ https://www.braveclojure.com/clojure-for-the-brave-and-true/
- microcolonel 7y agohttps://clojuredocs.org/ https://clojuredocs.org/ has been helpful, especially the examples, and the direct links to the code. Like working with libraries in any other language, if you have trouble understanding how something is working, reading the implementation is often your best recourse. Using let can also be a decent crutch, if you're just not used to doing things with functional composition and (occasional) macros.
- AlexCoventry 7y ago> Any resources you can recommend for gradually ramping up http://www.4clojure.com/problems http://www.4clojure.com/problems
- iLemming 7y agoOne of the best practical Clojure books in my opinion is this one: https://www.manning.com/books/clojure-the-essential-reference https://www.manning.com/books/clojure-the-essential-referenc.... IMO it's a second "must read" book after "Joy of Clojure 2nd ed."
- crispinb 7y ago> I wish more people looked past the unfamiliar syntax I truly doubt many professional devs are put off by the syntax. This is the least of the hurdles facing any (post-beginner) programmer learning a new language. It wouldn't even make it to the bottom of my list of criteria.
- keymone 7y agoYou’d be surprised. But also unfamiliarity in general is a hurdle. Clojure is unfamiliar in a multitude of ways and it takes some effort to realize how all these things Clojure is different in actually have great synergies.
- crispinb 7y agoYes no doubt unfamiliarity (beyond the syntax trivia) is of course a barrier. How much of one it is depends on many things. The closer someone is to the 'curious' end of the pragmatic <-> curious learning spectrum, the less of a problem it is (indeed it might be an attractant).
- iLemming 7y ago> I wish more people ... I think Clojure will be fine. It is slowly and steadily growing. I honestly think it has reached well beyond the point where it can fade away. People always bring Common Lisp as an example and mention so-called "Lisp curse", etc. but if you follow and analyze the history of PLs - you'd probably realize - Clojure is a natural next evolutionary step of Lisp. The industry desperately needs languages like Clojure and Haskell. Haskell being somewhat more "academic" and a bit "impractical" (I should emphasize the double quotes: Haskell in the right hands can be extremely, dangerously practical) never going to reach the status of a mainstream language, whereas Clojure just might. Realistically, it probably won't ever become top ten in TIOBE (or whatever similar) index, and that is totally okay. But it is pretty good already, and it is arguably better than Python and Ruby, Java, Javascript, Go, C#, etc. Clojure is Haskell's "worse is better". And look: it already has more conferences and meetups (more than Elixir, Elm, OCaml, Haskell, Groovy, maybe even Scala) around the world (almost every European country now has its own seasonal Clojure conf.), it has over a dozen of books (and more coming), more podcasts. Clojure is a niche, but it is slowly become more popular.