[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 ...
随机推荐
- El表达式日期处理
第1步:引入指令 <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt " %&g ...
- python 关于文件操作的一些理解
在用python进行数据处理编程中,往往涉及到文件IO口读写,IO口的读写性能会极大的影响程序的运行时间.在进行文件写入时,一般会存在两种情况.第一种是数据到来马上进行数据写入,即来一条写一条,第二种 ...
- wcf 错误:无法加载或初始化请求的服务提供程序
解决办法:cmd netsh winsock reset 恢复网络编程接口
- 自学Python七 爬虫实战一
此文承接上文,让我们写一个简简单单的爬虫,循序而渐进不是吗?此次进行的练习是爬取前5页什么值得买网站中的白菜价包邮信息.包括名称,价格,推荐人,时间. 我们所需要做的工作:1.确定URL并获得页面代码 ...
- 【Oracle】rollup函数
当我们在做报表统计的时候,很多时候需要用到‘合计’这个功能,比如我们想得到如下格式的报表: 这张表是按照deptno分组,然后按照deptno分组合计.rollup函数可以完美的解决这个问题. 1.建 ...
- Object.assign和序列/反序列
Object.assign let testObj = { a:[1,2,4], b:{ name:'ls', school:['huf','yelu'], parent:{ father:'lili ...
- count(*)实现原理+两阶段提交总结
count(*)实现原理 不同引擎的实现: MyISAM引擎把表的总行数存在了磁盘上,执行COUNT(*)就会直接返回,效率很高: InnoDB在count(*)时,需要把数据一行一行的从引擎里面取出 ...
- 关于JsonArray与JsonObject的使用
学习地址:http://blog.csdn.net/lishuangzhe7047/article/details/28880009 关于前台向后台传递数组(里面包含json格式) [{"i ...
- C# 发起Get和Post请求
public class ApiHelper { //contentType application/json or application/xml public string HttpGet(str ...
- 获取第n天日期
function datezh(s){ return s = s>9 ? s:"0"+s } function dateTime(t){ var getNowTime = v ...