Guest User

Untitled

a guest
Jan 22nd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. (defn slide
  2. "Slides a sequence by n. That is, apply f to every set of n elements of src.
  3. Ex: (slide \"vida\" 2) returns (\"da\" \"id\" \"vi\").
  4. Ex: (slide (fn [c] (reduce #(+ %1 %2) c)) '(1 2 3 4 5) 4) return ((14 10) (3 4 5))
  5. Ex: (slide (fn [c] (reduce #(+ %1 %2) c)) '(1 2 3 4 5) 2) return ((9 7 5 3) (5))
  6. Returns a list where first is the result of f applied to every slide and
  7. rest are the elements of src that couldn't fit in a slide of size n."
  8. [f src n]
  9. (loop [s src
  10. r (empty src)]
  11. (let [w (take n s)]
  12. (if (> n (count w))
  13. (list r w)
  14. (recur (rest s)
  15. (lazy-seq (cons (f w) r)))))))
Add Comment
Please, Sign In to add comment