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 a Maybe but would like to remain within a Maybe 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 a Justinstance wrapping the result of the function. The second function is used when coalesce is invoked on a Just and is used to map the original value, returning a new Just 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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. [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. ...

  6. [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 ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. Autobiography

    Autobiography My name is Donggongdechen. I am ** years old. I was born in XuChang, HeNan province, t ...

  2. HTTP权威协议笔记-8.集成点:网关、隧道及中继

    .8.1 网关 定义:网关类似与翻译器,它抽象出了一种能够到达资源的方法. 实用:网关可以自动将HTTP流量转换为其他协议,这样使用HTTP协议的一方就不需要了解其他协议,也可实现与其他程序或设备交互 ...

  3. Rails5 关联表格搜索

    创建: 2017/08/13   other_type_car = Car.joins(:car_type).active.find_by(car_type: car_type)   @recomme ...

  4. idea新建文件无法识别(二)

    1,出现问题的步骤: 当想新增一个Hello.java文件时候 选错了文件类型.新建了一个text文件.当回过头来把文件后缀改掉的时候发现idea无法识别,颜色一直是灰色. 2,解决问题的办法: 选择 ...

  5. Mysql数据库系列

    详情点击 MySQL基础 Mysql表操作 Mysql插入 更新 删除 查询操作 Mysql创建用户和授权 基本的Mysql语句 Mysql库的操作 Mysql表的操作 Mysql数据类型(一) My ...

  6. vue-pdf的使用方法及解决在线打印预览乱码

    最近在用vue做项目的时候,页面中需要展示后端返回的PDF文件,于是便用到了vue-pdf,其使用方法为 : npm install --save vue-pdf 官网地址:https://www.n ...

  7. 域名转让(bcd.cn,tjcpda.com,jscpda.com,fjcpda.com)

    因本人工作有所调整,现对所拥有的域名全部转让,有需要者请与我联系,谢谢. bcd.cn tjcpda.com jscpda.com fjcpda.com 联系方式: QQ:1181608198 Ema ...

  8. Vue初级

    来学习vue.js的宝宝们应该都了解了什么是vue并且是有兴趣去学的,我也是自己学习vue,欢迎大家一起讨论. 现在,我么先自己尝试用vue来写个hello world吧. 一.创建一个 .html  ...

  9. svn命令行批量删除和批量添加

    svn命令行批量删除和批量添加 如果使用svn的命令行,例如在linux下的终端中使用,svn的添加命令是svn add,删除命令是svn del,但是缺乏批量的操作,如果我在资源管理器中,手动添加了 ...

  10. 三维重建面试13X:一些算法试题-今日头条AI-Lab

             被人牵着鼻子走,到了地方还墨明棋妙地吃一顿砖头.今日头条AI-Lab,其实我一直发现,最擅长的还是点云图像处理,且只是点云处理. 一.C++题目   New 与Malloc的区别: ...