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. Find Minimum in Rotated Sorted Array 典型二分查找

    https://oj.leetcode.com/problems/find-minimum-in-rotated-sorted-array/ Suppose a sorted array is rot ...

  2. vim gvim技巧大全(9)(转载)

    vim gvim技巧大全(9) 2 用命令}移动到这个段落的底部,标记为b3 输入命令:'a,'b move来移动文本.老版本的Vi编辑器不能很好的来处理多文件.但是Vim在处理多文件上却显得优秀得多 ...

  3. js,jquery中.each()方法遍历如何终止循环

    用.each()方法遍历节点的时候,用“return false”只能终止当前循环并跳入下一次循环,并不能终止所有循环.代码如下: $(".days").each(function ...

  4. Spring Boot (7) JdbcTemplate访问数据库

    使用jdbcTemplate操作数据库 spring framework对数据库的操作在jdbc上面做了深层次的封装,通过依赖注入功能,可以将datasource注册到jdbcTemplate中,学习 ...

  5. 5.17领扣--Arrays.copyOf()方法

    ?? 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同 ...

  6. Android Studio项目中有用文件与可忽略文件(初学者)

    可通过Settings --> Version Control --> Ignored Files进行设置或察看: 支持指定文件或文件夹,也支持匹配模式. Android Studio 中 ...

  7. 酷派改变者S1(C105/C105-6/C105-8) 解锁BootLoader 并刷入recovery root

    首先下载好工具链接:https://pan.baidu.com/s/1qZjOCUw 密码:u2dr 备用下载链接:https://pan.baidu.com/s/1pMlmAef 本篇教程教你如何傻 ...

  8. poj 1564 Sum It Up 搜索

    题意: 给出一个数T,再给出n个数.若n个数中有几个数(可以是一个)的和是T,就输出相加的式子.不过不能输出相同的式子. 分析: 运用的是回溯法.比较特殊的一点就是不能输出相同的式子.这个可以通过ma ...

  9. Unity引擎GUI之Button

    UGUI Button,可以说是真正的使用最广泛.功能最全面.几乎涵盖任何模块无所不用无所不能的组件,掌握了它的灵巧使用,你就几乎掌握了大半个UGUI! 一.Button组件: Interactabl ...

  10. 【sqli-labs】 less3 GET - Error based - Single quotes with twist string (基于错误的GET单引号变形字符型注入)

    实质上和less1没有多大区别,看懂了一样走流程 提交参数 加单引号 http://localhost/sqli/Less-3/?id=1' 观察报错,看near 和 at 的引号之间内容 '1'') ...