[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 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: 1,
    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(42)
const anotherGood = Just(10)
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')
复制代码

 

posted @   Zhentiw  阅读(377)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2017-05-14 [Recompose] Add Local State with Redux-like Reducers using Recompose
2017-05-14 [Recompose] Add Local State to a Functional Stateless Component using Recompose
2016-05-14 [Javascript] Advanced Console Log Arguments
2016-05-14 [Javascript] Log Levels and Semantic Methods
点击右上角即可分享
微信分享提示