跳到主要内容

Point-Free Function

Point-free(无点风格)尽量不显式写出数据参数,通过返回高阶函数再组合,让数据在最后一环传入,代码更偏「管道」。

项目中的实现

  • 源码: src/pointFreeFunction.ts
  • 导出: mapValuesfilterValuesjoinWithpointFreeUserNames

各函数示例

mapValues

import { mapValues } from 'wssf-kage-js';

mapValues((n: number) => n * 2)([1, 2, 3]); // => [2, 4, 6]

filterValues

import { filterValues } from 'wssf-kage-js';

filterValues((n: number) => n > 1)([1, 2, 3]); // => [2, 3]

joinWith

import { joinWith } from 'wssf-kage-js';

joinWith(', ')(['a', 'b']); // => 'a, b'

pointFreeUserNames

import { pointFreeUserNames } from 'wssf-kage-js';

pointFreeUserNames([
{ name: 'Ada', active: true },
{ name: 'Bob', active: false }
]);
// => 'Ada'

什么时候优先用

  • 组合多个纯变换,且希望阅读顺序接近数据流
  • 与柯里化工具一起构造可复用的小算子