[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, altering functions to accept a specific container type isn’t desirable. You could use map, passing in your function, or you could “lift” your function into a Maybe context. In this lesson, we’ll look at how we can get a function that accepts raw values to work with our Maybe container without the need to modify the original function or the input values.
Imaging you want to double a number:
const crocks = require('crocks');
const { safeLift, safe, isNumber } = crocks;
const dbl = n => n * ;
const safeDbl = n => safe(isNumber, n).map(dbl);
const res = safeDbl().option();
console.log(res)
Calling 'safe(pred, n).map(fn)' is also a common partten.
We can use 'safeLift' to simplfiy the code:
const crocks = require('crocks');
const { safeLift, safe, isNumber } = crocks;
const dbl = n => n * ;
/*
const safeDbl = n => safe(isNumber, n).map(dbl);
*/
const safeDbl = safeLift(isNumber, dbl);
const res = safeDbl().option();
console.log(res)
[Javascript Crocks] Make your own functions safer by lifting them into a Maybe context的更多相关文章
- [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] 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 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] 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] 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 ...
- [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基础-即时函数(Immediate Functions)(017)
1.即时函数的声明方法 即时函数(Immediate Functions)是一种特殊的JavaScript语法,可以使函数在定义后立即执行:(function () { alert('watch ...
- JavaScript Patterns 4.5 Immediate Functions
The immediate function pattern is a syntax that enables you to execute a function as soon as it is d ...
- [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 ...
随机推荐
- POJ 1469 COURSES 二分图最大匹配 二分图
http://poj.org/problem?id=1469 这道题我绝壁写过但是以前没有mark过二分图最大匹配的代码mark一下. 匈牙利 O(mn) #include<cstdio> ...
- 洛谷 P4884 多少个1?
题面在这里 好久没做题了2333,竟然还一次A了,神奇 大概就是等比数列然后把分母乘过去,然后直接BSGS就行了,就是要写快速乘恩... #include<bits/stdc++.h> # ...
- 谈应用环境下的TIME_WAIT和CLOSE_WAIT[转]
昨天解决了一个HttpClient调用错误导致的服务器异常,具体过程如下: http://blog.csdn.net/shootyou/article/details/6615051 里头的分析过程有 ...
- hdu 1026 bfs+记录路径
题意:从0,0点出发到n-1,m-1点,路上的数字代表要在这个点额外待多少秒,求最短的路 递归输出路径即可 #include<cstdio> #include<iostream> ...
- py脚本打包exe可执行文件
python3以上版本打包exe需要扩展软件:cx_freeze 下载地址:http://cx-freeze.sourceforge.net/ 1)安装后在\Python32\Scripts\cxfr ...
- 51nod 1515 明辨是非 启发式合并
1515 明辨是非 题目连接: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1515 Description 给n组操 ...
- JQ中get()与eq()的区别
.eq() : 减少匹配元素的集合,根据index索引值,精确指定索引对象. .get() : 通过检索匹配jQuery对象得到对应的DOM元素. 同样是返回元素,那么eq与get有什么区别呢? eq ...
- linux—文件目录简单介绍
1.Linux系统以文件目录的方式来组织和管理系统中的所有文件.所谓文件目录就是将所有文件的说明信息采用树型结构组织起来,即我们常说的目录:整个文件系统有一个“根”(root),然后在根上分“杈”(d ...
- OPENCV----在APP性能测试中的应用(一)
应用项目: APP的性能测试 应用场景: APP启动速度 视频开播速度 加载速度 等~~ 缘来: 基于APP日志和UiAutomator的测试方案,测试结果不能直白且精确的反应,用户的体验 ...
- 我们为什么需要Map-Reduce?
在讨论我们是否真的需要Map-Reduce这一分布式计算技术之前,我们先面对一个问题,这可以为我们讨论这个问题提供一个直观的背景. 问题 我们先从最直接和直观的方式出发,来尝试解决这个问题: 先伪一下 ...