This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-08-19
Channels
- # announcements (4)
- # asami (1)
- # babashka (48)
- # beginners (84)
- # bristol-clojurians (1)
- # calva (15)
- # chlorine-clover (11)
- # cider (37)
- # clj-kondo (17)
- # clojure (72)
- # clojure-europe (13)
- # clojure-italy (43)
- # clojure-nl (6)
- # clojure-spec (8)
- # clojure-uk (19)
- # clojuredesign-podcast (7)
- # clojurescript (132)
- # code-reviews (7)
- # conjure (3)
- # cursive (24)
- # datascript (10)
- # datomic (61)
- # docker (4)
- # duct (24)
- # emacs (2)
- # figwheel-main (8)
- # fulcro (43)
- # graalvm (5)
- # juxt (1)
- # keechma (14)
- # malli (2)
- # off-topic (120)
- # re-frame (111)
- # reagent (6)
- # reitit (13)
- # shadow-cljs (118)
- # spacemacs (3)
- # tools-deps (32)
- # uncomplicate (5)
- # xtdb (6)
(defn pascal [n]
(take n (iterate #(mapv (fn [[x y]] (+ x y)) (partition 2 1 (cons 0 (into % [0])))) [1])))
For a given row, if you prepend and append 0 and then break into (overlapping) pairs, then add each pair, you get the next rows.
😮 3
So you can have an infinite sequence of Pascal's triangle rows and you just take as many rows as you want.
You could safely use (concat [0] % [0])
instead of (cons 0 (into % [0]))
if you find that clearer -- mapv
is eager so you won't get a stack overflow from a lazy computation.
❤️ 3
also concat is more forgiving about doesn't have subtle bug possibilities via seq / vector conj location