[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
option
aMaybe
but would like to remain within aMaybe
type,coalesce
can be used.coalesce
expects (2) functions for it's inputs.The first function is used when invoked on a
Nothing
and will return aJust
instance wrapping the result of the function. The second function is used whencoalesce
is invoked on aJust
and is used to map the original value, returning a newJust
instance 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 ...
随机推荐
- 包教包会:本地推送 & 远程推送
什么是推送?注意,和我们常用的抽象通知不同(NSNotification): 可以让不在前台运行的app,告知用户app内部发生了什么事情:或者没有运行的app接收到服务器发来的通知..比如离线QQ接 ...
- [BZOJ2017][Usaco2009 Nov]硬币游戏(要复习系列)
又是DP? 好吧,或者说是博弈论,但是我不会啊. 先搞个O(n^3)的记忆化搜索,然后瞎搞好像发现两个状态几乎一样? 竟然过了样例,然后竟然A了... #include<iostream> ...
- mysql数据库的介绍及安装
一.什么是数据库 1.什么是数据(Data) 描述事物的符号记录成为数据,描述事物的符号既可以是文字.图片.图像.声音.语言等,数据有多种表现形式,他们都可以经过数字化后存入计算机 在计算机中描述一个 ...
- MySQL学习笔记之内连接
不多说,直接上干货! MySQL的内连接 #内连接,两个表按照条件匹配 select class1.stuid,class1.stuname,class1.sex,course from class ...
- chm文件打开无显示解决办法
右键单击chm文件---属性---在该页面选择“解除锁定”---ok!
- 【Linux】七种文件类型
Linux中的七种文件类型 d 目录文件. l 符号链接(指向另一个文件). s 套接字文件. b 块设备文件,二进制文件. c 字符设备文件. p 命名管道文件. - 普通文件
- CNN:Windows下编译使用Caffe和Caffe2
用于检测的CNN分为基于回归网络的方法和基于区域+CNN网络的方法,其中基于回归网络的方法典型为YOLO9000,可以兼容使用VGG-Net框架.其中基于区域+CNN网络方法,大量使用了Caffe作为 ...
- [Intermediate Algorithm] - Everything Be True
题目 所有的东西都是真的! 完善编辑器中的every函数,如果集合(collection)中的所有对象都存在对应的属性(pre),并且属性(pre)对应的值为真.函数返回ture.反之,返回false ...
- DB120连接TTL--OpenWRT
DB120 TTL线连接 1.解压文件安装USB TTL PL2303HX 驱动 2.插上usb转ttl设备 3.串口调试 4.连接ttl线到db120 5.The END
- vue移动端Ui组件 mint-ui 使用指南
1.上啦加载下拉刷新的使用 this.$refs.loadmore.onTopLoaded(); this.$refs.loadmore.onBottomLoaded(); 上啦刷新下拉加载的 动画显 ...