[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 ...
随机推荐
- openStack 主机流量计运行状态 随笔记录
root@ruiy-controller:~# ifconfigeth0 Link encap:Ethernet HWaddr 0c:c4:7a:0d:97:2c ine ...
- [Apple开发者帐户帮助]三、创建证书(5)创建WatchKit服务证书
WatchKit服务证书允许您使用Apple推送通知(APN)将更新推送到Apple Watch上的复杂功能. 所需角色:帐户持有人或管理员. 在证书,标识符和配置文件中,从左侧的弹出菜单中选择iOS ...
- CRMEB系统就是集客户关系管理+营销电商系统,能够真正帮助企业基于微信公众号、小程序实现会员管理、数据分析,精准营销的电子商务管理系统。可满足企业新零售、批发、分销、等各种业务需求。
**可以快速二次开发的开源小程序商城系统源码**源码开源地址:https://github.crmeb.net/u/LXT 项目介绍: CRMEB系统就是集客户关系管理+营销电商系统,能够真正帮助企业 ...
- BZOJ 1511 KMP
题意:求出每个前缀的最长周期之和(等于本身的算0) 思路: 求出来next数组 建出next树 找到不为0的最小的 n减去它就是答案 //By SiriusRen #include <cstd ...
- 【java基础】(1)Java的权限修饰符(public,protected,default,private)
访问权限修饰符权限从高到低排列是public ,protected ,default, private. 一.根据“是否是同包”.“是否是子类”分为4中情况+本类 5种情况 二.把 同包中的子类 ...
- usaco 过路费 Cow Toll Paths, 2009 Dec
Description 翰家有 N 片草地,编号为 1 到 N ,彼此之间由 M 条双向道路连接,第 i 条道路连接了 Ai 和Bi,两片草地之间可能有多条道路,但没有道路会连接同一片草地,现有的道路 ...
- Maven项目pom.xml配置详解
maven项目pom.xml文件配置详解,需要时可以用作参考: <project xmlns="http://maven.apache.org/POM/4.0.0" xmln ...
- 查找索引/ie滤镜/动态背景/属性attr和prop
1. 查找索引 查找当前元素在指定范围内的索引序号,示例: $('.right_newestState_con').find('em').index($(this)); 2. ie滤镜 利用ie的私有 ...
- 解决Cannot change version of project facet Dynamic Web M 3.0
解决Cannot change version of project facet Dynamic Web M 3.0 dynamic web module 版本之间的区别: Servlet 3.0 D ...
- Everything is a file
"Everything is a file" describes one of the defining features of Unix, and its derivatives ...