REPL based development gives you instant feedback. lets you write a function in your editor and then run it, change it, run it again. If your idea of REPL is python or ruby, then you are not getting the entire picture. https://stackoverflow.com/questions/5671214/is-lisp-the-only...
Immutable by default. This helps with debugging and solves thread safety. Feel free to pass around references without worry.
Java interop - you can use all the jars
"isomorphic" - write clojurescript for the browser, Clojure for the server
Some other example, Python and JS are both similar languages in many respects. But even something as simple as not having list comprehensions makes JS more fidgety when you're writing simple code.
Python not having great utils for stuff like "give me this dictionary, but without these three keys, but keep the original dictionary intact" (Clojure has dissoc for this) also makes it a bit fidgety compared to Clojure.
"You can write all of this" maybe, but at one point you're really going upstream from the base language.
new_dictionary = {__k: __v for __k, __v in original_dictionary.items() if __k not in {‘key1’, ‘key2’, ‘key3’}}
(written on my phone without a repl to double check but the technique is sound and i use it all the time)I want `select` and `dissoc`. I also would like for those to be performant (instead of requiring a bunch of copies). I don't want to have to choose between expressiveness and performance.
I don't write Clojure in the day-to-day but I think it hits those beats very well.
That, combined with the fact that everything is generally made up of simple data structures (lists, vectors, maps, and sets) just makes working with Clojure fun (for me at least).
Here’s a great example of what I’m talking about:
That includes every major Lisp dialect.
What's not Lisp heritage is to have a vector or hash table to which you can add an item to get a new container, such that the original remains the same.
It's a generalization of consing an item onto a list, though.
Say you're writing a pure function...
You start with just data and a sense of how the output might look. Let's say I have APIs providing me with a user record and a list of transactions, and I want to get that person's balance...
Maybe you start with some canned data
(let [person {:name "Matt"
:id 12345
:current_balance 100.10}
txns [{:person_id 12345 :amt 10}
{:person_id 44444 :amt 0.5}
{:person_id 55555 :amt 10}
{:person_id 12345 :amt 11}
{:person_id 12345 :amt -5}
{:person_id 66666 :amt 3}]]
(->> txns
count)
)
=> 6
and maybe you want to filter the transactions to just that user (let [person {:name "Matt"
:id 12345
:current_balance 100.10}
txns [{:person_id 12345 :amt 10}
{:person_id 44444 :amt 0.5}
{:person_id 55555 :amt 10}
{:person_id 12345 :amt 11}
{:person_id 12345 :amt -5}
{:person_id 66666 :amt 3}]]
(->> txns
(filter #(= (:id person)
(:person_id %)))
count)
)
=> 3
then you want to extract the amount for each of those (let [person {:name "Matt"
:id 12345
:current_balance 100.10}
txns [{:person_id 12345 :amt 10}
{:person_id 44444 :amt 0.5}
{:person_id 55555 :amt 10}
{:person_id 12345 :amt 11}
{:person_id 12345 :amt -5}
{:person_id 66666 :amt 3}]]
(->> txns
(filter #(= (:id person)
(:person_id %)))
(map :amt))
)
=> (10 11 -5)
and sum those amounts (let [person {:name "Matt"
:id 12345
:current_balance 100.10}
txns [{:person_id 12345 :amt 10}
{:person_id 44444 :amt 0.5}
{:person_id 55555 :amt 10}
{:person_id 12345 :amt 11}
{:person_id 12345 :amt -5}
{:person_id 66666 :amt 3}]]
(->> txns
(filter #(= (:id person)
(:person_id %)))
(map :amt)
(apply +))
)
=> 16
and add that to the existing balance (let [person {:name "Matt"
:id 12345
:current_balance 100.10}
txns [{:person_id 12345 :amt 10}
{:person_id 44444 :amt 0.5}
{:person_id 55555 :amt 10}
{:person_id 12345 :amt 11}
{:person_id 12345 :amt -5}
{:person_id 66666 :amt 3}]
bal-change (->> txns
(filter #(= (:id person)
(:person_id %)))
(map :amt)
(apply +))]
(+ bal-change (:current_balance person))
)
=> 116.1
the function is done! (defn update-person-balance [{:keys [id current_balance]} txns]
(let [bal-change (->> txns
(filter #(= id
(:person_id %)))
(map :amt)
(apply +))]
(+ bal-change current_balance)))
I posted here as a series of snippets, but this would have been a single block of code, refined and evaluated and refined and evaluated, over and over, in a REPL-integrated editor. It feels like sketching a portrait or sculpting clay."But I can do that in any ol' REPL!" you might say. Try Clojure -- really try it, with an nREPL connected to your running application and to your editor, capturing inputs, playing them back, writing evaluation output as comments -- and then tell me you want to go back to your old REPL. A notebook environment is similar, though clumsier IMO.
Come for the REPL-driven-development, stay for the immutability, the Java library ecosystem, the lisp syntax, the concurrency support, etc. I know going on and on about how great Clojure development is is basically a meme at this point, but that's because it's really great!
I mess with Clojure with some regularity and each time ultimately back away in a combination of frustration and nerd sniping self awareness as the ratio of editor setup blog reading meta work to actual work hits infinity.
Either I am terrible at finding good setup guides or it’s pearls before swine and I just don’t get the aha moment. I’m starting to think it’s the latter.
You're not the 'swine' here, you shouldn't feel that way. I've learned that sometimes paradigms require the programmer to be in the correct 'frame of mind' before it even begins to make sense, its not you.
In my limited experience, keeping an open mind to tools will get you much further and have more enjoyment in your career and life. Trying to force that square peg into the round hole will just make you hate new things. If the time comes and your mindset is right, the tech will still be there.
The thing that really helped was watching videos of talks / tutorials of people on youtube coding something up, for example [0]. I'm not calling that one out specifically as a great tutorial, that's just an example of what I mean =)... I personally would start with an example of a form and then build it up in stages, basically tinkering with the form as I work out how I want it to function.
Another thing is that you don't have to use a particular editor etc. I'm not sure for example if you think you have to use emacs. Don't get me wrong it's a great environment, but if you're a beginner to it and clojure, that's not a good combination. Pick something you like which hopefully has a good repl plugin. There are a fair few options these days. Intellij, vscode, atom, vim among others.
Worst case if you'd like me to sit down and have a short call to go over stuff I'd be happy to set aside a little time =)... I've done tutorial stuff before and it's always nice to help someone else get comfortable in the ecosystem.
Hope that helps.
- [0]: [Clojure, REPL & TDD: Feedback at Ludicrous Speed - Avishai Ish-Shalom](https://www.youtube.com/watch?v=Ngt29DyNDRM)
Your specific setup will depend on your primary editor. For emacs, the go-to is https://cider.mx/. E.g., with my cursor beside an s-expression, I can hit a couple keys to evaluate and view the output. Don't give up!
You could also try Calva if you use Visual Studio Code. It works great for me.
let updatePersonBalance = ({id, currentBalance}, txns) => {
let balChange = txns
.filter(i => id === i.personID)
.map(i => i.amt)
.reduce((i,j) => i + j);
return (balChange + currentBalance);
}
Defaulting to immutability it's not as useful in single-threaded environments like nodejs or the browser. The above function is pure though and it's an exact copy of yours but could make it smaller and still be readable.Picking Clojure over JS today is a very hard sell. Both are dynamic and use the same information model, objects/arrays vs maps/vectors but with the difference that JS has a big enabling ecosystem of libraries. And there's Typescript when you need it.
> Picking Clojure over JS today is a very hard sell.
I'm not sure I would choose ClojureScript over JS on the frontend. I haven't worked with ClojureScript much nor thought much about the trade-offs.
> Defaulting to immutability it's not as useful in single-threaded environments like nodejs or the browser.
It might not be as useful, but I think it's still useful. With immutability, you know that inside a scope you can do whatever you want with any data (ignoring atoms for a moment) and you're not going to step on somebody else's state.
> with the difference that JS has a big enabling ecosystem of libraries
ClojureScript has interoperability with all the JS libraries, no?
And if you need strong typing, then I'd agree Clojure is not a good fit.
Looking at state mgmt solutions for JS today, many will require or recommend immutable data - for very good reasons.