- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
示例代码:
// 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 ]。
最佳答案
引述和部分示例摘自 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/
在 Vaadin 7.0,显示时JavaBean Table 中的数据与 BeanContainer ,用新数据刷新表的正确方法是什么? 最佳答案 该表通过监听器监视表项的属性。如果您通过表的 Ite
首先,我使用的是带有 Axis2 1.6.2 的 eclipse,我正在 tomcat 6 上部署我创建的 Web 服务。Web 服务是在 eclipse 中通过自上而下的方法创建的。 我被要求使对我
我已将 Rails 3.1.1 应用程序升级到 Rails 3.1.3,现在,对于每个请求,它仅响应错误数量的参数(3 for 1)。不幸的是,它没有说明错误在哪里,并且应用程序跟踪为空。我认为存在一
我是一名优秀的程序员,十分优秀!