[Functional Programming] Write a simple version of Maybe

Maybe has two types: Just / Nothing. Just() will just return the value that passed in. Nothing returns nothing...

Just/ Nothing are both functors, they should have .map() method:

复制代码
const Just = x => ({
    map: f => Just(f(x)),
    inspect: () => `Just ${x}`,
})

const Nothing = x => ({
    map: f => Nothing('Nothing'),
    inspect: () => `${x}`,
})

const Maybe = {
    Just,
    Nothing
}
复制代码

We added 'inspect' method so that in REPL we can log out understandable message. for example, 'Just 4'  instead of '{ map: [Function: map] }'.... or whatever...

 

Currently, the code below return 'Just 5'

const inc = n => n + 1;

const input = Just(4)
const result = input.map(inc)

But we don't need 'Just' as a result, we want just 5; in order to achieve that, we add 'option()' method:

复制代码
const Just = x => ({
    map: f => Just(f(x)),
    inspect: () => `Just ${x}`,
    option: (_) => x,
})

const Nothing = x => ({
    map: f => Nothing('Nothing'),
    inspect: () => `${x}`,
    option: defVal => defVal
})
复制代码

For Just, it return whatever the current value 'x', ignore the default value we passed in; Nothing it will return the defautl vlaue back.

Now, we got:

const input = Just(4)
const
result = input.map(inc).option(0) // 5 const input = Nothing(4) const result = input.map(in) // Nothing const result = input.map(inc).option(0) // 0

 

Since we don't know it should be Just or Nothing, we can use some predict function as helper:

const safeNum = num => typeof num === 'number' ? Maybe.Just(num): Maybe.Nothing()
const input = safeNum(4)
const result = input.map(inc).option(0) // 5


const input = safeNum('4')
const result = input.map(inc).option(0) // 0

 

---------------

复制代码
const Just = x => ({
    map: f => Just(f(x)),
    inspect: () => `Just ${x}`,
    option: (_) => x,
})

const Nothing = x => ({
    map: f => Nothing('Nothing'),
    inspect: () => `${x}`,
    option: defVal => defVal
})

const safeNum = num => typeof num === 'number' ? Maybe.Just(num): Maybe.Nothing()

const Maybe = {
    Just,
    Nothing
}

const inc = n => n + 1;

const input = safeNum(4)
const result = input.map(inc).option(0)

console.log(result)
复制代码

 

posted @   Zhentiw  阅读(210)  评论(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-02-19 [Angular] Difference between ngAfterViewInit and ngAfterContentInit
2017-02-19 [Angular] Difference between ViewChild and ContentChild
2017-02-19 [Angular] @ContentChildren and QueryList
2017-02-19 [Angular] @ContentChild and ngAfterContentInit
2017-02-19 [Angular] Content Projection with ng-content
2016-02-19 [Immutable + AngularJS] Use Immutable .List() for Angular array
2016-02-19 [Protractor] Running tests on multiple browsers
点击右上角即可分享
微信分享提示