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.
// pred :: Object -> Boolean
const pred = R.where({
a: R.equals('foo'),
b: R.complement(R.equals('bar')),
x: R.gt(R.__, 10),
y: R.lt(R.__, 20)
});
pred({a: 'foo', b: 'xxx', x: 11, y: 19}); //=> true
pred({a: 'xxx', b: 'xxx', x: 11, y: 19}); //=> false
pred({a: 'foo', b: 'bar', x: 11, y: 19}); //=> false
pred({a: 'foo', b: 'xxx', x: 10, y: 19}); //=> false
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方法(如果存在)。
R.equals(1, 1); //=> true
R.equals(1, '1'); //=> false
R.equals([1, 2, 3], [1, 2, 3]); //=> true
const a = {}; a.v = a;
const b = {}; b.v = b;
.equals(a, b); //=> true
三元+条件判断
This code tells you if someone should consider a tech career. Refactor it to be point-free.
import R from 'ramda';
const shouldCode = (person) => (
person.lovesTech && person.worksHard ?
`${person.name} may enjoy a tech career!` :
`${person.name} wouldn't enjoy a tech career.`
);
const enjoy = person => `${person.name} may enjoy a tech career!`
const notEnjoy = person => `${person.name} wouldn't enjoy a tech career.`
const isEnjoy = R.where({
lovesTech:R.equals(true),
worksHard:R.equals(true)
})
const shouldCode = R.ifElse(isEnjoy,enjoy,notEnjoy)
import { equals, ifElse, where } from 'ramda';
// Even though the two inner functions show a person
// parameter, shouldCode() itself is still point-free.
// Each function's independently judged from the others.
const shouldCode = ifElse(
where({
lovesTech: equals(true),
worksHard: equals(true),
}),
(person) => `${person.name} may enjoy a tech career!`,
(person) => `${person.name} wouldn't enjoy a tech career.`
);