Last active
January 2, 2016 11:49
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//-- 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