javascript - 在这种情况下如何作曲

标签 javascript functional-programming ramda.js

var articles = [
  {
    title: 'Everything Sucks',
    author: { name: 'Debbie Downer' }
  },
  {
    title: 'If You Please',
    author: { name: 'Caspar Milquetoast' }
  }
];

var names = _.map(_.compose(_.get('name'), _.get('author'))) 
// returning ['Debbie Downer', 'Caspar Milquetoast']

现在根据上面给定的 articles 和函数 names,创建一个 bool 函数,说明给定的人是否写过任何文章。

isAuthor('New Guy', articles) //false
isAuthor('Debbie Downer', articles)//true

我在下面的尝试

var isAuthor = (name, articles) => {
    return _.compose(_.contains(name), names(articles))
};

但是它在 jsbin 上失败并出现以下错误。也许有人可以尝试解释我的尝试出了什么问题,以便我可以从错误中吸取教训

Uncaught expected false to equal function(n,t){return r.apply(this,arguments)}

最佳答案

Compose 返回一个函数,因此您需要将 articles 传递给该函数。 Compose 会将 articles 传递给 getNames,并将 getNames 的结果传递给 contains(name)(其中还返回一个函数),它将处理作者姓名,并返回 bool 值 result:

const { map, path, compose, contains } = R

const getNames = map(path(['author', 'name']))

const isAuthor = (name) => compose(
  contains(name),
  getNames
)

const articles = [{"title":"Everything Sucks","author":{"name":"Debbie Downer"}},{"title":"If You Please","author":{"name":"Caspar Milquetoast"}}]

console.log(isAuthor('New Guy')(articles)) //false
console.log(isAuthor('Debbie Downer')(articles)) //true
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

关于javascript - 在这种情况下如何作曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55826887/

相关文章:

javascript - 使用 DOMNodeInserted 确定 .class 名称

clojure - 为什么每一个?函数在 Clojure 中用空向量返回 true?

kotlin - 高阶函数组合符号

javascript - Lodash.get 等价于 Ramda

javascript - 使用 ramdajs 将对象转换为对象数组

javascript - 使用 Ramda 对数组的项进行分组和转换

javascript - 将 HTML 值提取到 Javascript 变量中

javascript - 如何动态注入(inject) JavaScript 函数?

javascript - 子 li 上的父 li 样式 :hover

oop - 为什么 Haskell 不需要工厂模式?以及在 Haskell 中,OOP 中的模式解决的需求是如何解决的?