Created
December 8, 2015 14:04
-
-
Save domgetter/71aa6cc437d6d32d26ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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