我试图了解是否有办法终止还原操作而不检查整个流,我无法找到方法.
用例大致如下:让一长串的Integers需要折叠成一个Accumulator.每个元素检查都可能很昂贵,所以在内部Accumulator,我对传入进行检查Accumulator,看看我们是否需要执行昂贵的操作 - 如果我们不这样做,那么我只需返回累加器.
对于小(呃)列表来说,这显然是一个很好的解决方案,但是巨大的列表会产生我不想要的不必要的流元素访问成本.
这是一个代码草图 - 仅假设连续减少.
class Accumulator {
private final Set<A> setA = new HashSet<>;
private final Set<B> setB = new HashSet<>;
}
class ResultSupplier implements Supplier<Result> {
private final List<Integer> ids;
@Override
public Result get() {
Accumulator acc = ids.stream().reduce(new Accumulator(), f(), (x, y) -> null);
return (acc.setA.size > 1) ? Result.invalid() : Result.valid(acc.setB);
}
private static BiFunction<Accumulator, Integer, Accumulator> f() {
return (acc, element) -> {
if …Run Code Online (Sandbox Code Playgroud) javascript的新手,我无法计算布尔值数组中的trues数.我正在尝试使用reduce()函数.有人能告诉我我做错了什么吗?
//trying to count the number of true in an array
myCount = [false,false,true,false,true].reduce(function(a,b){
return b?a++:a;
},0);
alert("myCount ="+ myCount); // this is always 0
Run Code Online (Sandbox Code Playgroud) 我有一个类的代码,我应该使用reduce()方法来查找数组中的最小值和最大值.但是,我们只需要使用一次调用来减少.返回数组的大小应为2,但我知道reduce()方法总是返回一个大小为1的数组.我可以使用下面的代码获取最小值,但是我不知道如何获取同一个电话中的最大值.我假设一旦我获得了最大值,我只需在reduce()方法完成后将其推送到数组.
/**
* Takes an array of numbers and returns an array of size 2,
* where the first element is the smallest element in items,
* and the second element is the largest element in items.
*
* Must do this by using a single call to reduce.
*
* For example, minMax([4, 1, 2, 7, 6]) returns [1, 7]
*/
function minMax(items) {
var minMaxArray = items.reduce(
(accumulator, currentValue) => {
return (accumulator < currentValue ? accumulator : …Run Code Online (Sandbox Code Playgroud) 是否有可能有效地使用Clojure中的斐波那契系列reduce?"累加器"包含什么?
我想它一定是懒惰的.显而易见的是如何使用递归或循环/重复.
我想要left_join多个数据框:
dfs <- list(
df1 = data.frame(a = 1:3, b = c("a", "b", "c")),
df2 = data.frame(c = 4:6, b = c("a", "c", "d")),
df3 = data.frame(d = 7:9, b = c("b", "c", "e"))
)
Reduce(left_join, dfs)
# a b c d
# 1 1 a 4 NA
# 2 2 b NA 7
# 3 3 c 5 8
Run Code Online (Sandbox Code Playgroud)
这是有效的,因为它们都有相同的b列,但Reduce不允许我指定我可以传递给的其他参数left_join.是否有类似这样的工作?
dfs <- list(
df1 = data.frame(a = 1:3, b = c("a", …Run Code Online (Sandbox Code Playgroud) 在向 Array 原型添加方法后,其他一些不相关的脚本会中断。
该方法本身有效(按预期[1,2,3].xintsum()输出6)。
// adding a function to the Array prototype
Array.prototype.xintsum = function() { return this.reduce(function(old, add) {return old + add;}, 0); };
// accessing the array in a way that worked before
$(document).ready(function (){
var some_array = [];
for (head_n in some_array) {
var v = some_array[head_n];
$('<th></th>').text(v);
}
});Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>Run Code Online (Sandbox Code Playgroud)
在Java 8中,Stream有一个方法减少:
T reduce(T identity, BinaryOperator<T> accumulator);
Run Code Online (Sandbox Code Playgroud)
累加器运算符是否允许修改其任一参数?我认为不是因为JavaDoc说累加器应该是NonInterfering,尽管所有的例子都谈到修改集合,而不是修改集合的元素.
所以,举一个具体的例子,如果我们有
integers.reduce(0, Integer::sum);
Run Code Online (Sandbox Code Playgroud)
并假设一个Integer可变的时刻,sum可以通过向其添加(就地)第二个参数的值来修改其第一个参数吗?
我认为不是,但我也想要一个这个干扰导致问题的例子.
我有一系列for循环,它们在原始的字符串列表上工作,然后逐渐过滤列表,例如:
import re
# Regex to check that a cap exist in string.
pattern1 = re.compile(r'\d.*?[A-Z].*?[a-z]')
vocab = ['dog', 'lazy', 'the', 'fly'] # Imagine it's a longer list.
def check_no_caps(s):
return None if re.match(pattern1, s) else s
def check_nomorethan_five(s):
return s if len(s) <= 5 else None
def check_in_vocab_plus_x(s,x):
# s and x are both str.
return None if s not in vocab else s+x
slist = ['the', 'dog', 'jumps', 'over', 'the', 'fly']
# filter with check_no_caps
slist = [check_no_caps(s) …Run Code Online (Sandbox Code Playgroud) 使用reduce(bind_cols),可以组合相同维度的列表元素。但是,我想知道如何从可能具有不同维度元素的列表中仅组合相同维度(可能以某种方式指定维度)的元素。
library(tidyverse)
df1 <- data.frame(A1 = 1:10, A2 = 10:1)
df2 <- data.frame(B = 11:30)
df3 <- data.frame(C = 31:40)
ls1 <- list(df1, df3)
ls1
[[1]]
A1 A2
1 1 10
2 2 9
3 3 8
4 4 7
5 5 6
6 6 5
7 7 4
8 8 3
9 9 2
10 10 1
[[2]]
C
1 31
2 32
3 33
4 34
5 35
6 36
7 37
8 38
9 39 …Run Code Online (Sandbox Code Playgroud) 给定n部分和,可以将log2并行步骤中的所有部分和相加.例如,假设有八个线程与八个部分和:s0, s1, s2, s3, s4, s5, s6, s7.这可以在这样的log2(8) = 3连续步骤中减少;
thread0 thread1 thread2 thread4
s0 += s1 s2 += s3 s4 += s5 s6 +=s7
s0 += s2 s4 += s6
s0 += s4
Run Code Online (Sandbox Code Playgroud)
我想用OpenMP做这个,但我不想使用OpenMP的reduction子句.我想出了一个解决方案,但我认为可以使用OpenMP的task子句找到更好的解决方案.
这比标量加法更通用.让我选择一个更有用的情况:一个数组减少(见这里,这里,并在这里为更多关于阵列减少).
假设我想在阵列上进行数组缩减a.下面是一些代码,它们为每个线程并行填充私有数组.
int bins = 20;
int a[bins];
int **at; // array of pointers to arrays
for(int i = 0; i<bins; i++) a[i] = 0;
#pragma omp …Run Code Online (Sandbox Code Playgroud) reduce ×10
arrays ×3
javascript ×3
dplyr ×2
java ×2
java-8 ×2
java-stream ×2
r ×2
accumulator ×1
algorithm ×1
c ×1
clojure ×1
dictionary ×1
fibonacci ×1
filter ×1
fold ×1
nested-loops ×1
openmp ×1
prototype ×1
purrr ×1
python ×1
tidyverse ×1
typeerror ×1