使用Ramda映射和过滤对象

Map and filter an object using Ramda

本文关键字:过滤 对象 映射 Ramda 使用      更新时间:2023-09-26

我正在学习Ramda,我有点困惑如何使用Ramda构建下面的lodash链。Ramda为它的操作返回函数,而不是实际值,这似乎是函数式编程的焦点,但是在这个例子中,我有第二个参数localRegex,它不是主要参数。如果不包装Ramda函数并使用.apply().call()将包装的函数参数传播给Ramda函数,似乎不可能完全复制这一点,这似乎比使用lodash更复杂。

var _ = require("lodash")
var R = require("ramda")
var localRegex = /^.'.'/|^.'/|^'//
function getRecursiveDeps(deps, localRegex){
  return _.chain(deps)
    .map(function(dep){
      return dep.source.value
    })
    .filter(function(dep){
      return dep.match(localRegex)
    })
    .value()
}
var items = [
  {
    "source": {
      "value": "./foo"
    }
  },
  {
    "source": {
      "value": "bar"
    }
  }
]
console.log(getRecursiveDeps(items, localRegex))

这是我得到的,它不能工作。

var getRecursiveDeps = R.chain(
  R.map(function(dependency){
    return dependency.source.value
  }),
  R.filter(function(value){
    return value.match(localRegex)
  })
)

是否有办法让Ramda使用一个主变量链接,也传递localRegex ?有没有办法复制getRecursiveDeps,在Ramda中使用lodash ?

关于Ramda的功能和underscorelodash的功能有很多讨论。但在本例中,getRecursiveDeps是一个返回lodash值的函数。当你从lodashunderscore创建这样的函数时,结果是一样的,只是在包装它时需要更多的工作,在这种情况下,使用RamdaLodash有什么好处?

R.chain做的事情与_.chain完全不同。根据当前的文档,它的类型是(a -> [b]) -> [a] -> [b],尽管它的实际类型更为通用。把它想象成一个"平面地图"。函数。

这里实际需要的是R.compose或从左到右的等效R.pipe

如果函数的目标是查找本地依赖项,那么在函数中嵌入模式对我来说似乎是合适的。我将这样写:

// getLocalDeps :: [{ source :: { value :: String }}] -> [String]
const getLocalDeps =
R.pipe(R.map(R.path(['source', 'value'])),
       R.filter(R.test(/^[.]{0,2}[/]/)));
getLocalDeps(items);  // => ['./foo']

我有点困惑的名称getRecursiveDeps作为函数不是递归的。getLocalDeps似乎更合适。


如果你想参数化模式,我建议将getLocalDeps分解成更小的部分:

// isLocal :: String -> Boolean
const isLocal = R.test(/^[.]{0,2}[/]/);
// getDeps :: [{ source :: { value :: String }}] -> [String]
const getDeps = R.map(R.path(['source', 'value']));
// getLocalDeps :: [{ source :: { value :: String }}] -> [String]
const getLocalDeps = R.pipe(getDeps, R.filter(isLocal));

然后,您可以根据这些构建块定义其他函数:

// getNonLocalDeps :: [{ source :: { value :: String }}] -> [String]
const getNonLocalDeps = R.pipe(getDeps, R.reject(isLocal));
// getLocalJsonDeps :: [{ source :: { value :: String }}] -> [String]
const getLocalJsonDeps = R.pipe(getLocalDeps, R.filter(R.test(/[.]json$/)));

另一种方法是这样做的,无点且保留现有的API:

// getLocalDeps :: [{ source :: { value :: String }}] -> RegExp -> [String]
const getLocalDeps =  R.useWith(
  R.flip(R.call),
  R.map(R.path(['source', 'value'])),
  R.pipe(R.unary(R.test), R.filter)
);
localDeps(items, localRegex); //=> ["./foo"]

函数的最后一行对我来说感觉有点不太好,这个问题导致我打开了一个关于恢复对库的一些最近更改的问题。有几个变体可以使用:

// ...
R.pipe(regex => item => R.test(regex, item), R.filter)
//...

// ...
regex => R.filter(R.test(regex))
//...

但是在最近对Ramda进行更改之前,它应该是简单的

// ...
R.pipe(R.test, R.filter)
//...

然而,有一件事是,Ramda努力保持参数的逻辑顺序:那些不太可能改变的出现在那些更可能改变的之前。考虑到这一点,我更喜欢这样:

// getLocalDeps :: RegExp -> [{ source :: { value :: String }}] -> [String]
var getLocalDeps2 =  R.useWith(
  R.call,
  R.pipe(R.unary(R.test), R.filter),
  R.map(R.path(['source', 'value']))
);
localDeps2(localRegex, items); //=> ["./foo"]

感觉更干净了。此外,它允许您预定义函数并单独使用它:

myDeps = localDeps2(localRegex);
myDeps(items); //=> ["./foo"]