code-reviews

seancorfield 2020-08-19T00:08:35.236800Z

(defn pascal [n]
  (take n (iterate #(mapv (fn [[x y]] (+ x y)) (partition 2 1 (cons 0 (into % [0])))) [1])))

seancorfield 2020-08-19T00:10:14.237700Z

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.

😮 1
seancorfield 2020-08-19T00:11:00.238800Z

So you can have an infinite sequence of Pascal's triangle rows and you just take as many rows as you want.

seancorfield 2020-08-19T00:12:01.239100Z

@stopachka How about that? ^

😮 1
seancorfield 2020-08-19T00:16:22.240200Z

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.

❤️ 1
2020-08-19T01:16:28.241Z

Amaazing!

2020-08-19T05:23:42.241600Z

also concat is more forgiving about doesn't have subtle bug possibilities via seq / vector conj location