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
Please follow the template and fill the info. A PR will be auto-generated and always reflect on your changes.
Detailed solution/guide is not required, but please be sure the challenge is solvable.
Info
difficulty: mediumtitle: Pipetags: array
Question
Implement a type-safe version of the function composition pipe() function. pipe() takes a series of functions as an argument and returns a function with the arguments of the first and the return-type of the last.
A simple JS implementation would be:
constpipe=(...fns: (()=>any)[])=>(...args: any[])=>fns.reduce((p,c)=>[c.apply(null,p)],args);conststringToArray=(str: string)=>Array.from(str);constreverseArray=(arr: any[])=>arr.reverse();constarrayToString=(arr: string[])=>arr.join("");constreverseString=pipe(stringToArray,reverseArray,arrayToString);// should be (str: string) => string// same as (str: string) => arrayToString(reverseArray(stringToArray(str)))
It creates a pipeline, hence the name. Each function passes its return value to the next function. Running reverseString(str) would result in:
Info
Question
Implement a type-safe version of the function composition pipe() function. pipe() takes a series of functions as an argument and returns a function with the arguments of the first and the return-type of the last.
A simple JS implementation would be:
It creates a pipeline, hence the name. Each function passes its return value to the next function. Running reverseString(str) would result in:
stringToArray(str) -> reverseArray(arr) -> arrayToString(arr)
Template
Test Cases
The text was updated successfully, but these errors were encountered: