In this lesson, we’ll create a safe function that gives us a flexible way to create Maybes 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);

[Javascript Crocks] Create a Maybe with a `safe` Utility Function的更多相关文章

  1. [Javascript Crocks] Compose Functions for Reusability with the Maybe Type

    We can dot-chain our way to great success with an instance of Maybe, but there are probably cases wh ...

  2. [Javascript Crocks] Safely Access Object Properties with `prop`

    In this lesson, we’ll use a Maybe to safely operate on properties of an object that could be undefin ...

  3. [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 ...

  4. [Javascript Crocks] Safely Access Nested Object Properties with `propPath`

    In this lesson, we’ll look at the propPath utility function. We’ll ask for a property multiple level ...

  5. [Javascript Crocks] Apply a function in a Maybe context to Maybe inputs (curry & ap & liftA2)

    Functions are first class in JavaScript. This means we can treat a function like any other data. Thi ...

  6. javascript Object.create()究竟发生了什么

      这是我在博客园的第一篇博客,早上看了一个大牛的博客,关于javascript继承的,对于大牛使用Object.create()实现继承的方式觉得点问题,就自己研究了一下,所以就有了这篇帖子. 本帖 ...

  7. [Javascript Crocks] Make your own functions safer by lifting them into a Maybe context

    You probably have functions that aren’t equipped to accept a Maybe as an argument. And in most cases ...

  8. [Javascript Crocks] Recover from a Nothing with the `coalesce` Method

    The alt method allows us to recover from a nothing with a default Maybe, but sometimes our recovery ...

  9. [Javascript Crocks] Recover from a Nothing with the `alt` method

    Once we’re using Maybes throughout our code, it stands to reason that at some point we’ll get a Mayb ...

随机推荐

  1. Dirichlet's Theorem on Arithmetic Progressions

    http://poj.org/problem?id=3006 #include<stdio.h> #include<math.h> int is_prime(int n) { ...

  2. SiteMesh3使用实例和详解

    一.SiteMesh介绍 SiteMesh是一个网页布局和修饰的框架,利用它可以将网页的内容和页面结构分离,以达到页面结构共享的目的.[来自百度百科] 通俗的理解就是,SiteMesh把页面中变化的和 ...

  3. 遍历WPF DataGrid单元格

    using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; ...

  4. SLAM:飞行机器人的参数解析-分类

    在水电站存在的山区,公路运输效率极低,盘山公路绕行消耗大量时间,使用飞行机器人进行运输是合适的选择. 实现一位长辈在山区飞行的愿望,任重而道远 常见飞行机器人的参数解析:解读飞行机器人的基本类型及技术 ...

  5. NOPI读取Word模板并保存

    安装NPOI 可以在 程序包管理器控制台中输入 PM> Install-Package NPOI 会下载最新版本NPOI ----------------------------引用了NPOI- ...

  6. nim游戏解法(转)

    转自:http://acm.hdu.edu.cn/forum/read.php?fid=9&tid=10617 取火柴的游戏 题目1:今有若干堆火柴,两人依次从中拿取,规定每次只能从一堆中取若 ...

  7. sqlitManager

    @interface sqlitManager : NSObject +(instancetype)sharedSqlitManager; -(void)createDB; -(void)create ...

  8. 为什么使用dispatch_sync

    1.充分利用多线程的性能: 2.将分散在多线程中的核心操作归并到一个queue执行(通常为一个串行队列). 3.便于在任务线程中进行后继操作. 术语: 任务线程: 同步模块.

  9. jstl与el结合常见用法

    JSTL Functions标签库 在JSP文件中使用Functions标签库,要先通过taglib指令引入该标签库: <%@taglib uri=”http://java.sun.com/js ...

  10. 连接(JOIN)运算

    内连接--INNER JOIN 此处用商品表(product)和商店商品表(ShopProduct)测试,外键:product_id select sp.shop_id, sp.shop_name, ...