4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JavaScript:Array.prototype.reduceとrest parametersを使ってパイプライン演算子っぽいものを作ってみた。ついでに関数合成

Last updated at Posted at 2018-03-19

reduceってパイプラインに似てるなと思ったので作ってみました。

  • 初期値xと数が決まってない複数の関数を引数にとって
  • ()内で、fはrest parameters ... で 実際に渡された引数による配列になって関数に渡されます。
  • 初期値xのアキュムレータaccに配列fの要素eを順に適用していく(reduce)
const pipe = x => (...fs) => fs.reduce( (acc, f) => f(acc), x)
// 使用例
const succ = n => n + 1
const square = n => n * n

   pipe(1)()
//=> 1
   pipe(1)(succ)
//=> 1 + 1 = 2
   pipe(1)(succ, succ)
//=> ( 1 + 1 ) + 1 = 3
   pipe(1)(succ, succ, square)
//=> (( 1 + 1 ) + 1 ) ^ 2 = 9
   pipe(1)(succ, square, succ)
//=> (( 1 + 1 ) ^ 2 ) + 1 = 5

まあそのうち|>が使えるようになるんでしょうが。

追記: ついでに関数合成っぽいもの

これって順序を逆にすれば関数合成ってことになるのかな?

const compose = (...fs) => x => fs.reduceRight( (acc, f) => f(acc), x)
// 使用例
   compose()(1)
//=> 1
   compose(succ)(1)
//=> 1 + 1 = 2
   compose(succ, succ)(1)
//=> ( 1 + 1 ) + 1 = 3
   compose(square, succ, succ)(1)
//=> (( 1 + 1 ) + 1 ) ^ 2 = 9
   compose(succ, square, succ)(1)
//=> (( 1 + 1 ) ^ 2 ) + 1 = 5
// 部分適用して関数にする
   const succ2Sq = compose(square, succ, succ)
//=> undefined
   succ2Sq(1)
//=> (( 1 + 1 ) + 1 ) ^ 2 = 9
   succ2Sq(2)
//=> (( 2 + 1 ) + 1 ) ^ 2 = 16

いけてる。
むしろこっちの方が使いでがありそう。

4
3
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?