繁体   English   中英

使用 Ramda 映射和过滤对象

[英]Map and filter an object using Ramda

我正在学习Ramda和我有点困惑如何构建这个lodash下面使用链Ramda 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使用lodashRamda

有一个关于如何谈了很多Ramda是功能齐全, underscorelodash都没有。 但在这种情况下, getRecursiveDeps是一个从lodash返回值的lodash 当您从创建一个这样的功能lodashunderscore的结果是一样的,当涉及到包装它,在这种情况下会怎样使用是振作但只是更多的工作RamdaLodash

R.chain做了一些与_.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"]

这是 Ramda 的一个很好的部分。

暂无
暂无

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

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