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){return r.apply(this,arguments)}
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>