• js-函数式编程-monad-chain-mcompose-自动解嵌套


    chain和mcompose的例子:

    import fs from 'fs'
    import path from 'path'
    import * as R from 'ramda'
    const trace = R.curry((label, v) => {
      console.log(label, v)
      return v
    })
    const Maybe = function(x) {
      this.__value = x
    }
    
    Maybe.of = function(x) {
      return new Maybe(x);
    }
    
    Maybe.prototype.isNothing = function() {
      return (this.__value === null) || (this.__value === undefined);
    }
    
    Maybe.prototype.map = function(fun) {
      return this.isNothing() ? Maybe.of(null) : Maybe.of(fun(this.__value));
    }
    
    Maybe.prototype.join = function() {
      return this.isNothing() ? this.of(null): this.__value
    }
    const join = function(mma) {
      return mma.join()
    }
    
    const map = R.curry(function(f, any_functor_at_all) {
      return any_functor_at_all.map(f);
    });
    
    let threeFn = v => Maybe.of(2).map(R.add(v))
    let r = Maybe.of(3).map(threeFn).join().join() // 需要手动连续解两层
    console.log(r)
    
    // 使用chain
    // 写法1
    const chain = R.curry((f, m) => {
      return R.compose(join, map(f))(m)
    })
    let res = chain(function(three) {
      return Maybe.of(2).map(R.add(three));
    }, Maybe.of(3))
    console.log(res)
    
    // 写法2
    Maybe.prototype.chain = function(f) {
      return R.compose(join, map(f))(this)
    }
    let sum = Maybe.of(3).chain( v => Maybe.of(2).chain(R.add(v)) )
    console.log(sum)
    
    // 进一步优化
    // mcompose
    const mcompose = function(f, g) {
      return R.compose(chain(f), chain(g))
    }
    
    // 当有两个及以上chain时
    const addFn = R.curry((n, v) => Maybe.of(n).map(R.add(v))) 
    let sum2 = R.compose(
      chain(addFn(5)),
      chain(addFn(2))
    )
    console.log(sum2(Maybe.of(3)))
    
    // 使用mcompose
    let res2 = mcompose(
      addFn(5),
      addFn(2)
    )
    console.log(res2(Maybe.of(3)))
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 每次使用map都会导致新增一个容器
    • join用于手动解除容器包装
    • 使用chain自动解包装
    • mcompose 封装了compose和chain,语义清晰,使用简洁
  • 相关阅读:
    混合专家模型 (MoE) 详解
    MMDetection安装流程与测试
    数字化安全方案建设
    IDEA 2022.3 发布,终于支持 redis 了
    2023年的低代码:数字化、人工智能、趋势及未来展望
    2023秋招--游族--游戏客户端--二面面经
    ElasticSearch如何在前后台启动
    一文了解微服务低代码实现方式
    【应用程序代理对象ApplicationDelegate-应用程序启动过程介绍 Objective-C语言】
    论文翻译:2021_LACOPE: Latency-Constrained Pitch Estimation for Speech Enhancement
  • 原文地址:https://blog.csdn.net/junjiahuang/article/details/127694189