This code tells you if someone should consider a tech career. Refactor it to be point-free.

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

处理逻辑

(… → Boolean) → (… → ) → (… → ) → (… → *)
Parameters
Added in v0.8.0
Creates a function that will process either the onTrue or the onFalse function depending upon the result of the condition predicate.
See also unless, when, cond.

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

处理对象

whereObject

{String: ( → Boolean)} → {String: } → Boolean
Parameters
Added in v0.1.1
Takes a spec object and a test object; returns true if the test satisfies the spec. Each of the spec’s own properties must be a predicate function. Each predicate is applied to the value of the corresponding property of the test object. where returns true if all the predicates return true, false otherwise.
where is well suited to declaratively expressing constraints for other functions such as filter and find.
See also propSatisfies, whereEq.

  1. // pred :: Object -> Boolean
  2. const pred = R.where({
  3. a: R.equals('foo'),
  4. b: R.complement(R.equals('bar')),
  5. x: R.gt(R.__, 10),
  6. y: R.lt(R.__, 20)
  7. });
  8. pred({a: 'foo', b: 'xxx', x: 11, y: 19}); //=> true
  9. pred({a: 'xxx', b: 'xxx', x: 11, y: 19}); //=> false
  10. pred({a: 'foo', b: 'bar', x: 11, y: 19}); //=> false
  11. pred({a: 'foo', b: 'xxx', x: 10, y: 19}); //=> false
  12. pred({a: 'foo', b: 'xxx', x: 11, y: 20}); //=> false

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

处理关系

equalsRelation

a → b → Boolean
Parameters
Added in v0.15.0
Returns true if its arguments are equivalent, false otherwise. Handles cyclical data structures.
如果参数相等,则返回true,否则返回false。处理循环数据结构。
Dispatches symmetrically to the equals methods of both arguments, if present.
对称地分派到两个参数的equals方法(如果存在)。

  1. R.equals(1, 1); //=> true
  2. R.equals(1, '1'); //=> false
  3. R.equals([1, 2, 3], [1, 2, 3]); //=> true
  4. const a = {}; a.v = a;
  5. const b = {}; b.v = b;
  6. .equals(a, b); //=> true

三元+条件判断

This code tells you if someone should consider a tech career. Refactor it to be point-free.

  1. import R from 'ramda';
  2. const shouldCode = (person) => (
  3. person.lovesTech && person.worksHard ?
  4. `${person.name} may enjoy a tech career!` :
  5. `${person.name} wouldn't enjoy a tech career.`
  6. );
  1. const enjoy = person => `${person.name} may enjoy a tech career!`
  2. const notEnjoy = person => `${person.name} wouldn't enjoy a tech career.`
  3. const isEnjoy = R.where({
  4. lovesTech:R.equals(true),
  5. worksHard:R.equals(true)
  6. })
  7. const shouldCode = R.ifElse(isEnjoy,enjoy,notEnjoy)
  1. import { equals, ifElse, where } from 'ramda';
  2. // Even though the two inner functions show a person
  3. // parameter, shouldCode() itself is still point-free.
  4. // Each function's independently judged from the others.
  5. const shouldCode = ifElse(
  6. where({
  7. lovesTech: equals(true),
  8. worksHard: equals(true),
  9. }),
  10. (person) => `${person.name} may enjoy a tech career!`,
  11. (person) => `${person.name} wouldn't enjoy a tech career.`
  12. );