Skip to content

Instantly share code, notes, and snippets.

@favila
Created December 29, 2014 17:58
Show Gist options
  • Save favila/ecdd031e22426b93a78f to your computer and use it in GitHub Desktop.
Save favila/ecdd031e22426b93a78f to your computer and use it in GitHub Desktop.
as-transducer: convenience function to make a full transducer from a reducing-step function which accepts three arguments: the transforming function (xf), the reduced value, and the step value from the reduction.
(defn as-transducer [f]
"Convert function f into a transducer function.
This is merely a more convenient way to write an efficient reducing-step
function without the transducer signature.
The function should have the signature `(f transformer opaque-collection value)`
and return either `opaque-collection` or the return value of
`(transformer opaque-collection value)` to return new `opaque-collection`
things with `value` \"added\" to it. It can also box its return value with
`reduced` to terminate reduction early."
(fn [xf]
(fn
([] (xf))
([r] (xf r))
([r v] (f xf r v)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment