[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 ...
随机推荐
- python开发_webbrowser_浏览器控制模块
''' python的webbrowser模块支持对浏览器进行一些操作 主要有以下三个方法: webbrowser.open(url, new=0, autoraise=True) webbrowse ...
- Codeforces Round #280 (Div. 2) D. Vanya and Computer Game 预处理
D. Vanya and Computer Game time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Python学习笔记(四):字符串的学习
总结的内容: 1.字符串常用的方法 2.Python字符串格式化 3.Python字符串转义字 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符 ...
- lanmp安装一(centos+apache+nginx+mysql+php=lanmp地址下载)
背景 centos7 官网地址https://www.centos.org/download/ 下载选择DVD版进入(也就是标准版,系统齐全) 点击任何一个国家的下载链接 下载地址 http://m ...
- SQL Structured Query Language(结构化查询语言) 数据库
SQL是Structured Query Language(结构化查询语言)的缩写. SQL是专为数据库而建立的操作命令集,是一种功能齐全的数据库语言. 在使用它时,只需要发出“做什么”的命令,“怎么 ...
- Windows程序调试系列: 使用VC++生成调试信息 转
Windows程序调试系列: 使用VC++生成调试信息 ZhangTao,zhangtao.it@gmail.com, 译自 “Generating debug information with Vi ...
- 怎样防止ddos攻击
所有的主机平台都有抵御DoS的设置,总结一下,基本的有几种: 关闭不必要的服务 限制同时打开的Syn半连接数目 缩短Syn半连接的time out 时间 及时更新系统补丁 网络设置 网络设备可以从防火 ...
- C#,数据类型扩展 z
MACD的公式 DIFF : EMA(CLOSE,SHORT) - EMA(CLOSE,LONG);DEA : EMA(DIFF,M); MACD : 2*(DIFF-DEA), COLORSTIC ...
- 什么是软件project?
Normal 0 7.8 pt 0 2 false false false MicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNo ...
- RHPAM 7.2安装
1.产品架构 2.安装过程 下载相应介质 JBoss EAP (jboss-eap-7.2.0.zip) 下载地址 https://developers.redhat.com/products/e ...