Last active
October 4, 2015 11:27
-
-
Save number23/2627885 to your computer and use it in GitHub Desktop.
clojure for List comprehension
This file contains 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
user=> (for [file "ABCDEFGH" rank (range 1 9)] (format "%c%d" file rank)) | |
("A1" "A2" "A3" "A4" "A5" "A6" "A7" "A8" "B1" "B2" "B3" "B4" "B5" "B6" "B7" "B8" "C1" "C2" "C3" "C4" "C5" "C6" "C7" "C8" "D1" "D2" "D3" "D4" "D5" "D6" "D7" "D8" "E1" "E2" "E3" "E4" "E5" "E6" "E7" "E8" "F1" "F2" "F3" "F4" "F5" "F6" "F7" "F8" "G1" "G2" "G3" "G4" "G5" "G6" "G7" "G8" "H1" "H2" "H3" "H4" "H5" "H6" "H7" "H8") | |
user=> (for [rank (range 1 9) file "ABCDEFGH"] (format "%c%d" file rank)) | |
("A1" "B1" "C1" "D1" "E1" "F1" "G1" "H1" "A2" "B2" "C2" "D2" "E2" "F2" "G2" "H2" "A3" "B3" "C3" "D3" "E3" "F3" "G3" "H3" "A4" "B4" "C4" "D4" "E4" "F4" "G4" "H4" "A5" "B5" "C5" "D5" "E5" "F5" "G5" "H5" "A6" "B6" "C6" "D6" "E6" "F6" "G6" "H6" "A7" "B7" "C7" "D7" "E7" "F7" "G7" "H7" "A8" "B8" "C8" "D8" "E8" "F8" "G8" "H8") | |
from: http://stackoverflow.com/questions/10966142/what-is-the-difference-between-while-and-when-in-clojure | |
:when iterates over the bindings, but only evaluates the body of the loop when the condition is true. | |
:while iterates over the bindings and evaluates the body until the condition is false: | |
(for [x (range 20) :when (not= x 10)] x) | |
; =>(0 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19) | |
(for [x (range 20) :while (not= x 10)] x) | |
; => (0 1 2 3 4 5 6 7 8 9) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment