- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在学习Java 8。我要面对的最困难的事情是Parallel Reduction。以下是我正在学习的用户@Stuart Marks 示例中的代码。
class ImmutableAverager
{
private final int total;
private final int count;
public ImmutableAverager(){this.total = 0;this.count = 0;}
public ImmutableAverager(int total, int count)
{
this.total = total;
this.count = count;
}
public double average(){return count > 0 ? ((double) total) / count : 0;}
public ImmutableAverager accept(final int i)
{
return new ImmutableAverager(total + i, count + 1);
}
public ImmutableAverager combine(final ImmutableAverager other)
{
return new ImmutableAverager(total + other.total, count + other.count);
}
来电
public static void main(String[] args)
{
System.out.println(Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.parallel()
.reduce(new ImmutableAverager(),
ImmutableAverager::accept,
ImmutableAverager::combine)
.average());
}
这产生了正确的结果,但后来我检查了reduce方法的签名
<U> U reduce(U identity,
BiFunction<U, ? super T, U> accumulator,
BinaryOperator<U> combiner);
如果代码是这样的,我会清楚地理解:
.reduce(new ImmutableAverager(),(a,b)->a.accept(b),(a,b)->a.combine(b))
我不明白怎么做:
ImmutableAverager::accept
可以转换为BiFunction
我的理解是这样的:
ImmutableAverager::accept
将其转换为类似的内容
(ImmutableAverage a)->a.accept(); //but this is a function with 1 parameter not with 2 parameters.
和
ImmutableAverager::merge
可以转换为BinaryOperator
。我的 friend @Stuart Marks 说
这些方法与要减少的函数参数相匹配,因此我们可以使用方法引用。
最佳答案
是的,当使用这种方法引用时,参数的移动方式有一个微妙之处,特别是“未绑定(bind)”方法引用。
让我们看看 reduce()
的第二个参数。它想要
BiFunction<U, ? super T, U> accumulator
所以它的抽象方法的签名是:
U apply(U, T)
(为简洁起见,省略了通配符)。该示例使用了方法引用 ImmutableAverager::accept
它的签名是:
ImmutableAverager accept(int i)
看起来这不起作用,因为 BiFunction
需要两个参数,而 accept
方法只需要一个。但请注意 accept
方法是 ImmutableAverager
上的实例方法类,因此它也隐式地接受一个“接收者”,即调用此方法的对象。对此方法的普通调用可能如下所示:
newAverager = oldAverager.accept(i);
真的,accept
方法实际上需要两个参数,尽管它看起来不像。第一个是接收器,其类型为 ImmutableAverager
,第二个类型为 int
。方法调用语法使得接收者看起来有一些特殊之处,但实际上并没有。就好像这是一个静态方法,调用如下:
newAverager = accept(oldAverager, i);
现在让我们看看它如何与 reduce
一起工作。称呼。有问题的代码是,
reduce(..., ImmutableAverager::accept, ...)
我在这里只显示第二个参数。这需要是 BiFunction
其参数为 U
和T
并返回 U
如上图所示。如果你看accept
方法并将接收者视为普通参数,而不是特殊的东西,它采用 ImmutableAverager
类型的参数和类型为 int
的参数,并返回 ImmutableAverager
。所以U
推断为 ImmutableAverager
和T
推断为 Integer
(从 int
装箱),并且此处的方法引用有效。
关键点是,对于未绑定(bind)的方法引用,方法引用是对实例方法的,但该方法是使用类名而不是实际实例来指定的。当这种情况发生时,接收者变成方法调用的第一个参数。
关于Java 8流并行reduce BiFunction累加器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26476605/
我不知道如何从 reducerRegister.js 中的 reducerForm.js reducer 访问 bool 值 isLoading 标志。我使用了 combineReducers() 并
我正在尝试找到一种理想的方法来更新我的状态树上的几个顶级字段,同时仍然维护拆分 reducer 。 这是我想出的一个简单的解决方案。 var state = { fileOrder: [0],
如果我们想按相同的键(第一个 reducer 的输出)分组,是否可以将 reducer 的输出直接发送到另一个 reducer 有时在链接时我发现我正在使用一个映射器来读取输入并将其复制到输出。因此想
我有一个如下所示的 reducer : const chart = combineReducers({ data, fetchProgress, fetchError,
当Map Reduce代码中有多个reduce时,它们之间没有任何形式的通信。但是,当执行诸如聚合之类的操作时,所有化简器共同产生单个最终输出。当它们之间没有通信时,聚合如何发生?是通过写入上下文吗?
我在 hive 中有一个表,我想从中获取所有数据。问题是: select * from tbl; 给我的结果与以下情况截然不同: select count(*) from tbl; 这是为什么?第二个
假设我有一个带有两个 reducer 的应用程序 - 使用 combineReducers() 组合的 tables 和 footer。 当我点击某个按钮时,将分派(dispatch)两个操作 - 一
我正在学习更深入的 redux,并且在处理高阶 reducer 时遇到一些麻烦。 我试图使用一个简单的分页示例来了解它是如何工作的。 NB:下面的代码只是 Nodejs 上下文中 redux 的一个快
我调用 RSS 提要并使用解析器对其进行解析。我收到一个数组。我现在想在最后创建一个对象,看起来像这样: { "2019-06-13": { "rates": { "usd":
我有一份学生列表,我的应用程序始终显示当时的一个学生,即 activePupil。到目前为止我有两个 reducer 。其中一个包含并默认返回所有子项的列表(以数组的形式): [ { id:
我有一个叫做 animals 的特征缩减器(切片缩减器)。我想将这些 reducer 拆分为哺乳动物、鸟类、鱼类等。这部分很简单,因为我可以简单地使用 ActionReducerMap。 现在假设哺乳
空数组上的简单reduce会抛出: 线程“main”java.lang.UnsupportedOperationException 中的异常:无法减少空的可迭代对象。 链接时同样的异常: val a
我有一些 25k 文档(原始 json 中为 4 GB)的数据,我想对其执行一些 javascript 操作,以使我的最终数据使用者 (R) 更容易访问这些数据,并且我想通过为每个更改添加一个新集合来
我只是想验证我对这些参数及其关系的理解,如果我错了请通知我。 mapreduce.reduce.shuffle.input.buffer.percent 告诉分配给 reducer 的整个洗牌阶段的内
我想将 redux 状态的值从 reducer 传递到另一个 reducer。在我的例子中,我想将 groups 的值从 groupReducer.js 中的状态传递到 scheduleReducer
所以,我有一个应用程序,它有多个 reducer ,因此有多个关联的 Action 创建者。 有一段时间,我的一个 reducer 更新了状态(由于编辑),因此,我必须确保其他 reducer 看到此
我有一个 reducer ,可以在调度操作时重新调整适当的状态。现在我定期调用 API,因此结果会一次又一次地触发操作。所以我想要的是,如果 reducer 状态已经有数据,那么另一个 reducer
当我尝试执行来自 here 的 DISTINCT reduce 时,出现错误。我已经在啤酒 sample 桶上重现了这个错误,所以这应该很容易重现。我没有在 mapreduce_errors.txt
在以下语法的简单优先级解析(分解)中,我们存在 shift-reduce 和 reduce-reduce 冲突。 X 是开始符号,X'-->$X$ 是添加规则。另外+和下符号是终结符。 X'-->$X
我需要编写一个连续调用两个reducer的Mapreduce程序。即,第一个 reducer 的输出将是第二个 reducer 的输入。我如何实现这一目标? 到目前为止我发现的内容表明我需要在我的驱动
我是一名优秀的程序员,十分优秀!