Fork me on GitHub
#code-reviews
<
2020-08-19
>
seancorfield00:08:35

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

seancorfield00:08:14

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
seancorfield00:08:00

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

seancorfield00:08:01

@stopachka How about that? ^

😮 3
seancorfield00:08:22

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
stopa01:08:28

Amaazing!

noisesmith05:08:42

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