Pure Function(II)

Q: Definition?

相同的輸入丟入,永遠都會回傳相同的輸出,
並且不對任何該函數以外的任何作用域產生影響

Q: Pros?

  • 有利重構(Refactor)
  • 方便測試
  • 平行化
  • 利於建快取(Cache)
  • 易與其他函數組合(Compose)
    結合律

Q: What is Curry?

// 封裝陣列的 slice 讓它變為 functional 的 curry function。
// [1, 2, 3].slice(0, 2)
var slice = undefined;
var slice = _.curry(function(start, end, xs){   return xs.slice(start, end);});

Compose

  • Return value is a function

  • Left direction

var fn = compose(f, g);

Compose

結合律

各種接

各種結合

PointFree

Pointfree style means never having to say your data

手法:移除不必要的命名
目標:保持簡潔(concise)
通用 (generic)

Example Application

Pure Function (II)

By chiao

Pure Function (II)

  • 213