最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pointfree dynamic function composition - Stack Overflow

programmeradmin6浏览0评论

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.

Share Improve this question edited Apr 22, 2021 at 8:27 custommander 19k6 gold badges68 silver badges93 bronze badges asked Apr 22, 2021 at 2:03 AKGAKG 3,3166 gold badges30 silver badges38 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

I'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>

发布评论

评论列表(0)

  1. 暂无评论