Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Solutions/1-pipe.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';

// Define the type "function, that takes one argument
// and returnes the value of the same type"
type oneArg<T> = (arg: T) => T;

// Construct the "pipe" so it suits the exercise requirements
const pipe = <T>(...fns: oneArg<T>[]): oneArg<T> =>
x => fns.reduce((v, f) => f(v), x);


// Usage

const inc = (x: number) => ++x;
const twice = (x: number) => x * 2;
const cube = (x: number) => Math.pow(x, 3);

// You cannot do:
//
// const wrongFunc = x => 'someString';
// pipe(inc, twice, wrongFunc);

// Also you cannot do:
//
// const wrongFunc = (x, y) => x + y;
// pipe(inc, twice, wrongFunc);

// Because wrongFunc does not suit the type
// (arg: T) => T

const f = pipe(inc, twice, cube);
console.log(f(5));