简体   繁体   English

如何在不特别调用return函数的情况下使用ramda来处理多个参数

[英]How to curry multiple parameters with ramda without specifically invoking the return function

is it possible to create af function with multiple arguments that returns a compose and invoke it automatically with the last argument. 是否可以创建带有多个参数的AF函数,这些函数返回compose并使用最后一个参数自动调用它。 see the example below 见下面的例子

const data = {
  hello: 'world'
}

const propContains = (values, key) => compose(contains(__,values),prop(key))
propContains(['wor','ld'], 'hello')(data) // --> false
propContains(['wor','ld'], 'hello', data) // --> Function

// this is working but don't want to invoke the compose with the last argument
const propContains2 = (values, key, obj) => compose(contains(__,values),prop(key))(obj)
propContains2(['wor','ld'], 'hello', data) // --> false  

Here is the Ramda REPL 这是Ramda REPL

It's fairly unclear what you're looking to do. 目前还不清楚您要做什么。 Here's one version that looks for only a single value: 这是一个仅查找单个值的版本:

let propContains = curry((value, key, obj) => 
    contains(value, prop(key, obj)));
propContains('wor', 'hello', data); //=> true
propContains('ld', 'hello', data); //=> true
propContains('foo', 'hello', data); //=> false

And here's one that checks multiple values, essentially map ping over the above: 这是一个检查多个值的方法,实际上是对以上内容进行ping map

let propContains = curry((value, key, obj) => 
    map(contains(__, prop(key, obj)), value));
propContains(['wor', 'ld', 'foo'], 'hello', data); //=> [true, true, false]

And if you want to see if they're all contained, you can do 如果要查看它们是否全部包含在内,可以执行

let propContainsAll = curry((value, key, obj) => 
    all(contains(__, prop(key, obj)), value));
propContainsAll(['wor', 'ld'], 'hello', data); //=> true
propContainsAll(['wor', 'ld', 'foo'], 'hello', data); //=> false

You could do a similar thing for any . 您可以为any做类似的事情。

As all of these are curried with Ramda's curry , you can choose whether to supply the final parameter on the first call to get the data or to skip it and get back a function. 由于所有这些都是由Ramda的curry进行咖喱处理的,因此您可以选择是在第一次调用时提供最终参数以获取数据还是跳过数据并返回函数。 It's not clear to me if this is what you're looking for, though. 不过,对于我来说,这还不是很清楚。

Update 更新资料

I was quite confused by the name. 这个名字让我很困惑。 " propContains ", which to me meant that the value of the property contained something. propContains ”,对我而言,意味着财产的价值包含某些东西。 Now I see that you really want something I might write as " propContainedIn " or some such: 现在,我看到您真的想要我写成“ propContainedIn ”之类的东西:

// propContainedIn :: [a] -> String -> Object{String: a} -> Bool
const propContainedIn = curry((values, key, obj) => 
    contains(prop(key, obj), values));
propContainedIn(['wor', 'ld'], 'hello')(data) //=> false
propContainedIn(['wor', 'ld'], 'hello', data) //=> false
propContainedIn(['wor', 'ld', 'world'], 'hello', data) //=> true
propContainedIn(['wor', 'ld', 'world'], 'hello')(data) //=> true

Although I'm sure that with enough work this could be made points-free, it would surely be much less readable than this, so I don't see the point. 尽管我确信通过足够的工作可以将其设置为无点,但它的可读性肯定比这要差得多,所以我看不到重点。

You can see this too on the (updated) Ramda REPL . 您也可以在(更新的) Ramda REPL上看到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM