[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 efforts might need to enlist some logic to recover properly. In this lesson, we’ll see how we can use the coalesce method on the Maybe to recover from a Nothing by calling a function.
When one would like to
optionaMaybebut would like to remain within aMaybetype,coalescecan be used.coalesceexpects (2) functions for it's inputs.The first function is used when invoked on a
Nothingand will return aJustinstance wrapping the result of the function. The second function is used whencoalesceis invoked on aJustand is used to map the original value, returning a newJustinstance wrapping the result of the second function.
Difference between 'coalesce' & 'alt' is that:
'alt' returns just a hardcoded value;
'coalesce' is more dynamice, based on some condition, you can return different values.
The use case of 'coalesce' is similar to 'alt', but instead wrting two function as 'alt' does:
const getArticleName = obj => prop('name', obj)
.chain(safe(isNonEmptyString)) // If return Nothing then use alt value
.alt(Maybe.of('page not found'));
const getArticleUrl = obj => getArticleName(obj)
.map(createUrlFromName)
.option('default');
const url = getArticleUrl(article);
In 'coalesce', we just combine in one single function:
const getArticleUrl = obj => prop('name', obj)
.chain(safe(isNonEmptyString)) // If return Nothing then use first function of coalesce
.coalesce(() => 'page not found', createUrlFromName)
.option('default');
const url = getArticleUrl(article);
const crocks = require('crocks')
const { identity ,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 getArticleUrl = obj => prop('name', obj)
.chain(safe(isNonEmptyString)) // If return Nothing then use first function of coalesce
.coalesce(() => 'page not found', createUrlFromName)
.option('default');
const url = getArticleUrl(article);
console.log(url)
[Javascript Crocks] Recover from a Nothing with the `coalesce` Method的更多相关文章
- [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 ...
- [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 ...
随机推荐
- Android中使用Gson解析JSON数据
Android中使用Gson解析JSON数据 在Android中可以使用Gson解析JSON数据 首先,从 code.google.com/p/google-gson/downloads/list ...
- MySQL安装for windows
======MySQL安装 for windows====== 版本5.7.X MySQL服务器帮助我们来管理文件的操作 MySQL软件 - 服务器端软件 - 服务端程序 - 解析指令 - 对文件的操 ...
- POJ 1111(数字很吉利嘛) 简单BFS
Image Perimeters Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8594 Accepted: 5145 Desc ...
- java bean转Map
/** * @author xxxxxxxxxxx * @param object * 待转化类 * @param format自定义转化类型 * @return Map<String,Stri ...
- MYSQL日期时间字符串互转
--MYSQL date_format(date,'%Y-%m-%d') -------------->oracle中的to_char(); 日期时间转字符串 --MYSQL str_to_da ...
- 5.29MyBatis Generator
二.MyBatis Generator CRUD(创建,检索,更新,删除) 与表结构匹配的Java POJO BLOB (binary large object),二进制大对象,是一个可以存储二进制文 ...
- usaco 过路费 Cow Toll Paths, 2009 Dec
Description 翰家有 N 片草地,编号为 1 到 N ,彼此之间由 M 条双向道路连接,第 i 条道路连接了 Ai 和Bi,两片草地之间可能有多条道路,但没有道路会连接同一片草地,现有的道路 ...
- 通过Web Service实现IP地址查询功能
实例01 实现一个简单的Web服务访问 本实例将实现IP地址查询接口服务,根据用户传入的IP地址返回IP所在的省.市.地区,实例中将会用到IP地址库用于查询信息,由于数据较多,所以读者可在光盘资源文件 ...
- Linux之tar.gz file
A tarball (tar.gz file) is compressed tar archive. The tar program provides the ability to create ta ...
- Embedded之Stack之三
Stack Overflow While stacks are generally large, they don't occupy all of memory. It is possible to ...