码迷,mamicode.com
首页 > 其他好文 > 详细

[Functional Programming] propSatisfies with implies

时间:2019-05-18 00:45:28      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:isa   obj   AMM   isarray   self   logic   flip   val   const   

// 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

标签:isa   obj   AMM   isarray   self   logic   flip   val   const   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!