That's basically one stateful way of implementing function composition, which in Haskell is trivial with the . operator:
cat::String->String->Stringcatab=a++" "++bletresult=cat"a".cat"b".cat"c"$"d"-- the $ operator is just a way to call the composed function with the parameter "d"-- result == "a b c d"
In TypeScript, there are some libraries that implement function composition like fp-ts, but you can create your own pretty easily using Array.prototype.reduceRight:
letcat:(a:string)=>(b:string)=>string=(a)=>(b)=>a+''+bconstcompose=(...fns:Function)=>(argument:any)=>fns.reduceRight((v,f)=>f(v),argument)console.log(compose(cat('a'),cat('b'),cat('c'))('d'))// => "a b c d"
In this compose implementation, we're using currying again to build a function that expects the parameter of the final composition to be used as the initial value for the reduceRight function. reduceRight iterates our functions from right to left, resolving each function and passing its return value as an argument to the previous one.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
First time I've seen that bind method. Quite enlightning.
In the other hand, you might get used to infinite recursion
That's basically one stateful way of implementing function composition, which in Haskell is trivial with the
.
operator:In TypeScript, there are some libraries that implement function composition like
fp-ts
, but you can create your own pretty easily using Array.prototype.reduceRight:In this
compose
implementation, we're using currying again to build a function that expects the parameter of the final composition to be used as the initial value for thereduceRight
function.reduceRight
iterates our functions from right to left, resolving each function and passing its return value as an argument to the previous one.