You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This post is the first in a series that will be dedicated to implementating native versions of Haskell functions according to JavaScript ES6 standards. Full source can be found in this GitHub repo. You are more than welcome to contribute!
ƒ.comp
Function composition.
(.):: (b->c) -> (a->b) ->a->c
/** * Function composition * @param ...fs functions to compose * @return composed function **/exportfunctioncomp(...fs){return(v, ...args)=>fs.reduceRight((g,f)=>f(g, ...args),v);}
flip f takes its (first) two arguments in the reverse order of f.
flip:: (a->b->c) ->b->a->c
/** * Flip function arguments * @param f function to flip * @return f applied with args in reverse order **/exportfunctionflip(f){return(a,b, ...args)=>f(b,a, ...args);}
until p f yields the result of applying f until p holds.
until:: (a->Bool) -> (a->a) ->a->a
/** * Applies a function which is passed as the second argument to * the third argument and it comapares the result with the condition, * if the condition evaluates to true, it prints the result, if not, * it passes the result to the function and repeats the cycle as long * as the condition is matched * @param condition condition to be applied to f * @param f function to match against * @return result if condition is true else repeat cycle **/exportfunctionuntil(condition,f){return(...args)=>{varr=f(...args);returncondition(r) ? r : until(condition,f)(r);};}
zip takes two lists and returns a list of corresponding pairs. If one input list is short, excess elements of the longer list are discarded."
zipWith generalises zip by zipping with the function given as the first argument, instead of a tupling function. For example, zipWith (+) is applied to two lists to produce the list of corresponding sums."
/** * Zip two arrays into a list of n-ples * @param ...xs arrays to zip * @return a list of of n-ples **/exportfunctionzip(...xs){varr=[],nple=[],length=Math.min(...xs.map(x=>x.length));for(vari=0;i<length;i++){xs.forEach(x=>nple.push(x[i]));r.push(nple);nple=[];}returnr;}/** * Generalises zip by zipping with the function given * as the first argument, instead of a tupling function. * @param op function to zip with * @param ...xs arrays to zip * @return array zipped with the op function **/exportfunctionzipWith(op, ...xs){zip(...xs).map((x)=>x.reduce(op));}
Originally posted
2015-11-12
.This post is the first in a series that will be dedicated to implementating native versions of Haskell functions according to JavaScript ES6 standards. Full source can be found in this GitHub repo. You are more than welcome to contribute!
ƒ.comp
Examples
ƒ.flip
Examples
ƒ.until
Examples
List operations
Examples
Special folds
Examples
ƒ.zip and ƒ.zipWith
Examples
The text was updated successfully, but these errors were encountered: