簡體   English   中英

如何在JS中演示簡單的無點樣式

[英]How to demonstrate simple point-free style in JS

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演示這種效果?

可以輕松地將其轉換為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]);

這是函數組合,最簡單的解決方案是為給定的示例提供具有正確Arity的組合組合器:

 const foo = x => `foo(${x})`; const bar = x => `bar(${x})`; const baz = x => `baz(${x})`; const comp3 = (f, g, h) => x => f(g(h(x))); const fun = comp3(foo, bar, baz); console.log( fun(123)) 

為此, comp3的最后一個參數是咖喱,而函數參數都是一元函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM