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
,创建一个布尔函数,该函数说明给定的人是否写了任何文章。
isAuthor('New Guy', articles) //false
isAuthor('Debbie Downer', articles)//true
我在下面的尝试
var isAuthor = (name, articles) => {
return _.compose(_.contains(name), names(articles))
};
但是它在jsbin
上失败,并显示以下错误。也许有人可以尝试解释我的尝试出了什么问题,以便我可以从错误中学习
未捕获的期望假等于函数(n,t){返回r.apply(this,arguments)}
答案 0 :(得分:1)
Compose返回一个函数,因此您需要将articles
传递给该函数。 Compose会将articles
传递给getNames
,并将getNames
的结果传递给contains(name)
(还会返回一个函数),该结果将处理作者姓名,并返回布尔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>