In this lesson, we’ll create a safe function that gives us a flexible way to create Maybes based on a value and a predicate function that we supply. We’ll verify its behavior with values that satisfy the predicate, and values that do not.

We can write more functional approach, for example write predicate functions:

const isNumber = n => typeof n === 'number' ? Maybe.Just(n) : Maybe.Nothing();
const isString = s => typeof s === 'string' ? Maybe.Just(s) : Maybe.Nothing();

High order function:

const safe = pred => val => pred(val);

const safeNum = safe(isNumber);
const safeStr = safe(isString);

Those functions are useful when we want use in large scale application, because those are composable.

Full code demo:

const {inc, upper} = require('./utils');
const Maybe = require('crocks/Maybe'); const isNumber = n => typeof n === 'number' ? Maybe.Just(n) : Maybe.Nothing();
const isString = s => typeof s === 'string' ? Maybe.Just(s) : Maybe.Nothing();
const safe = pred => val => pred(val); const safeNum = safe(isNumber);
const safeStr = safe(isString); const inputN = safeNum(2); // Just 3 -> 3
const inputS = safeStr('test'); // Just TEST -> TEST
const input = safeStr(undefined); // Nothing -> 0 const result = inputS
.map(upper)
.option(""); console.log(result);

Crocks lib also provides those functions, you actually don't need to write it by yourself.

https://evilsoft.github.io/crocks/docs/functions/predicate-functions.html

const {inc, upper} = require('./utils');
const Maybe = require('crocks/Maybe');
const safe = require('crocks/Maybe/safe');
const { isNumber, isString} = require('crocks/predicates'); /*
const isNumber = n => typeof n === 'number' ? Maybe.Just(n) : Maybe.Nothing();
const isString = s => typeof s === 'string' ? Maybe.Just(s) : Maybe.Nothing();
const safe = pred => val => pred(val);
*/
const safeNum = safe(isNumber);
const safeStr = safe(isString); const inputN = safeNum(2); // Just 3 -> 3
const inputS = safeStr('test'); // Just TEST -> TEST
const input = safeStr(undefined); // Nothing -> 0 const result = inputS
.map(upper)
.option(""); console.log(result);

最新文章

  1. salesforce 零基础学习(五十三)多个文件生成一个zip文件(使用git上封装的代码)
  2. CSS Hack解决浏览器IE部分属性兼容性问题
  3. 缺少动态连接库.so--cannot open shared object file: No such file or directory
  4. cocos2d-x 3.0 版本 骨骼动画设置shader
  5. 硬刚Google ,这家小公司的增长团队长啥样
  6. sehll_if
  7. mybatis java.lang.UnsupportedOperationException
  8. 51nod1158 全是1的最大子矩阵
  9. Java中使用ThreadPoolExecutor并行执行独立的单线程任务
  10. 《vi中的替换艺术》-linux命令五分钟系列之十一
  11. java-信息安全(五)-非对称加密算法RSA
  12. find命令配合sed命令使用
  13. Nginx的alias的用法及与root的区别
  14. SCU 4438 Censor(Hash)题解
  15. poj1988(判断一个结点下面有多少个结点,推荐)
  16. VS生成事件执行XCOPY时出现Invalid num of parameters的解决方案
  17. 基于 Laravel 开发博客应用系列 —— 设置 Windows 本地开发环境
  18. 10 套华丽的 CSS3 按钮推荐
  19. VS Code 的常用快捷键及插件(前端)
  20. xshell连接centos虚拟机

热门文章

  1. 可持久化Treap(fhq Treap,非旋转式Treap)学习(未完待续)
  2. JavaScript 获取星期几函数
  3. netty之ByteBuf详解
  4. 工具分享2:Python 3.6.4、文本编辑器EditPlus、文本编辑器Geany
  5. CodeForces 680A&680B&680C&680D Round#356
  6. JS 有趣的eval优化输入验证
  7. usaco 过路费 Cow Toll Paths, 2009 Dec
  8. css样式变 及实际用法
  9. poj 1564 Sum It Up 搜索
  10. for 循环 乘法口诀表