[Javascript Crocks] Understand the Maybe Data Type
In this lesson, we’ll get started with the Maybe type. We’ll look at the underlying Just and Nothing types and create a simple utility function to create a Maybe from a value. Then we’ll see how we can apply functions to values and skip invocation for values that might cause errors or unexpected results.
Most of time, we will meet this kind of problem in our code:
const {inc} = require('./utils');
const res = inc(); //
const res1 = inc(''); //
const res2 = inc(undefined); // NaN
We expect the function 'inc' can help to increase the value by 1, but if we pass the string type of undefined, we won't get the result we want.
Of course, you can update your inc function like that:
// from
const inc = n => n + ; // to
const inc = n => typeof n === 'number' ? n + : ; module.exports = {
inc
};
But it only works if you have full control of your code, if you are using a thrid-party lib, then it doesn't work.
Install:
npm i -S crocks
By import Maybe from crocks lib:
const Maybe = require('crocks/Maybe');
You two APIs to use:
Maybe.Just() // Just 2
Maybe.Nothing() // Nothing
Update our code:
// utils.js
const inc = n => n + ; module.exports = {
inc
}; // index.js
const {inc} = require('./utils');
const Maybe = require('crocks/Maybe'); const safeNum = n => typeof n === 'number' ? Maybe.Just(n) : Maybe.Nothing();
const input = safeNum(); // Just 3
// const input = safeNum('2'); // Nothing
// const input = safeNum(undefined); // Nothing
const result = input
.map(inc); console.log(result); // Just 3
Now we get 'Just 3' as a result, but we want the actual value and also apply a default value 0:
const safeNum = n => typeof n === 'number' ? Maybe.Just(n) : Maybe.Nothing();
const input = safeNum(2); // 3
// const input = safeNum('2'); // 0
// const input = safeNum(undefined); // const result = input
.map(inc)
.option() // unwrap the value and give a default value; console.log(result);
[Javascript Crocks] Understand the Maybe Data Type的更多相关文章
- PHP 笔记一(systax/variables/echo/print/Data Type)
PHP stands for "Hypertext Preprocessor" ,it is a server scripting language. What Can PHP D ...
- JAVA 1.2(原生数据类型 Primitive Data Type)
1. Java的数据类型分为2类 >> 原生数据类型(primitive data type) >> 引用数据类型(reference data type) 3. 常量和变量 ...
- salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解
建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema Builder查看表结构以及多表之间的关联关系,可以登录后点击setup在左侧搜索框输入schema ...
- The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
刚刚有在程序中,传递一个空值至MS SQL Server数据库,这个值的数据类型为DATETIME执行时,它却发生了如标题提示的异常:The conversion of a varchar data ...
- XML Data Type Methods(一)
XML Data Type Methods(一) /*XML Data Type Methods: 1.The query('XQuery') method retrieves(vt.检索,重新得到) ...
- include pointers as a primitive data type
Computer Science An Overview _J. Glenn Brookshear _11th Edition Many modern programming languages in ...
- Extended Data Type Properties [AX 2012]
Extended Data Type Properties [AX 2012] This topic has not yet been rated - Rate this topic Updated: ...
- org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.String
org.hibernate.id.IdentifierGenerationException: Unknown integral data type for ids : java.lang.Strin ...
- Linux C double linked for any data type
/************************************************************************** * Linux C double linked ...
随机推荐
- Gym - 101981K The 2018 ICPC Asia Nanjing Regional Contest K.Kangaroo Puzzle 暴力或随机
题面 题意:给你1个20*20的格子图,有的是障碍有的是怪,你可以每次指定上下左右的方向,然后所有怪都会向那个方向走, 如果2个怪撞上了,就融合在一起,让你给不超过5w步,让所有怪都融合 题解:我们可 ...
- Python 34(进程了解)
一:僵尸进程与孤儿进程 测试程序: 基本概念: 一个进程使用fork创建子进程,如果子进程退出,而父进程并没有调用wait或waitpid获取子进程的状态信息,那么子进程的进程描述符仍然保存在系统中. ...
- SpringCloud学习 什么是微服务(一)
关于SpringCloud,我是看了周老师的<SpringCloud与Docker微服务架构实战>之后才有了一点了解,做下记录,以供后期学习.本人知识有限,如有不对,欢迎批评 1.什么是单 ...
- c# 官方文档必看
https://docs.microsoft.com/zh-cn/dotnet/csharp/whats-new/csharp-version-history
- JavaScript异步加载方案
(1) defer,只支持IE defer属性的定义和用法(我摘自w3school网站) defer 属性规定是否对脚本执行进行延迟,直到页面加载为止. 有的 javascript 脚本 docume ...
- AFN请求后台返回数据为NSInlineData类型的处理
在利用AFN进行数据解析时出现返回数据为 <7b227374 61747573 223a302c 226d6573 73616765 223a22e6 82a8e79a 84e6898b e69 ...
- css2.0文档查阅及字体样式
css2.0文档查阅下载 网址:http://soft.hao123.com/soft/appid/9517.html <html xmlns="http://www.w3.o ...
- 【Oracle】恢复重做日志组
我们在Oracle的日常运维中,有可能会遇到重做日志组丢失的情况.下面我将模拟丢失不同状态的日志组,并分别给出解决办法: 重做日志有以下几种状态,如下: - CURRENT:此状态表示正在被 LGW ...
- 分布式机器学习框架:CXXNet
caffe是很优秀的dl平台.影响了后面很多相关框架. cxxnet借鉴了很多caffe的思想.相比之下,cxxnet在实现上更加干净,例如依赖很少,通过mshadow的模板化使得gpu ...
- 触发a标签
var aLink = document.createElement("a"); aLink.download = ''; aLink.href = url; if (docume ...