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. 协同过滤算法中皮尔逊相关系数的计算 C++

    template <class T1, class T2>double Pearson(std::vector<T1> &inst1, std::vector<T ...

  2. Java 发送短信

    这是一个调用sms接口发短信的程序,支持同时发送的短信量并不是很大,只作为学习使用(当然如果你想内部使用也行) 源码:package com; import org.apache.commons.ht ...

  3. 我的MYSQL学习心得(推荐)

    http://www.cnblogs.com/lyhabc/category/573945.html

  4. C#之经理评分系统

    PM类,几乎全是属性 using System; using System.Collections.Generic; using System.Linq; using System.Text; usi ...

  5. JQuery 一些特殊符号的使用

    前言:我写博客的频率与我的清闲程度成正比..   太闲了所以想记录一下JQuery里的特殊符号,级别:入门级.用到哪里写到哪里,不全面是肯定的. 其实只要接触前端就肯定少不了用jquery,但是以前太 ...

  6. 【java基础】(6)内部类

    内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动) 显然, ...

  7. CSS画各种二维图形

    1.效果 2.源码 <%@ page contentType="text/html;charset=UTF-8" language="java" %> ...

  8. Intellij IDEA 2018.3.5版安装详解及破解

    几个参考链接: 软件下载链接:https://www.jetbrains.com/idea/ 破解补丁:链接:https://pan.baidu.com/s/1xUbil5jq_DyTbXJWUUsM ...

  9. EKF优化:协方差coff计算公式、意义、Code优化

    复习!复习! 原文链接:http://blog.csdn.net/goodshot/article/details/8611178 1.代码: Matlab相关系数的意义: Eigen::Matrix ...

  10. Gartner2017年数据科学领域最酷供应商出炉,实至名归

    文 | 帆软数据应用研究院 水手哥 更多大数据资讯和企业案例可关注 :知乎专栏<帆软数据应用研究院> 近日,Gartner公布了2017年度数据科学和机器学习领域的最酷供应商,清一色的美国 ...