首页 > 解决方案 > 如何在 JS 中演示简单的无点样式

问题描述

Point-Free 风格或默认编程在 wikipedia 中使用 Python 进行了解释。

def example(x):
  y = foo(x)
  z = bar(y)
  w = baz(z)
  return w

和..

def flow(fns):
    def reducer(v, fn):
        return fn(v)

    return functools.partial(functools.reduce, reducer, fns)

example = flow([baz, bar, foo])

如何以最简单易懂的概念形式使用 JS 来演示这种效果?

标签: javascriptfunctional-programmingpointfree

解决方案


这可以很容易地变成 JS:

 function example(x) {
  const y = foo(x);
  const z = bar(y);
  const w = baz(z);
  return w;
}

...和

function flow(fns) {
  function reducer(v, fn) {
     return fn(v);
  }

  return fns.reduce.bind(fns, reducer);
}

const example = flow([baz, bar, foo]);

推荐阅读