Skip to content

Instantly share code, notes, and snippets.

@HerbM
Created October 2, 2016 02:05
Show Gist options
  • Save HerbM/9e9638b4f618874cea6a91e7fe3894f5 to your computer and use it in GitHub Desktop.
Save HerbM/9e9638b4f618874cea6a91e7fe3894f5 to your computer and use it in GitHub Desktop.
(defn rducts
([f aseq] (lazy-seq (rducts f (f (first aseq)) (rest aseq))))
([f accum aseq]
(lazy-seq (cons accum (let [fs (first aseq) rs (rest aseq)]
(if (not (seq rs))
(list (f accum fs))
(rducts f (f accum fs) rs)))))))
(take 5 (rducts + (range)))
(rducts conj [1] [2 3 4])
(last (rducts * 2 [3 4 5]))
(= (take 5 (rducts + (range))) [0 1 3 6 10])
(= (rducts conj [1] [2 3 4]) [[1] [1 2] [1 2 3] [1 2 3 4]])
(= (last (rducts * 2 [3 4 5])) (reduce * 2 [3 4 5]) 120)
(defn rducts
([f aseq] (lazy-seq (rducts f (f (first aseq)) (rest aseq))))
([f accum aseq]
(lazy-seq (cons accum (let [fs (first aseq) rs (rest aseq)]
(if (not (seq rs))
(list (f accum fs))
(rducts f (f accum fs) rs)))))))
(take 5 (rducts + (range)))
(rducts conj [1] [2 3 4])
(last (rducts * 2 [3 4 5]))
(= (take 5 (rducts + (range))) [0 1 3 6 10])
(= (rducts conj [1] [2 3 4]) [[1] [1 2] [1 2 3] [1 2 3 4]])
(= (last (rducts * 2 [3 4 5])) (reduce * 2 [3 4 5]) 120)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment