Created
June 16, 2018 16:56
-
-
Save pertrai1/5ceb9aace8b3558e16af787956b055e3 to your computer and use it in GitHub Desktop.
This file contains 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
function add(a, b) { | |
return a + b; | |
} | |
function sub(a, b) { | |
return a - b; | |
} | |
function mul(a, b) { | |
return a * b; | |
} | |
add(4, 5); /*? */ | |
sub(12, 4); /*? */ | |
mul(3, 4); /*? */ | |
function identityf(arg) { | |
return function() { | |
return arg; | |
} | |
} | |
var three = identityf(3); | |
three(); /*? */ | |
function addf(a) { | |
return function(b) { | |
return a + b; | |
} | |
} | |
addf(3)(4); /*? */ | |
function liftf(fn) { | |
return function(a) { | |
return function(b) { | |
return fn(a, b); /*? */ | |
} | |
} | |
} | |
var addf = liftf(add); | |
addf(3)(4); /*? */ | |
liftf(mul)(3)(4); /*? */ | |
function curry(fn, arg) { | |
return function(x) { | |
return fn(arg, x); /*? */ | |
} | |
} | |
var add3 = curry(add, 3); /*? */ | |
add3(4); /*? */ | |
curry(mul, 5)(6);/*? */ | |
// var inc = addf(1); /*? */ | |
// var inc = liftf(add)(1); | |
var inc = curry(add, 1); | |
inc(5); /*? */ | |
inc = liftf(add)(1); /*? */ | |
inc(inc(5)); /*? */ | |
function twice(fn) { | |
return function(x) { | |
return fn(x, x); /*? */ | |
} | |
} | |
add(11, 11); /*? */ | |
var doubl = twice(add); | |
doubl(11); /*? */ | |
function reverse(fn) { | |
return function(...args) { | |
return fn(...args.reverse()); | |
} | |
} | |
var bus = reverse(sub); /*? */ | |
bus(3, 2); /*? */ | |
function composeu(fn, fn1) { | |
return function(x) { | |
return fn1(fn(x)); | |
} | |
} | |
composeu(doubl, mul)(5); /*? */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment