[Javascript Crocks] Create a Maybe with a `safe` Utility Function
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的更多相关文章
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- [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 ...
- javascript Object.create()究竟发生了什么
这是我在博客园的第一篇博客,早上看了一个大牛的博客,关于javascript继承的,对于大牛使用Object.create()实现继承的方式觉得点问题,就自己研究了一下,所以就有了这篇帖子. 本帖 ...
- [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 ...
- [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 ...
- [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 ...
随机推荐
- js实用篇之数组、字符串常用方法
常常在开发中,会使用到很多js数组和字符串的处理方法,这里列举一些我常用到的一些,方便大家参考使用. 数组方面 push:向数组尾部增加内容,返回的是新数组的长度. var arr = [1,2,3] ...
- c#调用带有自定义表结构的存储过程
1.新建自定义表结构 CREATE TYPE [dbo].[HBForHBGHDR] AS TABLE( [序号] [int] NULL, [客户编号] [varchar](15) NULL ) GO ...
- php 过滤敏感关键词
php 过滤敏感关键词 function badwords($content){ $keywords=M("config")->where("name='badwo ...
- Cracking the Coding Interview 8.7
Given a infinite number of quarters(25cents), dimens(10cents), nickels(5cents) and pennies(1cent), w ...
- css 继承性和层叠性
css有两大特性:继承性和层叠性 继承性 面向对象语言都会存在继承的概念,在面向对象语言中,继承的特点:继承了父类的属性和方法.那么我们现在主要研究css,css就是在设置属性的.不会牵扯到方法的层面 ...
- 上传预览图片的插件jquery-fileupload
上传预览图片的插件jquery-fileupload github地址:https://github.com/blueimp/jQuery-File-Upload 中文文档:http://www.jq ...
- buf.readInt16LE函数详解
offset {Number} 0 noAssert {Boolean} 默认:false 返回:{Number} 从该 Buffer 指定的带有特定尾数格式(readInt16BE() 返回一个较大 ...
- Vue掉坑记
本文章汇总学习过程中掉入和不理解的坑,会持续更新,请保持关注 1.过滤器类 搜索过滤 2.修饰符 修饰符汇总 3.webpack webpack+vuecli打包路径 4.Vue后台管理框架 组件后台 ...
- Java冒泡,快速,插入,选择排序^_^+二分算法查找
这段时间在学Java,期间学到了一些排序和查找方法.特此写来和大家交流,也方便自己的日后查看与复习. 1.下边是Java的主类: public class Get { public static vo ...
- java与安卓中的回调callback学习笔记
1.回调的简单设计如下: package com.listercai.top; public class A { private CallBack callBack; private AnotherC ...