ramda 函数 logic

原文链接: ramda 函数 logic

上一篇: ramda 函数 String

下一篇: Functor(函子)、Monad(单子)、Applicative

逻辑操作

up-017775e207fbeb0451ba746afd8c2fe3cc3.png

const R = require('ramda')
console.log('and 逻辑与')
R.and(true, true); //=> true
R.and(true, false); //=> false
R.and(false, true); //=> false
R.and(false, false); //=> false

console.log('or 逻辑或')
R.or(true, true); //=> true
R.or(true, false); //=> true
R.or(false, true); //=> true
R.or(false, false); //=> false

console.log('not 逻辑非')
// 逻辑非运算。 当传入参数为 false-y 值时,返回 true;truth-y 值时,返回 false。
R.not(true); //=> false
R.not(false); //=> true
R.not(0); //=> true
R.not(1); //=> false

console.log('allPass 多条件and')
// 传入包含多个 predicate 的列表,返回一个 predicate:如果给定的参数满足列表中的所有 predicate ,则返回 true。
// 该函数返回一个柯里化的函数,参数个数由列表中参数最多的 predicate 决定。
const isQueen = R.propEq('rank', 'Q');
const isSpade = R.propEq('suit', 'S');
const isQueenOfSpades = R.allPass([isQueen, isSpade]);
console.log(
  isQueenOfSpades({rank: 'Q', suit: ' '}), //=> false
  isQueenOfSpades({rank: 'Q', suit: 'S'}), //=> true
)

console.log('anyPass 多条件or')
// 传入包含多个 predicate 的列表,返回一个 predicate:只要给定的参数满足列表中的一个 predicate ,就返回 true。
// 该函数返回一个柯里化的函数,参数个数由列表中参数最多的 predicate 决定。
const isClub = R.propEq('suit', 'C');
const isBlackCard = R.anyPass([isClub, isSpade]);
console.log(
  isBlackCard({rank: '10', suit: 'C'}), //=> true
  isBlackCard({rank: 'Q', suit: 'S'}), //=> true
  isBlackCard({rank: 'Q', suit: 'B'}), //=> false
)

console.log('both 短路and')
// 该函数调用两个函数,并对两函数返回值进行与操作。若第一个函数结果为 false-y 值 (false, null, 0 等),则返回该结果,否则返回第二个函数的结果。注意,both 为短路操作,即如果第一个函数返回 false-y 值,则不会调用第二个函数。
const gt10 = R.gt(R.__, 10)
const lt20 = R.lt(R.__, 20)
const f = R.both(gt10, lt20);
console.log(
  f(15), //=> true
  f(30), //=> false
  // R.both(Maybe.Just(false), Maybe.Just(55)), // => Maybe.Just(false)
  R.both([false, false, 'a'], [11]), //=> [false, false, 11]
  R.both([true, false], [false]), //=> [false, false]
  R.both([true, false], [true]), //=> [true, false]
  R.both([], [11]), //=> []
  // R.both(false, [11]), //=>  报错 fn is not a function
  R.both(R.F, [11])(), //=> false
  R.both(() => false, () => "abc")(), //=> false
  R.both(() => true, () => false)(), //=> false

)

console.log('either 短路或')
// 返回由 || 运算符连接的两个函数的包装函数。如果两个函数中任一函数的执行结果为 truth-y,则返回其执行结果。 注意,这个是短路表达式,
// 意味着如果第一个函数返回 truth-y 值的话,第二个函数将不会执行。
const gt11 = x => x > 11
const even = x => x % 2 === 0;
const ff = R.either(gt10, even);
console.log(
  ff(101), //=> true
  ff(8), //=> true
)

// R.either(Maybe.Just(false), Maybe.Just(55)); // => Maybe.Just(55)
console.log(
  R.either([false, false, 'a'], [11]), // => [11, 11, "a"]
  R.either([true, false, true], [null]), // => [ true, null, true ]
)

console.log('complement 取反')
// 对函数的返回值取反。接受一个函数 f,返回一个新函数 g:在输入参数相同的情况下,若 f 返回 'true-y' ,则 g 返回 false-y ,反之亦然。
const isNotNil = R.complement(R.isNil);
console.log(
  R.isNil(null), //=> true
  isNotNil(null), //=> false
  R.isNil(7), //=> false
  isNotNil(7), //=> true
)

console.log('cond 条件选择')
// 返回一个封装了 if / else,if / else, ... 逻辑的函数 fn。
// R.cond 接受列表元素为 [predicate,transformer] 的列表。
// fn 的所有参数顺次作用于每个 predicate,直到有一个返回 "truthy" 值,
// 此时相应 transformer 对参数处理,并作为 fn 的结果返回。
// 如果没有 predicate 匹配,则 fn 返回 undefined。
const fn = R.cond([
  [R.equals(0), R.always('water freezes at 0°C')],
  [R.equals(100), R.always('water boils at 100°C')],
  // R.T 恒定返回 true 的函数。忽略所有的输入参数。
  [R.T, temp => 'nothing special happens at ' + temp + '°C']
]);
console.log(
  fn(0), //=> 'water freezes at 0°C'
  fn(50), //=> 'nothing special happens at 50°C'
  fn(100), //=> 'water boils at 100°C'
)

console.log('defaultTo 默认值')
// 如果第二个参数不是 null、undefined 或 NaN,
// 则返回第二个参数,否则返回第一个参数(默认值)。
const defaultTo42 = R.defaultTo(42);

defaultTo42(null);  //=> 42
defaultTo42(undefined);  //=> 42
defaultTo42(false);  //=> false
defaultTo42('Ramda');  //=> 'Ramda'
// parseInt('string') results in NaN
defaultTo42(parseInt('string')); //=> 42


console.log('ifElse if-else包装')
// 根据 condition predicate 的返回值调用 onTrue 或 onFalse 函数。
const incCount = R.ifElse(
  R.has('count'),
  R.over(R.lensProp('count'), R.inc),
  R.assoc('count', 1)
);
incCount({});           //=> { count: 1 }
incCount({count: 1}); //=> { count: 2 }

console.log('isEmpty 空值检测')
// 检测给定值是否为其所属类型的空值,若是则返回 true ;否则返回 false 。
R.isEmpty([1, 2, 3]);   //=> false
R.isEmpty([]);          //=> true
R.isEmpty('');          //=> true
R.isEmpty(null);        //=> false
R.isEmpty({});          //=> true
R.isEmpty({length: 0}); //=> false

console.log('pathSatisfies 路径检测')
// 如果对象的给定路径上的属性满足 predicate,返回 ture;否则返回 false。
console.log(
  R.pathSatisfies(y => y > 0, ['x', 'y'], {x: {y: 2}}), //=> true
  R.pathSatisfies(y => y > 0, ['x', 'y'], {x: {y: -2}}), //=> false
  // [] 表示根, 即传入的数据
  R.pathSatisfies(R.is(Object), [], {x: {y: 2}}), //=> true
)

// 如果指定的对象属性满足 predicate,返回 true;否则返回 false。可以使用 R.where 进行多个属性的判断。
console.log('propSatisfies 属性条件检测')
const gtZero = x => {
  console.log('gtZero', x)
  return x > 0
}
console.log(
  R.propSatisfies(gtZero, 'x', {x: 1, y: 2}), //=>  gtZero 1 true
  R.propSatisfies(gtZero, '', {x: 1, y: 2}), //=>  gtZero undefined false
  R.propSatisfies(gtZero, 'xyz.abc', {x: 1, y: 2}), //=>  gtZero undefined false
  R.propSatisfies(R.isNil, 'xyz', {x: 1, y: 2}), //=> true
)

// 判断输入值是否满足 predicate,
// 若不符合,则将输入值传给 whenFalseFn 处理,并将处理结果作为返回;
// 若符合,则将输入值原样返回。
console.log('unless 和when相反')
let safeInc = R.unless(R.isNil, R.inc);
safeInc(null); //=> null
safeInc(1); //=> 2

// 接受一个 predicate ,transform function 和 初始值,返回一个与初始值相同类型的值。
// 对输入值进行 transform ,直到 transform 的结果满足 predicate,
// 此时返回这个满足 predicate 的值。
console.log('until 对while的包装')
R.until(R.gt(R.__, 100), R.multiply(2))(1) // => 128

// 判断输入值是否满足 predicate,
// 若符合,则将输入值传给 whenTrueFn 处理,并将处理结果作为返回;
// 若不符合,则将输入值原样返回。
// truncate :: String -> String
console.log('when 和unless相反')
let safeDec = R.when(R.isNil, R.dec);
console.log(
  safeDec(null), //=> -1
  safeDec(1), //=> 1
  R.dec(null), // -1
  R.dec(false) // -1
)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值