Skip to content

Instantly share code, notes, and snippets.

@jedschneider
Created November 3, 2011 17:44
Show Gist options
  • Select an option

  • Save jedschneider/1337161 to your computer and use it in GitHub Desktop.

Select an option

Save jedschneider/1337161 to your computer and use it in GitHub Desktop.
user=> (first (list 1 2 3))
1
user=> (first {:foo "bar" :baz "buff"})
[:foo "bar"]
user=> (first [1 2 3])
1
user=> (first (set [3 1 2 3]))
1
;;;; perform a map function (addition) across two types of collections
user=> (def myset (set [1 2 3]))
#'user/myset
user=> (def mylist (list 1 2 3 4))
#'user/mylist
user=> (map + myset mylist)
(2 4 6)
;;; construct a collection containing both collections
user=> (cons mylist myset)
((1 2 3 4) 1 2 3)
;;; compose to get all unique values from both collections
user=> (set (flatten (cons mylist myset)))
#{1 2 3 4}
;;; Abstraction
;;; use a built in macro to rewrite the above into a higher abstraction that looks more like chaining methods in oop
;;; takes the result of the first form and passes it to the second function, takes the result and passes it to the third.
user=> (-> (cons mylist myset) flatten set)
#{1 2 3 4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment