about summary refs log blame commit diff stats
path: root/seq-primer.clj
blob: 0f9fe43de6ee4d324ca659870a979ce2d23563d3 (plain) (tree)




































                                                                                                                           
;; 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