The One Thing That Made Me Choose Clojure Over Javascript — How Simplicity Wins

Select only unique objects from a list

Javascript:

const myDistinctArray = [
...new Set(myObjArray.map(JSON.stringify)),
].map(JSON.parse);
// This one presumes unique "id" === unique object 
let uniqueObjArray = [
...new Map(
objArray.map((item) => [item["id"], item])
).values(),
];

Clojure

(distinct my-object-list) // that's it

Why does the Clojure one work?

Clojure promotes immutability by default, and this includes maps. So maps are compared directly by value and not by reference or identity.

Conclusion

The beautiful thing about Clojure is how expressive the language is and how much you can say with very few lines of code.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ovidiu Stoica

Ovidiu Stoica

Hi, I’m Ovi Stoica! I help people build quality software and I write about technology, startups and marketing for developers.