[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 ...
随机推荐
- [COGS 2066]七十与十七
http://218.28.19.228/cogs/problem/problem.php?pid=2066 [题目描述] 七十君最近爱上了排序算法,于是Ta让十七君给Ta讲冒泡排序. 十七君给七十君 ...
- bzoj 2244
没有正确分析路径可能的条数,它是指数增长的,会爆long long. 然后就是正反两次时间分治. 另一个就是max with count,即带计数的最值,即除了记录最值,还要记录最值取得的次数. /* ...
- mac下递归创建ctags报错: "illegal option -- R"
在mac系统下不论是使用vim还是sublime text2的ctags插件都会碰到“illegal option -- R”这个错误,原因是mac使用的是自己的ctags,而我们通常在linux或w ...
- Codeforces Round #353 (Div. 2) D. Tree Construction 模拟
D. Tree Construction 题目连接: http://www.codeforces.com/contest/675/problem/D Description During the pr ...
- hdu 4163 Stock Prices 花式排序
Stock Prices Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- Codeforces Round #234 (Div. 2) B. Inna and New Matrix of Candies SET的妙用
B. Inna and New Matrix of Candies time limit per test 1 second memory limit per test 256 megabytes i ...
- C# 高级编程9 第30章MEF C#可扩展编程之MEF第2章(抄录)
Managed Extensibility Framework (MEF) 什么是 MEF? Managed Extensibility Framework 即 MEF 是用于创建轻量.可扩展应用 ...
- 委托、多播委托、泛型委托Func,Action,Predicate,ExpressionTree
当试图通过一个事件触发多个方法,抽象出泛型行为的时候,或许可以考虑使用委托. 通过委托构造函数或委托变量把方法赋值给委托 private delegate double DiscountDel ...
- 【docker-compose】使用docker-compose部署运行spring boot+mysql 【处理容器的时区问题】【详解】【福利:使用docker-compose构建 wordpress+mysql】
==================================================================================================== ...
- 【linux】centos7安装使用rz/sz命令
今天在一个新的centos上使用rz上传文件,提示找不到命令 需要在root用户下 安装命令: yum install -y lrzsz 使用rz上传文件到服务器上: rz 使用sz命令发送服务器文件 ...