Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //-----------------------------curry-------------------------
- const add4=(a1,a2,a3,a4) => a1+a2+a3+a4;
- const curry= func => {
- const prevParams=[];
- const currFunc= (...params) => {
- prevParams.push(...params);
- if ( prevParams.length >= func.length ) {
- return func(...prevParams);
- }
- else{
- return currFunc;
- }
- }
- return currFunc;
- }
- const cAdd4=curry(add4);
- console.log(cAdd4(3,5)(4)(3),cAdd4(1,2,3,4));
- //------------------------curry better-----------------------------------
- // definitions
- const curry = func => function currFunc(...params) {
- if (params.length >= func.length) {
- return func(...params);
- }
- return (...moreParams) => currFunc(...params, ...moreParams);
- }
- const partial = (func, ...params) => (...restParams) => func(...params, ...restParams); //partial
- // examples
- const add4 = (a1, a2, a3, a4) => a1 + a2 + a3 + a4;
- const cAdd4 = curry(add4);
- const x = cAdd4(4); //x exei params 4 execution context to ena mesa sto allo mexri to telos an grapsw y= x(3) neo mesa sto idio meros //me x1
- const x1 = x(5); //x1 exei params 5
- const x2 = x1(7); //x2 exei params 5
- console.log(x2(5)); //to moreParams ginetai 5
- const pAdd4 = partial(add4, 1, 3);
- console.log(pAdd4(2,4))
- console.log(pAdd4(2))
- console.log(pAdd4(3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement