Skip to content

Instantly share code, notes, and snippets.

@tmoerman
Created May 14, 2014 13:07
Show Gist options
  • Save tmoerman/fd45a689fcc4c85ba96e to your computer and use it in GitHub Desktop.
Save tmoerman/fd45a689fcc4c85ba96e to your computer and use it in GitHub Desktop.
(defn toggled
"Add a toggle to an input channel.
The ctrl channel accepts truthy, falsy or :toggle values.
The toggle value in the loop determines whether the input value is piped
to the output channel or not.
_____
=[in]===>| |
| alt |=[out]=>
=[ctrl]=>| |
'-----'
"
([in ctrl]
(toggled in ctrl (chan)))
([in ctrl out]
(go-loop [toggle true]
(alt! in ([v] (if (and v toggle) (>! out v))
(recur toggle))
ctrl ([v] (recur (if (= :toggle v) (not toggle) v)))))
out))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment