gpt4 book ai didi

javascript - 为什么 compose 使用换能器从左到右应用?

转载 作者:行者123 更新时间:2023-12-04 03:30:38 26 4
gpt4 key购买 nike

示例代码:

// Compose functionality
const compose = (...fns) => {
return args => {
return fns.reduceRight((arg, fn) => fn(arg), args);
}
};

// List of transformation and predicate functions
const add1 = x => x + 1;
const isGreaterThanThree = x => x > 3;
const times2 = x => x * 2;

// Concat and Sum reducers (or the thing that I want to build).
/*
In this case, I'm using concatReducer, but I can easily substitute concatReducer with
sumReducer and change the initial value of the reduce method to zero.
*/
const concatReducer = (acc, el) => acc.concat(el);
const sumReducer = (acc, el) => acc += el;

// Transformation reducer (not sure the appropriate terminology)
const mapReducer = transform => {
return reducer => {
return (acc, el) => {
return reducer(acc, transform(el));
}
}
};

// Predicate reducer (again, not sure the appropriate terminology here)
const filterReducer = predicate => {
return reducer => {
return (acc, el) => {
return predicate(el) ? reducer(acc, el) : acc;
}
}
}

[1, 2, 3]
.reduce(
compose(
mapReducer(times2),
filterReducer(isGreaterThanThree),
mapReducer(add1),
)(concatReducer),
[]
);
我希望值是 [ 8 ] 而不是 [ 5, 7 ]。
Compose 是一个右结合 (reduceRight),但在这种情况下,它表现为左结合。
我心想,也许我的 compose 函数实现是错误的。
结果,我拉进来了 并使用了 R.compose,但我得到了相同的结果。
难道我做错了什么?或者这是组合在处理换能器时左关联的场景之一?

最佳答案

引述和部分示例摘自 https://github.com/cognitect-labs/transducers-js .
什么是换能器?

Transducers are simply a function of one arity. The only argument is another transducer transformer (labeled xf in the code base).


Since transducers are simply functions of one argument they can be composed easily via function composition to create transformer pipelines. Note that transducers return transformers when invoked.


示例:(改编)
var mapper = function(f) {
return function(xf) { // <- This is a transducer, it takes a transformer xf
return Mapper(f, xf); // <- and it returns another transformer xf'
};
};
注意:我们将涵盖 Mapper之后。
让我们用一些箭头函数重写:
var mapper = f => xf => Mapper(f, xf);
// ^^^^^^^^^^^^^^^^^^^
// This is a transducer
在上面的片段中,应该更清楚的是,转换器确实是一个函数,它接受一个转换器并返回另一个转换器。这两个组合是相等的:
compose(mapper(double), mapper(inc))

compose(xf => Mapper(double, xf), xf => Mapper(inc, xf))
趣闻
函数组合现在等待初始转换器,它通常被称为“步进”转换器(或“步进”函数),它负责将转换累积到容器中。
典型的容器可以是数组、字符串或对象。这种“步进”变压器可以:
  • 推送到一个数组(我们将在本答案的其余部分中将此类转换器称为 Push)
  • 或附加到字符串
  • 或在对象上设置属性

  • 但是我们不需要知道这种变压器的细节。然而,我们确实需要了解什么是变压器。
    什么是变压器?

    Transformers are objects. They must implement 3 methods, @@transducer/init, @@transducer/result and @@transducer/step. If a transformer is intended to be composed with other transformers they should either close over the next transformer or store it in a field.


    示例:(改编)
    var Mapper = function(f, xf) {
    return { // <-- This is a transformer
    "@@transducer/init": function() {
    return xf["@@transducer/init"]();
    },
    "@@transducer/result": function(result) {
    return xf["@@transducer/result"](result);
    },
    "@@transducer/step": function(result, input) {
    return xf["@@transducer/step"](result, f(input));
    // ^^^^^^^^
    // e.g. inc(41)
    }
    };
    };
    为了理解为什么换能器会颠倒组合顺序,我们需要仔细查看 @@transducer/step方法:
    "@@transducer/step": function(result, input) {
    return xf["@@transducer/step"](result, f(input));
    // ^^^^^^^^^^^^^^^^^^^^^^^ ^
    // called last called first!
    注: result是我们将在其中累积转换的容器。
    当你这样做时:
    compose(mapper(double), mapper(inc))(Push())
    您最终会得到一个如下所示的最终转换器:
    const xfinal = Mapper(double, Mapper(inc, push));
    通常,您的图书馆会为您执行此操作,但出于教育目的,我们将调用 @@transducer/step最终转换器上的方法并分解函数调用:
    xfinal['@@transducer/step']([], 20)
    类似于:
    Mapper(double, Mapper(inc, push))([], 20)
    ^^^^^^ ^^^^^^^^^^^^^^^^^
    f xf

    Mapper(inc, push)([], double(20))
    ^^^^^^^^^^^^^^^^^
    xf ^^^ ^^^^
    f' xf'

    push([], inc(double(20)))
    ^^^^ ^^^ ^^^^^^
    xf' f' f
    ^^^^^^^^^^^^^^^
    HERE!
    即使我们做了 compose(mapper(double), mapper(inc))我们可以看到 double函数之前被应用 inc做过。这不是 compose 中的错误功能,这只是变压器组合在一起时应该如何工作。

    关于javascript - 为什么 compose 使用换能器从左到右应用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66912608/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com