admin管理员组文章数量:1331849
I'm trying to refactor this function to be pointfree.
function siblings(me) {
return R.pipe(family, R.reject(equalsMe(me)))(me);
}
I'd like to pass me
to a function down the pipe along with the value that family
returns.
Tried a few things with R.useWith
or R.converge
with R.identity
or R.__
(not even sure if I should be using that) but found nothing to work.
I'm trying to refactor this function to be pointfree.
function siblings(me) {
return R.pipe(family, R.reject(equalsMe(me)))(me);
}
I'd like to pass me
to a function down the pipe along with the value that family
returns.
Tried a few things with R.useWith
or R.converge
with R.identity
or R.__
(not even sure if I should be using that) but found nothing to work.
3 Answers
Reset to default 7I'd also suggest using R.converge
and swapping R.reject(equalsMe(me))
with R.without(me)
const withFamily = R.always(['fratello', 'sorella', 'io']);
const siblingsOf = R.converge(R.without, [
R.identity,
withFamily,
]);
console.log(
siblingsOf('io'),
);
console.log(
siblingsOf('sorella'),
);
<script src="https://cdnjs.cloudflare./ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>
If I understand correctly family
is a function that takes a person and returns a list of family members (including that person) e.g.
family(2);
//=> [1, 2, 3]
Then you want to create a function siblings
which takes a person and returns only their siblings e.g.
siblings(2);
//=> [1, 3]
Personally I think your function would read slightly better if it was written this way:
const siblings = me => reject(equals(me), family(me));
siblings(2);
//=> [1, 3]
If you really wanted a pointfree version of it, you could use converge
but I really don't think it is any better:
const siblings = converge(reject, [unary(equals), family]);
If you're using R.without (as suggested in Hitmands answer), you can use R.chain with a flipped R.without:
chain(flip(without), withFamily);
Because chain(f, g)(x)
is equivalent to f(g(x), x)
:
chain(flip(without), withFamily)(me)
equals to:
flippedWithout(withFamily(me), me)
Example:
const { always, chain, flip, without } = R;
const withFamily = always(['fratello', 'sorella', 'io']);
const siblingsOf = chain(flip(without), withFamily);
console.log(
siblingsOf('io'),
);
console.log(
siblingsOf('sorella'),
);
<script src="https://cdnjs.cloudflare./ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>
本文标签: javascriptPointfree dynamic function compositionStack Overflow
版权声明:本文标题:javascript - Pointfree dynamic function composition - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742235270a2437963.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论