This code rejects everyone under 18, and over 25. Refactor it to be point-free.

https://ramdajs.com/docs/#filter

操作一个list(可以是对象 也可以是数组) 返回新list

Takes a predicate and a Filterable, and returns a new filterable of the same type containing the members of the given filterable which satisfy the given predicate. Filterable objects include plain objects or any object that has a filter method such as Array.
Dispatches to the filter method of the second argument, if present.
Acts as a transducer if a transformer is given in list position.
See also reject, transduce, addIndex.
获取一个谓词和一个Filterable,并返回一个相同类型的新Filterable,其中包含满足给定谓词的给定Filterable的成员。可过滤对象包括普通对象或任何具有过滤方法(如数组)的对象。 发送到第二个参数的筛选器方法(如果存在)。 如果变压器处于列表位置,则充当传感器。 另见拒收、换能、addIndex。

  1. const isEven = n => n % 2 === 0;
  2. R.filter(isEven, [1, 2, 3, 4]); //=> [2, 4]
  3. R.filter(isEven, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}

https://ramdajs.com/docs/#reject filter的补集

接受一个list (对象、数组)处理逻辑关系

Filterable f => (a → Boolean) → f a → f a
Parameters
Added in v0.1.0
The complement of filter.
Acts as a transducer if a transformer is given in list position. Filterable objects include plain objects or any object that has a filter method such as Array.
See also filter, transduce, addIndex.

  1. const isOdd = (n) => n % 2 === 1;
  2. R.reject(isOdd, [1, 2, 3, 4]); //=> [2, 4]
  3. R.reject(isOdd, {a: 1, b: 2, c: 3, d: 4}); //=> {b: 2, d: 4}

https://ramdajs.com/docs/#propSatisfies 属性是否满足逻辑

处理对象的某个值是否符合逻辑

Added in v0.16.0
Returns true if the specified object property satisfies the given predicate; false otherwise. You can test multiple properties with R.where.
See also where, propEq, propIs.

  1. R.propSatisfies(x => x > 0, 'x', {x: 1, y: 2}); //=> true

https://ramdajs.com/docs/#both 逻辑与 会短路返回第一个falsey

处理同时满足两个逻辑
(… → Boolean) → (… → Boolean) → (*… → Boolean)
Parameters
Added in v0.12.0
A function which calls the two provided functions and returns the && of the results. It returns the result of the first function if it is false-y and the result of the second function otherwise. Note that this is short-circuited, meaning that the second function will not be invoked if the first returns a false-y value.
In addition to functions, R.both also accepts any fantasy-land compatible applicative functor.
See also and.
调用提供的两个函数并返回结果的&&的函数。
如果第一个函数的结果为false-y,则返回该函数的结果,否则返回第二个函数的结果。
请注意,这是短路的,这意味着如果第一个函数返回false-y值,则不会调用第二个函数。
除了函数外,R.both还接受任何与fantasy land兼容的应用函子。

