Skip to content

Instantly share code, notes, and snippets.

@christiantakle
Last active January 2, 2016 11:49
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 christiantakle/8299140 to your computer and use it in GitHub Desktop.
Save christiantakle/8299140 to your computer and use it in GitHub Desktop.
Using functional composition as before, around or after filter. This is a smart use of tap, to compose a sequence of functions that need somekind of sideeffect and provide a clear seperation of pure and impure functions
//-- http://allong.es/try/
tap = allong.es.tap
flip = allong.es.flip
curry = allong.es.curry
compose = allong.es.compose
call = allong.es.call
//-- square :: Int -> Int
function square(x) { return x*x }
//-- log :: String -> a -> IO ()
function log(msg, value) { console.log(msg, value) }
logger = curry(log)
tapWith = flip(tap)
logAndSquare = compose(square, tapWith(logger('Logging this')))
//-- Since allong.es curry functions with `call` it could be written as
logAndSquare = compose(square, tapWith(call(log, 'Logging this')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment