about summary refs log tree commit diff stats
path: root/clj/seq-primer.clj
blob: 0f9fe43de6ee4d324ca659870a979ce2d23563d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
;; From Jack Rusher
;; https://gist.github.com/jackrusher/959b35e031fb9b2d30c0211337a310ed
;; Condensed visual tutorial in #Bauhaus style for a subset of the #Clojure seq API (inspired by similar JS tweets)

(def  ')
(def  ')
(def  ')

(first [  ])   ; ●
(second [  ])  ; ■
(nth [  ] 2)   ; ▲
(rest [  ])    ; (■ ▲)
(last [  ])    ; ▲
(butlast [  ]) ; (● ■)

(map (partial = ) [   ]) ; (true false true false)

(filter (partial = ) [   ])     ; (■ ■)
(remove (partial = ) [   ])     ; (● ▲)
(distinct [     ])             ; (■ ● ▲)
(interpose ' [  ])               ; (● ▲ ● ▲ ●)
(interleave [  ] [  ] [  ]) ; (■ ▲ ● ■ ▲ ● ■ ▲ ●)

(take 3 [     ])                       ; (■ ■ ●)
(take-nth 2 [       ])               ; (■ ● ▲ ■)
(take-while (partial = ) [       ]) ; (■ ■)
(drop-while (partial = ) [       ]) ; (● ● ▲ ▲ ■ ■)

(partition 2 [       ])   ; ((■ ■) (● ●) (▲ ▲) (■ ■))
(partition 2 1 [       ]) ; ((■ ■) (■ ●) (● ●) (● ▲) (▲ ▲) (▲ ■) (■ ■))
(split-at 3 [       ])    ; [(■ ■ ●) (● ▲ ▲ ■ ■)]

(frequencies [       ]) ; {■ 4, ● 2, ▲ 2}

(some (partial = ) [   ])   ; true
(every? (partial = ) [   ]) ; false 
(every? (partial = ) [  ])   ; true