我理解Map如何易于并行化 - 每台计算机/ CPU只能在阵列的一小部分上运行.
Reduce/foldl可并行化吗?似乎每个计算都取决于前一个计算.它是否可以与某些类型的函数并行化?
我能够使用map和sum实现这个功能,但如何使用reduce?
有两个列表:a,, b它们具有相同数量的值.我想算一算
a[0]*b[0]+a[1]*b[1]+...+a[n]*b[n]
Run Code Online (Sandbox Code Playgroud)
我写的工作版本map是
value = sum(map(lambda (x,y): x*y, zip(a, b)))
Run Code Online (Sandbox Code Playgroud)
怎么用reduce呢?我写:
value = reduce(lambda (x,y): x[0]*y[0] + x[1]*y[1], zip(a, b)))
Run Code Online (Sandbox Code Playgroud)
我收到错误" TypeError: 'float' object is unsubscriptable".
任何人都可以对此有所了解吗?
这不应该工作吗?
> val setOfSets = Set[Set[String]]()
setOfSets: scala.collection.immutable.Set[Set[String]] = Set()
> setOfSets reduce (_ union _)
java.lang.UnsupportedOperationException: empty.reduceLeft
at scala.collection.TraversableOnce$class.reduceLeft(TraversableOnce.scala:152)
[...]
Run Code Online (Sandbox Code Playgroud) 我正在开发一个应用程序,用户输入正则表达式作为过滤条件,但我不希望人们(轻松)能够输入.*(即匹配任何东西).问题是,如果我只是使用if (expression == ".*"),那么通过输入诸如此类的东西可以很容易地回避这个问题.*.*.
有没有人知道一个可能需要一个正则表达式的测试,看看它是否基本上.*是一个稍微复杂的形式?
我的想法是:
我可以看到表达式是否是一个或多个重复.*,(即如果它匹配(\.\*)+(引用/转义可能不完全准确,但你得到了想法).这个问题是可能有其他形式的全局写匹配(例如with $和^)太过于详尽,甚至没有考虑到前期,请进行测试.
我可以用它测试一些随机生成的字符串,并假设如果它们全部通过,则用户输入了全局匹配模式.这种方法的问题在于,可能存在表达式足够严密的情况,我只选择坏的字符串来匹配.
思绪,有人吗?
(仅供参考,该应用程序使用Java,但我想这更像是一个算法问题,而不是特定语言的问题.)
你知道reducePython中的方便功能.例如,您可以使用它来总结这样的列表(假装没有内置总和):
reduce(lambda x,y: x+y, [1,2,3,4], 0)
Run Code Online (Sandbox Code Playgroud)
返回(((0 + 1)+2)+3)+4 = 10.
现在如果我想要一个中间总和列表怎么办?在这种情况下,[1,3,6,10].
这是一个丑陋的解决方案.还有更多的pythonic吗?
def reducelist(f, l, x):
out = [x]
prev = x
for i in l:
prev = f(prev, i)
out.append(prev)
return out
Run Code Online (Sandbox Code Playgroud) 这个问题一直困扰着我一段时间,我似乎无法在网上找到答案.
是否可以从某个索引开始使用Array reduce方法?
简单的例子
var studentGrades = ["John Doe", "Some School", 6, 7, 8, 7, 9, 9];
Run Code Online (Sandbox Code Playgroud)
如果我只需要在studentGrades中遍历整数,我可以通过简单的for循环来完成
for(var i = 2; i < studentGrades.length; i++) {
// do stuff here ...
}
Run Code Online (Sandbox Code Playgroud)
但是,假设我需要得到一个平均等级,它是所有整数除以整数计数的总和.如果Array只包含整数,那么使用reduce就没有问题.
var onlyIntegersArr = [5,2,3,4];
var averageGrade = onlyIntegersArr.reduce(function(a,b){
return a + b;
}) / onlyIntegersArr.length;
Run Code Online (Sandbox Code Playgroud)
但是,如果我知道无论出于何种原因我需要跳过前两个数组元素并从索引数组[2]开始.
因此,例如我将reduce应用于studentGrades,但仅从index studentGrades开始[2].
减少可能吗?
谢谢你的解决方案,我喜欢切片方法,但我不喜欢在这种情况下使用新方法.
例如
var average = studentGrades.reduce(function(a,b,i){
return i >= 2 ? a+b : 0; …Run Code Online (Sandbox Code Playgroud) 我想知道使用JavaScript将数组拆分成两个不同数组的最佳方法是什么,但要将其保留在函数式编程领域.
假设应该根据某些逻辑创建两个数组.例如,拆分一个数组应该只包含少于四个字符的字符串,另一个包含其余字符串.
const arr = ['horse', 'elephant', 'dog', 'crocodile', 'cat'];
Run Code Online (Sandbox Code Playgroud)
我考虑过不同的方法:
过滤:
const lessThanFour = arr.filter((animal) => {
return animal.length < 4;
});
const fourAndMore = arr.filter((animal) => {
return animal.length >= 4;
});
Run Code Online (Sandbox Code Playgroud)
对我来说这个问题是你必须两次检查你的数据,但它是非常易读的.如果你有一个相当大的阵列,会有两次这样的巨大影响吗?
减少:
const threeFourArr = arr.reduce((animArr, animal) => {
if (animal.length < 4) {
return [[...animArr[0], animal], animArr[1]];
} else {
return [animArr[0], [...animArr[1], animal]];
}
}, [[], []]);
Run Code Online (Sandbox Code Playgroud)
数组的0索引包含少于4的数组,1索引包含多于3的数组.
我不太喜欢这个,因为看起来数据结构会带来一些问题,因为它是一个数组数组.我曾考虑使用reduce构建一个对象,但我无法想象它会比数组解决方案中的数组更好.
我已经设法在线查看类似的问题以及Stack Overflow,但是其中许多通过使用push()或者它们具有非常难以理解的实现来打破不可变性的想法,在我看来这打破了函数式编程的表现力.
有没有其他方法可以做到这一点?(功能当然)
我有以下示例数据集,我想根据方向的值使用Java流api进行转换/缩减
Direction int[]
IN 1, 2
OUT 3, 4
OUT 5, 6, 7
IN 8
IN 9
IN 10, 11
OUT 12, 13
IN 14
Run Code Online (Sandbox Code Playgroud)
至
Direction int[]
IN 1, 2,
OUT 3, 4, 5, 6, 7
IN 8, 9, 10, 11
OUT 12, 13
IN 14
Run Code Online (Sandbox Code Playgroud)
到目前为止我写的代码
enum Direction { IN, OUT }
class Tuple {
Direction direction;
int[] data;
public Tuple merge(Tuple t) {
return new Tuple(direction, concat(getData(), t.getData()));
}
}
private static int[] concat(int[] first, int[] second) …Run Code Online (Sandbox Code Playgroud) 1我试图制作一个无限制因子函数(只是出于好奇.)这适用于大型n(尝试高达100000并且它似乎工作,虽然我无法检查输出值的正确性,因为,它是大!)
(BigInt(1) to n).reduceRight(_*_)
Run Code Online (Sandbox Code Playgroud)
但我担心整个BigInt(1) to n范围可能都在记忆中,而我只是逐个元素地需要它reduceRight.看看Scala的标准库代码看起来BigInt(1) to n实际上是输出整个Range而不是懒惰Stream但我发现Stream.range我可以这样使用(注意n+1,流范围是独占的)
Stream.range[BigInt](BigInt(1), BigInt(n+1)).reduceRight(_*_)
Run Code Online (Sandbox Code Playgroud)
它适用于n=10000(由于某种原因,它需要更长的时间!这让我觉得也许正常的范围实际上也是一个Stream?)但是因为n=100000我得到这个堆栈溢出
java.lang.StackOverflowError
at java.math.BigInteger.add(Unknown Source)
at scala.math.BigInt.$plus(BigInt.scala:161)
at scala.math.Numeric$BigIntIsIntegral$class.plus(Numeric.scala:28)
at scala.math.Numeric$BigIntIsIntegral$.plus(Numeric.scala:40)
at scala.math.Numeric$BigIntIsIntegral$.plus(Numeric.scala:40)
at scala.math.Numeric$Ops.$plus(Numeric.scala:208)
at scala.collection.immutable.Stream$$anonfun$range$1.apply(Stream.scala:695)
at scala.collection.immutable.Stream$$anonfun$range$1.apply(Stream.scala:695)
at scala.collection.immutable.Stream$Cons.tail(Stream.scala:634)
at scala.collection.immutable.Stream$Cons.tail(Stream.scala:626)
at scala.collection.LinearSeqOptimized$class.reduceRight(LinearSeqOptimized.scala:130)
at scala.collection.immutable.Stream.reduceRight(Stream.scala:47)
at scala.collection.LinearSeqOptimized$class.reduceRight(LinearSeqOptimized.scala:131)
at scala.collection.immutable.Stream.reduceRight(Stream.scala:47)
at scala.collection.LinearSeqOptimized$class.reduceRight(LinearSeqOptimized.scala:131)
at scala.collection.immutable.Stream.reduceRight(Stream.scala:47)
...
Run Code Online (Sandbox Code Playgroud)
很明显,reduceRight就是这样称呼自己
reduceRight(reduceRight(first, second, op), third, op)...
Run Code Online (Sandbox Code Playgroud)
因此堆栈溢出.我假设它不是尾部调用优化的,因为它首先减少然后在返回值之前运行,因此无法进行优化.那我怎么能解决这个问题呢?我是否应该放弃功能方法并针对自定义命令式代码进行减少?
让我感到非常奇怪的是,虽然使用流几乎相同(BigInt(1) …
我有2个对象的数组的列表:
List<Object[2]>
Run Code Online (Sandbox Code Playgroud)
其中object [0]是一个整数,而object [1]是一个字符串。
我如何流式列表并在每个对象上应用不同的功能?这样,结果将是一个具有以下内容的数组:
result[0] = multiplication of all object[0]
result[1] = concatenation of all object[1]
Run Code Online (Sandbox Code Playgroud) reduce ×10
arrays ×2
java ×2
java-8 ×2
java-stream ×2
javascript ×2
python ×2
scala ×2
collections ×1
list ×1
map ×1
map-function ×1
multicore ×1
optimization ×1
range ×1
regex ×1
set ×1
stream ×1
sum ×1
wildcard ×1