Lets Talk more Clojure
Reading time: 1 minSets
Sets are simply a mathematical set, which means it retuns the no duplicates from a collection.
A collection such as the one below only returns the unique elements.
[code lang=clojure]
<br />(set '(1 1 1 2 2 2 3 3 3))
#{1 2 3}
[/code]
To retrieve the union of two collections, you can use the union method.
[code lang=clojure]
(require 'clojure.set)
(set/union #{4 2 1} #{ 2 3 4 })
[/code]
To get the intersection, unsurprisingly, we wuse the intersection method
[code lang=clojure]
(set/intersection #{1 2 3 4} #{2 3 5})
#{2 3}
[/code]
Higher order functions
One of the aspects that I like about clojure is that it's lisp and functional.
Which means we can write code like this.
[code lang=clojure]
((fn [f] (f 4 5)) *)
[/code]
A function taking another function. Essential a higher order function, this I believe makes for some elegant code solutions.
I will talk more about this next time.