gpt4 book ai didi

javascript - 如何一起提升和组合功能?

转载 作者:行者123 更新时间:2023-11-30 14:01:48 25 4
gpt4 key购买 nike

我有这个结构中的人员列表:

const people = [
{name: 'jenny', friends: ['jeff']},
{name: 'frank', friends: ['jeff', 'ross']},
{name: 'sarah', friends: []},
{name: 'jeff', friends: ['jenny', 'frank']},
{name: 'russ', friends: []},
{name: 'calvin', friends: []},
{name: 'ross', friends: ['frank']},
];

我想通过两种方式过滤掉人:有 friend 和没有 friend ;而且我想要Predicate Array.filterlifted ,像这样:

const peopleWithoutFriends = people.filter(withoutFriends);
console.log(peopleWithoutFriends);

const peopleWithFriends = people.filter(withFriends);
console.log(peopleWithFriends);

我可以通过如下显式编写 by 函数来实现此行为:

const by = x => i => {
return Boolean(get(i, x));
};
const withFriends = by('friends.length');
const peopleWithFriends = people.filter(withFriends);
console.log(peopleWithFriends);

问题:如果我想要反转,我需要明确地为 peopleWithoutFriends

编写一个全新的函数
const notBy = x => i => {
return !Boolean(get(i, x));
};

const withOutFriends = notBy('friends.length');
const peopleWithoutFriends = people.filter(withOutFriends);

我不想将我的 by 函数写两次。我宁愿将较小的函数组合在一起。

问题:

我如何编写和使用像这样的小函数:flow Boolean get curry not并为我的 Array.filter 在 people 列表上编写 withFriendswithOutFriends 谓词。

回复:https://repl.it/@matthewharwood/ChiefWelloffPaintprogram

const {flow, get, curry} = require('lodash');

const people = [
{name: 'jenny', friends: ['jeff']},
{name: 'frank', friends: ['jeff', 'ross']},
{name: 'sarah', friends: []},
{name: 'jeff', friends: ['jenny', 'frank']},
{name: 'russ', friends: []},
{name: 'calvin', friends: []},
{name: 'ross', friends: ['frank']},
];
const not = i => !i;

const withFriends = i => flow(
Boolean,
get(i, 'friends.length'), // arity of this is 2 so might be harder to lift, is it possible tho with curry?
); // No idea what i'm doing here.


const peopleWithFriends = people.filter(withFriends);
console.log(peopleWithFriends);

const withoutFriends = flow(not, withFriends);
const peopleWithoutFriends = people.filter(withoutFriends);
console.log(peopleWithoutFriends);

最佳答案

因为有/没有 friend 函数的结果是一个 bool 值,你可以取反(或补充)一个的结果来得到另一个。此外,函数的元数为 1(它们所操作的对象)。

Lodash/fp:

const { flow, get, isEmpty, negate } = _;

const people = [
{name: 'jenny', friends: ['jeff']},
{name: 'frank', friends: ['jeff', 'ross']},
{name: 'sarah', friends: []},
{name: 'jeff', friends: ['jenny', 'frank']},
{name: 'russ', friends: []},
{name: 'calvin', friends: []},
{name: 'ross', friends: ['frank']},
];

const withoutFriends = flow(get('friends'), isEmpty); // create a function that gets the friends array, and check if it is empty
const withFriends = negate(withoutFriends); // negate the result of withoutFriends

const peopleWithFriends = people.filter(withFriends);
console.log(peopleWithFriends);

const peopleWithoutFriends = people.filter(withoutFriends);
console.log(peopleWithoutFriends);
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>

lambda :

const { pipe, prop, isEmpty, complement } = R;

const people = [
{name: 'jenny', friends: ['jeff']},
{name: 'frank', friends: ['jeff', 'ross']},
{name: 'sarah', friends: []},
{name: 'jeff', friends: ['jenny', 'frank']},
{name: 'russ', friends: []},
{name: 'calvin', friends: []},
{name: 'ross', friends: ['frank']},
];

const withoutFriends = pipe(prop('friends'), isEmpty); // create a function that gets the friends array, and check if it is empty
const withFriends = complement(withoutFriends); // negate the result of withoutFriends

const peopleWithFriends = people.filter(withFriends);
console.log(peopleWithFriends);

const peopleWithoutFriends = people.filter(withoutFriends);
console.log(peopleWithoutFriends);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

注意事项:

  1. _.flow()R.pipe 都从左到右(从上到下)执行序列。函数 _.compose()R.compose 顺序颠倒。
  2. flow/pipe/compose 中的第一个函数获取传递给组合函数的所有内容。序列中的其他函数总是获得一个参数(前一个函数的结果)/。
  3. Ramda 和 Lodash 都有一个 reject 方法,与 filter 相反,如果 predicate 返回 true,则该项被移除。例如,R.reject(foo, xs) 等同于 R.filter(R.complement(foo), xs)。 (@ScottSauyet 在此 comment 中指出)

关于javascript - 如何一起提升和组合功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56202076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com