[Functional Programming] Write a simple version of Maybe
of version Write Programming Simple functional
2023-09-14 08:59:15 时间
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)
相关文章
- vue报错: ERROR TypeError: Cannot read property 'version' of undefined
- 《The Joy of Javascript》- 4 - Meta Programming
- vant安装报错npm WARN read-shrinkwrap This version of npm is compatible with lockfile
- Django DateTimeField 指定日期筛选(How can I filter a date of a DateTimeField in Django)
- ORA-01070: Using an old version of Oracle for the server ORACLE 报错 故障修复 远程处理
- ORA-22374: cannot reset the version of a type with table dependents ORACLE 报错 故障修复 远程处理
- ORA-25022: cannot reference a trigger of a different type ORACLE 报错 故障修复 远程处理
- ORA-25454: error during evaluation of rule set: string.string for iterator: string ORACLE 报错 故障修复 远程处理
- ORA-29309: export dump file was generated by different version of DBMS_PITR package ORACLE 报错 故障修复 远程处理
- ORA-31137: Index out of range ORACLE 报错 故障修复 远程处理
- ORA-62008: Cannot rewrite a newer version of XML redo diff (encountered: string, supported: string) ORACLE 报错 故障修复 远程处理
- ORA-01070: Using an old version of Oracle for the server ORACLE 报错 故障修复 远程处理
- ORA-02333: cannot create constraints on attributes of this column ORACLE 报错 故障修复 远程处理
- ORA-07513: sscgctl: $deq unexpected return on cancel of term. lock ORACLE 报错 故障修复 远程处理
- ORA-14287: cannot REBUILD a partition of a Composite Range partitioned index ORACLE 报错 故障修复 远程处理
- Akka(42): Http:身份验证 – authentication, authorization and use of raw headers详解编程语言
- ASSIGN COMPONENT OF STRUCTURE TO .详解leader 2021年06月11日 编程语言 135 0 编程语言
- 解决TabError: inconsistent use of tabs and spaces in indentation详解编程语言
- Exploring the Possibilities of Oracle 2195(oracle2195)
- CD and Explore the Amazing World of Linux(cd.linux)
- RDM: Unleashing the Power of Redis(rdmredis)
- Exploring the Massive Storage Capabilities of Oracle: Understanding its Size(oracle大小)
- Exploring the System Databases of MySQL: A Comprehensive Overview(mysql的系统数据库)
- Using pysvn to Make the Most of Linux Version Control(linuxpysvn)
- Exploring the Benefits and Uses of PPPD in the Linux Environment(pppdlinux)