[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 Maybe
s 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 ...
随机推荐
- Django day04 路由控制
Django请求的整个的生命周期 Django中路由控制的作用: 一: 简单配置 url 是一个函数 -第一个参数是正则表达式(如果要精确匹配:'^publish'/$ 以^开头,以$结尾) -第二个 ...
- pjax使用小结
简介 虽然传统的ajax方式可以异步无刷新改变页面内容,但无法改变页面URL,因此有种方案是在内容发生改变后通过改变URL的hash的方式获得更好的可访问性(如https://liyu365.gith ...
- U - Three displays
Problem description It is the middle of 2018 and Maria Stepanovna, who lives outside Krasnokamensk ( ...
- B - Taxi(贪心)
Problem description After the lessons n groups of schoolchildren went outside and decided to visit P ...
- MySQL实现递归查询
DROP FUNCTION IF EXISTS queryChildrenCaseInfo;CREATE FUNCTION queryChildrenCaseInfo(cId INT)RETURNS ...
- Eclipse 添加本地 SVN插件以及运行项目的流程
去网上下载SVN插件包.里面包含文件如图: 把features和plugins文件夹里面的东西全部复制粘贴到eclipse安装目录下的features和plugins文件夹中就行.然后重启eclips ...
- 使用Attiny 85开发板制作BadUSB
什么是BadUSB?请查看:http://www.baike.com/wiki/BadUSB 或者看看腾讯这个视频!https://v.qq.com/x/page/l01425u2igw.html ...
- 5.13redis图形化工具---idea中配置redis密码
安装window下的redis,redis可视化管理工具(Redis Desktop Manager)安装,基础使用,实例化项目 源博客地址:https://www.cnblogs.com/cheng ...
- @PathVariable注解的使用和@Requestparam
一. @PathVariable @PathVariable这是一个路径映射格式的书写方式注解,在类映射路径的后加上/{对应方法参数中属性@PathVariable("code") ...
- Android RecyclerView初体验
很早之前就听说过RecyclerView这个组件了,但一直很忙没时间学习.趁着周末,就花了一天时间来学习RecyclerView. 准备工作 在Android Studio里新建一个Android项目 ...