[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 ...
随机推荐
- 维护直线的线段树:Bzoj1568,Bzoj3938(Uoj88)
有这样一类线段树,可以维护一些直线方程并对每个点求出最大值. 首先先看BZOJ1568,输入给你条直线的方程,你需要对于指定的位置求出最大的函数值. 看到数据范围nlog^2n可做,考虑用线段树去维护 ...
- Splay和LCT的复杂度分析
\(Splay\)的复杂度分析 不论插入,删除还是访问,我们可以发现它们的复杂度都和\(splay\)操作的复杂度同阶,只是一点常数的区别 我们不妨假设有\(n\)个点的\(splay\),进行了\( ...
- 保存全局Crash报告&发送邮件
上篇写到,将程序中没有处理到的crash信息保存到本地文件夹下.但是实际的情况是,你不可能总是将用户的设备拿过来.所以一般性的处理是,将crash reports发送到服务器或者邮箱.所以针对上篇的代 ...
- Codeforces Round #280 (Div. 2) A. Vanya and Cubes 水题
A. Vanya and Cubes time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- POJ Anniversary party 树形DP
/* 树形dp: 给一颗树,要求一组节点,节点之间没有父子关系,并且使得所有的节点的权值和最大 对于每一个节点,我们有两种状态 dp[i][0]表示不选择节点i,以节点i为根的子树所能形成的节点集所能 ...
- wikioi 3130 CYD刷题(背包)
题目描述 Description 下午,CYD要刷题了,已知CYD有N题可刷,但他只有M分钟的时间,而且他的智慧值为Q,也就是说他只能做出难度小于等于Q的题目.已知每题可得积分Ai,需花费时间Bi,难 ...
- Oracle VM VisualBox 虚拟机创建共享文件夹。
先来啰嗦几句,公司的电脑用的是 VMware10的虚拟机 相信大家都很熟悉了 VMware 创建共享文件功能可以直接安装tools来实现 但是 Oracle VM VisualBox 第一次玩 ...
- CareerCup之1.3字符串去重
[题目] 原文: 1.3 Design an algorithm and write code to remove the duplicate characters in a string witho ...
- The YubiKey -- HOW IT WORKS
A single YubiKey has multiple functions for protecting access to your email, your apps and your phys ...
- 在MyEclipse上安装svn插件
最近需要用到myeclipse做一个商城的项目开发,用svn作为项目的版本控制软件.但是在myeclipse上安装svn插件就是装不好,反复折腾了好几次都安装不成功.网上提供的安装办法有两种,一是:在 ...