Skip to content

Instantly share code, notes, and snippets.

@kellegous
Last active May 6, 2017 18:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kellegous/8fa436166fc296e575e3a2a3b49777ad to your computer and use it in GitHub Desktop.
Save kellegous/8fa436166fc296e575e3a2a3b49777ad to your computer and use it in GitHub Desktop.
(ns playsync.core
(:require [clojure.core.async
:as a
:refer [>! <! >!! <!! go chan buffer close! thread
alts! alts!! timeout]]))
(defn pipe-through
[f in out]
(go (>! out (f (<! in)))))
(defn pipeline-of
[& stages]
(let [first-chan (chan)]
(loop [first-chan first-chan in first-chan stages stages]
(if (empty? stages)
#(do
(go (>! first-chan %))
(<!! in))
(let [out (chan)]
(pipe-through (first stages) in out)
(recur first-chan out (rest stages)))))))
(defn -add
[a b]
((apply pipeline-of (repeat a inc)) b))
(def add (memoize -add))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment