[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 ...
随机推荐
- fsockopen get,post 封装 (转)
function http_request($url, $method = 'GET', $postfields = NULL, $headers = array()) { $parse = pa ...
- jQuery 对象转成 DOM 对象
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- Oracle快速收集AWR的方案
记一种方便的awr收集方法,该脚本可以按小时收集目标时段的awr 素材:awr_generate.sql(具体脚本内容请见本文末尾) (1)将awr_generate.sql置于数据库服务器本地路径, ...
- Spring Boot (9) mybatis全注解化
ORM对比图 框架对比 Spring JDBC Spring Data Jpa Mybatis 性能 性能最好 性能最差 居中 代码量 多 少 多 学习成本 低 高 居中 推荐指数 ❤❤❤ ❤❤❤❤❤ ...
- windows phone数据网络开发
LINQ LINQ的全称是Language INtegrated Query,即语言集成查询.LINQ是一种查询语言,不仅可以对数字库进行查询,还可以对.net的数据集.数组.Xml文档等对象进行查询 ...
- HTML的display属性将行内元素、块状元素、行内块状元素互相转换以及三者的区别
1.行内元素 查看演示 (1)设置宽高无效 (2)对margin仅设置左右方向有效,上下无效:padding设置上下左右都有效,即会撑大空间 (3)不会自动进行换行 <html> < ...
- 浅谈Websocket、Ajax轮询和长轮询(long polling)
浅谈Websocket.Ajax轮询和长轮询(long p0ll) 最近看到了一些介绍Websocket的文章,觉得挺有用,所以在这里将自己的对其三者的理解记录一下. 1.什么是Websocket W ...
- ie8及其以下版本兼容性问题之placeholder实现
1. 普通浏览器下修改placeholder颜色 因为每个浏览器的CSS选择器都有所差异,所以需要针对每个浏览器做单独的设定. 示例: input::-webkit-input-placeholder ...
- Android开发笔记(12)——ListView & Adapter
转载请注明:http://www.cnblogs.com/igoslly/p/6947225.html 下一章是关于ListFragment的内容,首先先介绍ListView的相关配置,理解ListF ...
- postgreSQL中跨库查询在windows下的实现方法
以下是在postgreSQL 8.1版本中的实践,其他版本类似: 1.将C:\Program Files\PostgreSQL\8.1\share\contrib下的dblink.sql复制到C:\P ...