11. Compositionとは?pipe()を実装する

簡単  - accepted / - tried

Compositionとは?実は難しくない、@dan_abramov の説明を見ればわかります。

pipe() を実装してください。

例えこちらの演算関数

const times = (y) =>  (x) => x * y
const plus = (y) => (x) => x + y
const subtract = (y) =>  (x) => x - y
const divide = (y) => (x) => x / y

pipe() を使って、新しい演算関数を作れることになる


pipe([
  times(2),
  times(3)
])  
// x * 2 * 3

pipe([
  times(2),
  plus(3),
  times(4)
]) 
// (x * 2 + 3) * 4

pipe([
  times(2),
  subtract(3),
  divide(4)
]) 
// (x * 2 - 3) / 4

補足

  1. pipe()に渡す関数の引数は一つしかないと想定して良い

常にもっといい方法を求めよう。

(1)
(210)