Created
November 28, 2015 19:14
-
-
Save nornagon/03b85fbc22b3613087f6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn sliding | |
([^long n] (sliding n 1)) | |
([^long n ^long step] | |
(fn [rf] | |
(let [a (java.util.ArrayDeque. n)] | |
(fn | |
([] (rf)) | |
([result] | |
(let [result (if (.isEmpty a) | |
result | |
(let [v (vec (.toArray a))] | |
;;clear first! | |
(.clear a) | |
(unreduced (rf result v))))] | |
(rf result))) | |
([result input] | |
(.add a input) | |
(if (= n (.size a)) | |
(let [v (vec (.toArray a))] | |
(dorun (take step (repeatedly #(.removeFirst a)))) | |
(rf result v)) | |
result))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This isn't getting us quite the same thing as
partition-all
, in terms of the sequences returned (and getting a transducing version ofpartition-all
is what we want here, right?):