Skip to content

Instantly share code, notes, and snippets.

@domgetter
Created December 8, 2015 14:04
Show Gist options
  • Save domgetter/71aa6cc437d6d32d26ec to your computer and use it in GitHub Desktop.
Save domgetter/71aa6cc437d6d32d26ec to your computer and use it in GitHub Desktop.
(def fruits ["apple" "banana" "orange" "avocado" "pepper" "pumpkin" "grape" "tomato" "blueberry" "raspberry"])
(def users ["John" "Mary" "Joseph"])
; You can be imperative
(for [i (range 0 (count fruits))]
(.length (fruits i)))
; (5 6 6 7 6 7 5 6 9 9)
; You can be more declarative
(for [fruit fruits]
(.length fruit))
; (5 6 6 7 6 7 5 6 9 9)
; You can just map over the list
(map #(.length %) fruits)
; (5 6 6 7 6 7 5 6 9 9)
; (for) in clojure results in the cartesian product of all the local variables
(for [fruit fruits
user users]
(str user " ate one " fruit))
; ("John ate one apple" "Mary ate one apple" "Joseph ate one apple"
; "John ate one banana" "Mary ate one banana" "Joseph ate one banana"
; "John ate one orange" "Mary ate one orange" "Joseph ate one orange"
; "John ate one avocado" "Mary ate one avocado" "Joseph ate one avocado"
; "John ate one pepper" "Mary ate one pepper" "Joseph ate one pepper"
; "John ate one pumpkin" "Mary ate one pumpkin" "Joseph ate one pumpkin"
; "John ate one grape" "Mary ate one grape" "Joseph ate one grape"
; "John ate one tomato" "Mary ate one tomato" "Joseph ate one tomato"
; "John ate one blueberry" "Mary ate one blueberry" "Joseph ate one blueberry"
; "John ate one raspberry" "Mary ate one raspberry" "Joseph ate one raspberry")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment