首页 > 其他 > 详细

[Functional Programming] propSatisfies with implies

时间:2019-05-18 00:15:27      阅读:126      评论:0      收藏:0      [点我收藏+]
// implies :: ((a -> Boolean), (a -> Boolean)) -> a -> Boolean
const implies = (p, q) =>
  ifElse(
    p,
    compose(
      Boolean,
      q
    ),
    constant(true)
  );

// hasLEngth :: a -> Boolean
const hasLength = compose(
  Boolean,
  length
);
// isLarge :: a -> Boolean
const isLarge = propSatisfies(flip(gt, 3), "length");
const arrayWithLength = implies(isArray, hasLength);
const isLargeString = implies(isString, isLarge);

/**
 * isValidStringOrArray is week can check array has length
 * or string is large, only for those two types
 * other types, such as number, objet, it return false
 */
const isValidStringOrArray = allPass([
  or(isString, isArray),
  arrayWithLength,
  isLargeString
]);

log(isLargeString(undefined)); // true
log(arrayWithLength(undefined)); // true
log(isValidStringOrArray(undefined)); // false
log(isValidStringOrArray({})); // false
log(isValidStringOrArray([1, 2])); // true
log(isValidStringOrArray("fwe")); // false
log(isValidStringOrArray("fwef")); // true

  

Crocks.js has the implementation, no need to do it yourself.

https://evilsoft.github.io/crocks/docs/functions/logic-functions.html#implies

[Functional Programming] propSatisfies with implies

原文:https://www.cnblogs.com/Answer1215/p/10884176.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!