美文网首页
MayBe 函子

MayBe 函子

作者: 湘兰沅芷 | 来源:发表于2021-08-28 22:45 被阅读0次

我们在编程过程中可能会遇到很多错误,需要对这些错误做相应的处理
MayBe 函子的作用就是可以对外不的空值情况做处理(控制副作用在允许的范围内)

 class MayBe {
   static of (value) {
     return new MayBe(value)
   }
   constructor (value) {
     this._value = value
   }
   map(fn) {
     return this.isNothing() ? MayBe.of(null) : MayBe.of(fn(this._value))
   }
   isNothing() {
     return this._value === null || this._value === undefined
   }
 }

 let r = MayBe.of('hello world').map(x => x.toUpperCase())
 let r = MayBe.of('hello world').map(x => x.toUpperCase()).map(x => null).map(x => x.split('  '))
 console.log(r)

MayBe函子的问题在于不知道是哪次调用出现了问题。Either函子解决这个问题=》下篇

相关文章

  • MayBe 函子

    我们在编程过程中可能会遇到很多错误,需要对这些错误做相应的处理MayBe 函子的作用就是可以对外不的空值情况做处理...

  • 详解函数式编程中的函子

    本文目录:1.什么是函子2.MayBe函子3.Either函子4.Pointed函子5.IO函子6.IO函子存在的...

  • 函数式编程(五)—— 函子

    Functor为什么要学函子?什么是Functor理解Functor总结MyBe函子Either函子IO函子Tas...

  • 【函数式】Monads模式初探——Endofunctor

    自函子 自函子(Endofunctor)是一个将范畴映射到自身的函子(A functor that maps a ...

  • 5.范畴的例子

    接着学习函子,函子是范畴之间的结构保持映射。 给出两个函子,通过逐点复合的方式可以得到一个新的函子,可以验证,这种...

  • 函子

  • JS函数式编程03--函子

    函子 函子的概念 函子是函数式编程里面最重要的数据类型,也是基本的运算单位和功能单位。函子首先是一个容器,它包含了...

  • 31.限制保持函子

    这一节关注于,与限制结构交换的函子 一个函子称之为保持限制的,当对任意小范畴和任意函子,如果限制存在,那么经过函子...

  • Maybe not maybe

    There are some things I haven't considered clearly I just...

  • Maybe not maybe

    There are some things I haven't considered clearly I just...

网友评论

      本文标题:MayBe 函子

      本文链接:https://www.haomeiwen.com/subject/qcmiiltx.html