幻想地相容应用函子

  1. const gt10 = R.gt(R.__, 10)
  2. const lt20 = R.lt(R.__, 20)
  3. const f = R.both(gt10, lt20);
  4. f(15); //=> true
  5. f(30); //=> false
  6. R.both(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(false)
  7. R.both([false, false, 'a'], [11]); //=> [false, false, 11] ????

https://ramdajs.com/docs/#either 或 任何一个真就真 会短路

(… → Boolean) → (… → Boolean) → (*… → Boolean)
Parameters
Added in v0.12.0
A function wrapping calls to the two functions in an || operation, returning the result of the first function if it is truth-y and the result of the second function otherwise. Note that this is short-circuited, meaning that the second function will not be invoked if the first returns a truth-y value.
In addition to functions, R.either also accepts any fantasy-land compatible applicative functor.
See also or.

  1. const gt10 = x => x > 10;
  2. const even = x => x % 2 === 0;
  3. const f = R.either(gt10, even);
  4. f(101); //=> true
  5. f(8); //=> true
  6. R.either(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(55)
  7. R.either([false, false, 'a'], [11]) // => [11, 11, "a"]

https://ramdajs.com/docs/#and

a → b → a | b
Parameters
Added in v0.1.0
Returns true if both arguments are true; false otherwise.
See also both, xor.

  1. R.and(true, true); //=> true
  2. R.and(true, false); //=> false
  3. R.and(false, true); //=> false
  4. R.and(false, false); //=> false

https://ramdajs.com/docs/#xor 排他

a → b → Boolean
Parameters
Added in v0.27.0
Exclusive disjunction logical operation. Returns true if one of the arguments is truthy and the other is falsy. Otherwise, it returns false.
See also or, and.

  1. R.xor(true, true); //=> false
  2. R.xor(true, false); //=> true
  3. R.xor(false, true); //=> true
  4. R.xor(false, false); //=> false

https://ramdajs.com/docs/#or

a → b → a | b
Parameters
Added in v0.1.0
Returns true if one or both of its arguments are true. Returns false if both arguments are false.
See also either, xor.

  1. R.or(true, true); //=> true
  2. R.or(true, false); //=> true
  3. R.or(false, true); //=> true
  4. R.or(false, false); //=> false

https://ramdajs.com/docs/#gt 只有大于

Ord a => a → a → Boolean
Parameters
Added in v0.1.0
Returns true if the first argument is greater than the second; false otherwise.
See also lt.

Ord a=>a→ A.→ 布尔值
参数 添加到v0.1.0中
如果第一个参数大于第二个参数,则返回true;否则就错了。 另见lt。

  1. R.gt(2, 1); //=> true
  2. R.gt(2, 2); //=> false
  3. R.gt(2, 3); //=> false
  4. R.gt('a', 'z'); //=> false
  5. R.gt('z', 'a'); //=> true

https://ramdajs.com/docs/#gte 大于和等于

Ord a => a → a → Boolean
Parameters
Added in v0.1.0
Returns true if the first argument is greater than or equal to the second; false otherwise.
See also lte.

  1. R.gte(2, 1); //=> true
  2. R.gte(2, 2); //=> true
  3. R.gte(2, 3); //=> false
  4. R.gte('a', 'z'); //=> false
  5. R.gte('z', 'a'); //=> true

https://ramdajs.com/docs/#lt 只有小于

Ord a => a → a → Boolean
Parameters
Added in v0.1.0
Returns true if the first argument is less than the second; false otherwise.
See also gt.

  1. R.lt(2, 1); //=> false
  2. R.lt(2, 2); //=> false
  3. R.lt(2, 3); //=> true
  4. R.lt('a', 'z'); //=> true
  5. R.lt('z', 'a'); //=> false

https://ramdajs.com/docs/#lte 小于和等于

Ord a => a → a → Boolean
Parameters
Added in v0.1.0
Returns true if the first argument is less than or equal to the second; false otherwise.
See also gte.

  1. R.lte(2, 1); //=> false
  2. R.lte(2, 2); //=> true
  3. R.lte(2, 3); //=> true
  4. R.lte('a', 'z'); //=> true
  5. R.lte('z', 'a'); //=> false

遍历list判断属性逻辑

18到25

  1. // import R from 'ramda';
  2. const keepYoungAdults = (people) => people.filter((p) => (
  3. p.age >= 18 && p.age <= 25
  4. ));
  5. [{"age":20},{"age":16},{"age":18},{"age":26},{"age":25},{"age":19}]
  6. [{"age":20},{"age":18},{"age":25},{"age":19}]

不完全的ponitfree 还可以把

  1. x=>x>=18&&x<=25

拆出来

  1. const keepYoungAdults = R.filter(R.propSatisfies(x=>x>=18&&x<=25,"age"))
  1. import { both, filter, gte, lte, propSatisfies } from 'ramda';
  2. const youngAdultAge = both(gte(18), lte(25));
  3. //18到25
  4. const isYoungAdult = propSatisfies(youngAdultAge, 'age');
  5. const keepYoungAdults1 = filter(isYoungAdult);
  1. // You can also apply the opposite logic with reject()
  2. import { either, gt, lt, propSatisfies, reject } from 'ramda';
  3. const notYoungAdultAge = either(gt(18), lt(25)); //小于28或25
  4. const isNotYoungAdult = propSatisfies(notYoungAdultAge, 'age');
  5. const keepYoungAdults2 = reject(isNotYoungAdult);