[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 Maybe and want to continue applying transformations. We might get a Nothing and want to perform those same transformations on some default value. In this lesson, we’ll do exactly that! We’ll see how we can use the alt method on the Maybe to recover from a Nothing and continue applying transformations with a Just.
The use case is that you get a Nothing from previous transformation, but you wish not to stop with Nothing, you want oto provide a default Maybe value and continue the process.
const crocks = require('crocks')
const { and, compose, isEmpty, isString, Maybe, not, prop, safe } = crocks
const { join, split, toLower } = require('ramda')
const article = {
id: ,
name: 'Learning crocksjs functional programming library'
};
const createUrlSlug = compose(join('-'), split(' '), toLower);
const createUrl = slug => `https://egghead.io/articles/${slug}`;
const createUrlFromName = compose(createUrl, createUrlSlug);
const isNonEmptyString = and(not(isEmpty), isString);
const getArticleName = obj => prop('name', obj)
.chain(safe(isNonEmptyString)) // If previous return Just(""), we want to continue check make sure it is not empty, other return Nothing
.alt(Maybe.of('page not found')); // If return Nothing then use alt value
const getArticleUrl = obj => getArticleName(obj)
.map(createUrlFromName)
.option('default');
const url = getArticleUrl(article);
console.log(url)
We are also able to chain multi 'alt' together, it will return first Just():
const bad = Nothing()
const good = Just()
const anotherGood = Just()
console.log(Nothing()
.alt(bad)) // Nothing console.log(Nothing()
.alt(bad)
.alt(good)) // Just 42 console.log(Nothing()
.alt(bad)
.alt(good)
.alt(anotherGood)) // Just 42
More example:
const Maybe = require('crocks/Maybe')
const alt = require('crocks/pointfree/alt')
const converge = require('crocks/combinators/converge')
const prop = require('crocks/Maybe/prop')
const {Just} = Maybe;
// maybeGetDisplay :: a -> Maybe b
const maybeGetDisplay = prop('display')
// maybeGetFirst :: a -> Maybe b
const maybeGetFirst = prop('first')
// maybeGetLast :: a -> Maybe b
const maybeGetLast = prop('last')
// maybeConcatStrings :: Maybe String -> Maybe String -> Maybe String
const maybeConcatStrings = x => y => Just(x => y => x + ' ' + y).ap(x).ap(y).alt(x).alt(y)
// maybeMakeDisplay :: a -> Maybe String
const maybeMakeDisplay = converge(maybeConcatStrings, maybeGetFirst, maybeGetLast)
// maybeGetName :: a -> Maybe b
const maybeGetName = converge(alt, maybeGetDisplay, maybeMakeDisplay)
console.log(maybeMakeDisplay({ display: 'Jack Sparrow' }))
//=> Just('Jack Sparrow')
console.log(maybeMakeDisplay({ first: 'J', last: 'S' }))
//=> Just('J S')
console.log(maybeMakeDisplay({ display: 'Jack Sparrow', first: 'J', last: 'S' }))
//=> Just('Jack Sparrow')
console.log(maybeMakeDisplay({ first: 'J' }))
//=> Just('J')
console.log(maybeGetName({ last: 'S' }))
//=> Just('S')
[Javascript Crocks] Recover from a Nothing with the `alt` method的更多相关文章
- [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] 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] 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] 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] Flatten Nested Maybes with `chain`
Sometimes, we run into situations where we end up with a Maybe within the context of another Maybe. ...
- [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] 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 ...
随机推荐
- Prime Path(bfs)
http://poj.org/problem?id=3126 题意:给两个四位数n,m,将n变成m需要多少步,要求每次只能改变n的某一位数,即改变后的数与改变前的数只有一位不同,且每次改变后的数都是素 ...
- Python 32 通信循环 连接循环 粘包问题
一:通信循环 二:连接循环 三:粘包问题
- 使用maven搭建SSH框架实现登陆、列表查询分页
SSH框架:struts2 + spring + hibernate web层:struts2+jsp service层:javaBean dao层:hibernate spring:管理Action ...
- windows phone媒体应用开发
MediaElement 可以播放许多不同类型的音频和视频媒体. MediaElement 是一个可以在其表面显示视频的矩形区域,也可以播放音频.MediaElement 支持触控输入事件. 使用属性 ...
- HDFS Shell命令操作与java代码操作
(一)编程实现以下功能,并利用 Hadoop 提供的 Shell 命令完成相同任务: (1) 向 HDFS 中上传任意文本文件,如果指定的文件在 HDFS 中已经存在,则由用户来指定是追加到原 ...
- Leetcode0100--Same Tree 相同树
[转载请注明]http://www.cnblogs.com/igoslly/p/8707664.html 来看一下题目: Given two binary trees, write a functio ...
- js---通过代码学习
1:本例演示 getElementsByTagName 方法. 2:本例演示 getElementsByTagName 方法 3:注意:
- css3基础篇三
CSS3 文本阴影 在 CSS3 中,text-shadow 可向文本应用阴影. 您能够规定水平阴影.垂直阴影.模糊距离,以及阴影的颜色: 实例 向标题添加阴影: h1 { text-shadow: ...
- vue遇到的大坑,h5在ios10版本下不能打开页面
无论是谁,在做事情的过程中总是会遇到学坑,才能成为最后的大神 这个坑不说了,找了半天.希望能帮助到你们 进入build文件夹: 找到webpack.prod.conf.js文件: 在UglifyPlu ...
- class path resource [processes/] cannot be resolved to URL because it does not exist
springboot整合activiti时报以下错误,原因是项目启动时检查流程文件 nested exception is java.io.FileNotFoundException: class p ...