Ramda:记住并使用价值

我正在尝试“Ramdafy”一个功能并使其无点。这是我走了多远:


const hasFetchedForFilterQueryString = state => {

  const filterQueryString = getFilterQueryString(state);

  return either(

    pipe(getPages, has(filterQueryString)),

    pipe(getNewlyLoadedUsers, has(filterQueryString)),

  )(state);

};


// or


const hasFetchedForFilterQueryString = state =>

  either(

    pipe(getPages, has(getFilterQueryString(state))),

    pipe(getNewlyLoadedUsers, has(getFilterQueryString(state))),

  )(state);

我想知道是否有一种方法可以编写更多的无点,因此无需保存filterQueryString变量并且必须手动应用state?


收到一只叮咚
浏览 138回答 1
1回答

BIG阳

我通常同意Bergi的评论。无点不应该是一个目标。当我开始编写 Ramda 时,我就迷上了它,其中有许多功能旨在帮助使事情变得无意义。我很少再推荐他们了。我不认为useWith或者converge经常使函数更易于阅读或维护。但是有一个函数,lift,在 FP 世界中广为人知,在这里很有帮助。我认为是lift将功能从价值领域提升到这些价值的容器领域。因此lift (add) (Maybe(3), Maybe(5)) //=> Maybe(8)。当与产生我们的值的函数一起使用时,它可能会像这样工作:lift ((a, b) => a + b) (&nbsp; n => n * 2,&nbsp;&nbsp; n => n - 1) (5)&nbsp;//=> (5 * 2) + (5 -1) //=> 14`.这正是您想要对您的功能做的事情。所以这是一个lift基于 - 的无点版本:const hfffqs2 = either (&nbsp; lift (has) (getFilterQueryString, getPages),&nbsp; lift (has) (getFilterQueryString, getNewlyLoadedUsers))我发现这比您上面的任何一个版本都更具可读性。我认为该版本可与我能想到的最简单的非 Ramda 版本相媲美:const hfffqs3 = state => {&nbsp; const query = getFilterQueryString(state);&nbsp; return query in getPages(state)&nbsp; &nbsp; &nbsp; || query in getNewlyLoadedUsers(state)}不过,它不一定更具可读性。而且它的效率较低——除了额外的函数调用——它必须调用getFilterQueryString两次。但它是一个可读和可维护的功能。如果lift还不是很清楚,中间格式可能如下所示:const hfffqs1 = either (&nbsp; state => has(getFilterQueryString(state), getPages(state)),&nbsp; state => has(getFilterQueryString(state), getNewlyLoadedUsers(state)))通过应用getFilterQueryStringand getState,lift (has)我们得到state => has(getFilterQueryString(state), getPages(state)), 和下一行的等价物。我们已经将函数从字符串和对象领域提升到返回字符串和对象的函数领域。我们可以传递给这两个函数来获取一个字符串和一个对象。hasstate您可以在以下代码段中看到这些操作:// Dummy implementations just for demo.&nbsp; `state` is a small integer.const getFilterQueryString = (state) => ['foo', 'bar', 'baz', 'qux', 'corge', 'grault'][state]const getPages = (state) => [{foo: 1, bar: 2}, {foo: 1, corge: 2, grault: 3}, {baz: 1}, {foo: 1, grault: 2}, {corge: 1, baz: 2}, {bar: 1, qux: 2}][state]const getNewlyLoadedUsers = (state) => [{baz: 1, qux: 2}, {corge: 1, grault: 2}, {foo: 1, corge: 2}, {baz: 1, qux: 2}, {foo: 1, qux: 2}, {bar: 1, corge: 2}][state]const hasFetchedForFilterQueryString = state => {&nbsp; const filterQueryString = getFilterQueryString(state);&nbsp; return either(&nbsp; &nbsp; pipe(getPages, has(filterQueryString)),&nbsp; &nbsp; pipe(getNewlyLoadedUsers, has(filterQueryString)),&nbsp; )(state);};console .log ([0, 1, 2, 3, 4, 5].map(hasFetchedForFilterQueryString))const hfffqs1 = either (&nbsp; state => has(getFilterQueryString(state), getPages(state)),&nbsp; state => has(getFilterQueryString(state), getNewlyLoadedUsers(state)))console .log ([0, 1, 2, 3, 4, 5].map(hfffqs1))const hfffqs2 = either (&nbsp; lift (has) (getFilterQueryString, getPages),&nbsp; lift (has) (getFilterQueryString, getNewlyLoadedUsers))console .log ([0, 1, 2, 3, 4, 5].map(hfffqs2))const hfffqs3 = state => {&nbsp; const query = getFilterQueryString(state);&nbsp; return query in getPages(state)&nbsp; &nbsp; &nbsp; || query in getNewlyLoadedUsers(state)}console .log ([0, 1, 2, 3, 4, 5].map(hfffqs3)).as-console-wrapper {min-height: 100% !important; top: 0}<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.0/ramda.js"></script><script> const {either, pipe, has, lift} = R&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</script>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript