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的更多相关文章

  1. PHP 笔记一(systax/variables/echo/print/Data Type)

    PHP stands for "Hypertext Preprocessor" ,it is a server scripting language. What Can PHP D ...

  2. JAVA 1.2(原生数据类型 Primitive Data Type)

    1. Java的数据类型分为2类 >> 原生数据类型(primitive data type) >> 引用数据类型(reference data type) 3. 常量和变量 ...

  3. salesforce 零基础开发入门学习(四)多表关联下的SOQL以及表字段Data type详解

    建立好的数据表在数据库中查看有很多方式,本人目前采用以下两种方式查看数据表. 1.采用schema Builder查看表结构以及多表之间的关联关系,可以登录后点击setup在左侧搜索框输入schema ...

  4. 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 ...

  5. XML Data Type Methods(一)

    XML Data Type Methods(一) /*XML Data Type Methods: 1.The query('XQuery') method retrieves(vt.检索,重新得到) ...

  6. include pointers as a primitive data type

    Computer Science An Overview _J. Glenn Brookshear _11th Edition Many modern programming languages in ...

  7. Extended Data Type Properties [AX 2012]

    Extended Data Type Properties [AX 2012] This topic has not yet been rated - Rate this topic Updated: ...

  8. 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 ...

  9. Linux C double linked for any data type

    /************************************************************************** * Linux C double linked ...

随机推荐

  1. K-means (PRML) in C++

    原始数据 #include <iostream>#include <fstream>#include <sstream>#include <vector> ...

  2. c programs

  3. 练习2 及pl/sql

    Rownum 如果不是对主键排序是不会变得 -查询没有学分的学生信息 --SELECT * FROM z_student zs WHERE zs.code NOT IN (SELECT DISTINC ...

  4. php统计网站 / html页面 浏览访问次数程序

    本文章来给大这介绍了php自己写的一些常用的网站统计代码写法,用无数据库的与使用数据库及html静态页面浏览资次数统计代码,大家可进入参考. 实例1 直接使用txt文件进行统计的代码 <?php ...

  5. A - Word

    Problem description Vasya is very upset that many people on the Net mix uppercase and lowercase lett ...

  6. 7.union

    联合结果集union 简单的结果集联合: select number,name,age from emp union select cardnumber,name,age from emp2 基本的原 ...

  7. Nmap linux端口扫描神器

    #简介 Nmap亦称为Network Mapper(网络映射)是一个开源并且通用的用于Linux系统/网络管理员的工具.nmap用于探查网络.执行安全扫描.网络核查并且在远程机器上找出开放端口.它可以 ...

  8. android指纹识别、拼图游戏、仿MIUI长截屏、bilibili最美创意等源码

    Android精选源码 一个动画效果的播放控件,播放,暂停,停止之间的动画 用 RxJava 实现 Android 指纹识别代码 Android仿滴滴打车(滴滴UI)源码 Android高仿哔哩哔哩动 ...

  9. 【JSP】上传图片到数据库中

    第一步:建立数据库 create table test_img(id number(4),name varchar(20),img long raw); 第二步:(NewImg.html) <h ...

  10. JSP_内置对象_请求转发和请求重定向的区别

    请求重定向:客户端行为,response.sendRedirect(),从本质上将等同与两次请求,前一次请求request对象不会保存,地址栏的URL地址会改变. 请求转发:服务器行为,request ...