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. vs2010永久删除项目的相关操作

    1.选中要删除的项目,在解决方案资源管理器中,直接左键点击一下即可.. 2.点击键盘的删除键“DElect”. 3.然后找到工程的目录后,删除项目.即可永久性删除不在需要的程序或者是项目.

  2. php 原生简版日志导出

    <?phpfunction writeLog($msg){ $logFile = date('Y-m-d').'.txt'; $msg = date('Y-m-d H:i:s').' >& ...

  3. qW3xT.2挖矿病毒 解决过程及坑

    周一早上老大让我把项目更新一下,然后配置一下elasticsearch,我登上服务器之后部署的时候没有什么感觉,但是在配置elasticsearch的过程中感觉服务器哪个地方有点不对,下意识的top了 ...

  4. mysql数据库的介绍及安装

    一.什么是数据库 1.什么是数据(Data) 描述事物的符号记录成为数据,描述事物的符号既可以是文字.图片.图像.声音.语言等,数据有多种表现形式,他们都可以经过数字化后存入计算机 在计算机中描述一个 ...

  5. Coursera公开课-Machine_learing:编程作业6

    Support Vector Machines I have some issues to state. First, there were some bugs in original code wh ...

  6. 【java并发】(1)深入理解volatile关键字

    volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字才得以 ...

  7. 自己整理的HTML基本标签参考知识

          基 本 标 签 创建一个HTML文档 <html></html> 设置文档标题以及其他不在WEB网页上显示的信息 <head></head> ...

  8. Unity引擎GUI之Canvas和EventSystem

    最近想写一套关于UGUI所有控件的基础使用教程系列,主要是根据本人的使用心得以及部分测试附带字面翻译来写的,所以其中可能难以避免会有不正确的地方. 好了进入主题,既然是第一篇,我觉得我有必要先介绍一下 ...

  9. SLAM: Structure From Motion-移动中三维场景重建

    wiki链接:https://en.wikipedia.org/wiki/Structure_from_motion 三维重建: 三维物体建模总结 1. 视野内三维物体重建 : Kinect fusi ...

  10. PCL: 根据几何规则的曲面剖分-贪婪法表面重建三角网格

    点云场景中进行物体识别,使用全局特征的方法严重依赖于点云分割,难以适应杂乱场景.使用局部特征,即对点云进行提取类似于3D SURF.ROPS之类的局部特征,需要寻找离散点云块的局部显著性. 点云的基本 ...