【问题标题】:Is it possible to implement scan as a transducer是否可以将扫描实现为换能器
【发布时间】:2015-02-14 18:14:23
【问题描述】:

我注意到我查看的各种传感器库中缺少scan(例如transducers-js)。是不可能实现还是我遗漏了什么?

【问题讨论】:

    标签: javascript rxjs transducer


    【解决方案1】:

    其实我可以回答我自己的问题。我试图以一种功能性的方式实现它,但我误解了传感器接口的使用方式。查看来自transducers-js的源代码,它们通常将状态保存在一个对象中,我可以以同样的方式实现scan

    var Scan = function( agg, start, xf ){
        this.xf = xf;
        this.agg = agg;
        this.accum = start;
    }
    Scan.prototype.init = function(){
        return this.xf.init();
    }
    Scan.prototype.result = function(v) {
        return this.xf.result(v);
    }
    Scan.prototype.step = function( result, input ) {
        this.accum = this.agg( this.accum, input );
        return this.xf.step( result, this.accum );
    }
    
    var scan = function(agg, start) {
        return function (xf) {
            return new Scan(agg, start, xf);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-05-01
      • 1970-01-01
      • 2016-12-09
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      相关资源
      最近更新 更多