Once we’re using Maybes throughout our code, it stands to reason that at some point we’ll get a Maybe and want to continue applying transformations. We might get a Nothing and want to perform those same transformations on some default value. In this lesson, we’ll do exactly that! We’ll see how we can use the alt method on the Maybe to recover from a Nothing and continue applying transformations with a Just.

The use case is that you get a Nothing from previous transformation, but you wish not to stop with Nothing, you want oto provide a default Maybe value and continue the process.

const crocks = require('crocks')
const { 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 getArticleName = obj => prop('name', obj)
.chain(safe(isNonEmptyString)) // If previous return Just(""), we want to continue check make sure it is not empty, other return Nothing
.alt(Maybe.of('page not found')); // If return Nothing then use alt value const getArticleUrl = obj => getArticleName(obj)
.map(createUrlFromName)
.option('default'); const url = getArticleUrl(article); console.log(url)

We are also able to chain multi 'alt' together, it will return first Just():

const bad = Nothing()
const good = Just()
const anotherGood = Just()
console.log(Nothing()
.alt(bad)) // Nothing console.log(Nothing()
.alt(bad)
.alt(good)) // Just 42 console.log(Nothing()
.alt(bad)
.alt(good)
.alt(anotherGood)) // Just 42

More example:

const Maybe = require('crocks/Maybe')
const alt = require('crocks/pointfree/alt')
const converge = require('crocks/combinators/converge')
const prop = require('crocks/Maybe/prop') const {Just} = Maybe; // maybeGetDisplay :: a -> Maybe b
const maybeGetDisplay = prop('display') // maybeGetFirst :: a -> Maybe b
const maybeGetFirst = prop('first') // maybeGetLast :: a -> Maybe b
const maybeGetLast = prop('last') // maybeConcatStrings :: Maybe String -> Maybe String -> Maybe String
const maybeConcatStrings = x => y => Just(x => y => x + ' ' + y).ap(x).ap(y).alt(x).alt(y) // maybeMakeDisplay :: a -> Maybe String
const maybeMakeDisplay = converge(maybeConcatStrings, maybeGetFirst, maybeGetLast) // maybeGetName :: a -> Maybe b
const maybeGetName = converge(alt, maybeGetDisplay, maybeMakeDisplay) console.log(maybeMakeDisplay({ display: 'Jack Sparrow' }))
//=> Just('Jack Sparrow') console.log(maybeMakeDisplay({ first: 'J', last: 'S' }))
//=> Just('J S') console.log(maybeMakeDisplay({ display: 'Jack Sparrow', first: 'J', last: 'S' }))
//=> Just('Jack Sparrow') console.log(maybeMakeDisplay({ first: 'J' }))
//=> Just('J') console.log(maybeGetName({ last: 'S' }))
//=> Just('S')

最新文章

  1. django models进行数据库增删查改
  2. Java内存模型深度解析:重排序 --转
  3. js正则
  4. string literals may have at most 255 elements
  5. 看起来像一个输入框的input,实际上是有两个input
  6. ASP.NET URL伪静态重写实现方法
  7. MySQL主存复制与读写分离的感悟
  8. 【转】ACE编程小结
  9. 关于DISPLAY变量显示问题
  10. GiftWrapping算法解决二维凸包问题
  11. 实践过配置成功的VNC安装配置
  12. 11661 - Burger Time?
  13. 2781: [JSOI2007]文本生成器
  14. SQL Server 表的管理_关于表的操作增删查改的操作的详解(案例代码)
  15. css_css 盒子水平居中 垂直居中
  16. AtCoder Grand Contest 011
  17. 012 Spark在IDEA中打jar包,并在集群上运行(包括local模式,standalone模式,yarn模式的集群运行)
  18. C++泛型编程(2)--通过排序和查找元素理解迭代器
  19. bootstrap日历控件
  20. ftp sftp vsftp

热门文章

  1. 96.extjs 页面
  2. python 函数参数的传递(参数带星号的说明)
  3. Apache上php项目简单部署
  4. sql 系统函数
  5. 使用GetInvocationList对委托链进行更多的控制
  6. PHP操作多进程
  7. [编码]ASCII、GBK、Unicode(万国码) 和 UTF-8
  8. Maven项目pom.xml配置详解
  9. Android 使用SQLite存储以及读取Drawable对象
  10. Go中的main函数和init函数