Guest User

Untitled

a guest
Mar 23rd, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. ;; map-alt is like the map xform but calls the funtions in an alternating order
  2. ;; (f i0) (g i1) (h i2) (f i3) ...
  3. ;;
  4. ;; In other words, map-alt spreads fn calls across elements, whereas (mapcat (juxt ...)) calls all
  5. ;; fns on each element.
  6.  
  7. (defn map-alt
  8. ([] (map identity))
  9.  
  10. ([f] (map f))
  11.  
  12. ([f g]
  13. (fn [rf]
  14. (let [toggle (volatile! false)]
  15. (fn
  16. ([] (rf))
  17. ([result] (rf result))
  18. ([result input]
  19. (rf result (if (vswap! toggle not) (f input) (g input))))))))
  20.  
  21. ([f g & more]
  22. (fn [rf]
  23. (let [fv (into [f g] more)
  24. cnt (count fv)
  25. inc0 (fn [n] (let [n (unchecked-inc n)] (if (>= n cnt) 0 n)))
  26. i (volatile! -1)]
  27. (fn
  28. ([] (rf))
  29. ([result] (rf result))
  30. ([result input]
  31. (rf result ((nth fv (vswap! i inc0)) input))))))))
Add Comment
Please, Sign In to add comment