[Javascript Crocks] Create a Maybe with a `safe` Utility Function
2024-08-31 06:15:10
In this lesson, we’ll create a safe
function that gives us a flexible way to create Maybe
s 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);
最新文章
- salesforce 零基础学习(五十三)多个文件生成一个zip文件(使用git上封装的代码)
- CSS Hack解决浏览器IE部分属性兼容性问题
- 缺少动态连接库.so--cannot open shared object file: No such file or directory
- cocos2d-x 3.0 版本 骨骼动画设置shader
- 硬刚Google ,这家小公司的增长团队长啥样
- sehll_if
- mybatis java.lang.UnsupportedOperationException
- 51nod1158 全是1的最大子矩阵
- Java中使用ThreadPoolExecutor并行执行独立的单线程任务
- 《vi中的替换艺术》-linux命令五分钟系列之十一
- java-信息安全(五)-非对称加密算法RSA
- find命令配合sed命令使用
- Nginx的alias的用法及与root的区别
- SCU 4438 Censor(Hash)题解
- poj1988(判断一个结点下面有多少个结点,推荐)
- VS生成事件执行XCOPY时出现Invalid num of parameters的解决方案
- 基于 Laravel 开发博客应用系列 —— 设置 Windows 本地开发环境
- 10 套华丽的 CSS3 按钮推荐
- VS Code 的常用快捷键及插件(前端)
- xshell连接centos虚拟机
热门文章
- 可持久化Treap(fhq Treap,非旋转式Treap)学习(未完待续)
- JavaScript 获取星期几函数
- netty之ByteBuf详解
- 工具分享2:Python 3.6.4、文本编辑器EditPlus、文本编辑器Geany
- CodeForces 680A&;680B&;680C&;680D Round#356
- JS 有趣的eval优化输入验证
- usaco 过路费 Cow Toll Paths, 2009 Dec
- css样式变 及实际用法
- poj 1564 Sum It Up 搜索
- for 循环 乘法口诀表