我想知道在Ruby中是否有办法用Python做我可以做的事情:
sum = reduce(lambda x, y: x + y, map(lambda x, y: x * y, weights, data))
Run Code Online (Sandbox Code Playgroud)
我有两个相同大小的数组与权重和数据,但我似乎无法找到类似于Ruby中的map的函数,减少我的工作.
我正在尝试连接一系列Seqs.
我可以做到apply concat.
user=> (count (apply concat (repeat 3000 (repeat 3000 true))))
9000000
Run Code Online (Sandbox Code Playgroud)
但是,根据我有限的知识,我会假设使用apply力量来实现懒惰的Seq,这对于非常大的输入来说似乎并不合适.如果可以的话,我宁愿懒洋洋地这样做.
所以我认为使用reduce就可以完成这项工作.
user=> (count (reduce concat (repeat 3000 (repeat 3000 true))))
Run Code Online (Sandbox Code Playgroud)
但这导致了
StackOverflowError clojure.lang.RT.seq (RT.java:484)
Run Code Online (Sandbox Code Playgroud)
我很惊讶,因为我会认为语义reduce意味着它是尾调用的递归.
两个问题:
apply是最好的方法吗?reduce一般不适合大投入?是否有适用reduce于熊猫系列的模拟?
例如,对于模拟map是pd.Series.apply,但我无法找到任何模拟reduce.
我的申请是,我有一个大熊猫系列清单:
>>> business["categories"].head()
0 ['Doctors', 'Health & Medical']
1 ['Nightlife']
2 ['Active Life', 'Mini Golf', 'Golf']
3 ['Shopping', 'Home Services', 'Internet Servic...
4 ['Bars', 'American (New)', 'Nightlife', 'Loung...
Name: categories, dtype: object
Run Code Online (Sandbox Code Playgroud)
我想将这些系列列表合并在一起使用reduce,如下所示:
categories = reduce(lambda l1, l2: l1 + l2, categories)
Run Code Online (Sandbox Code Playgroud)
但这需要花费可怕的时间,因为将两个列表合并在一起就是O(n)Python的时间.我希望pd.Series有一种矢量化的方式来更快地执行此操作.
Scala foldLeft在Java 8中的优势是什么?
我很想想它reduce,但是减少必须返回与它减少的相同类型的东西.
例:
import java.util.List;
public class Foo {
// this method works pretty well
public int sum(List<Integer> numbers) {
return numbers.stream()
.reduce(0, (acc, n) -> (acc + n));
}
// this method makes the file not compile
public String concatenate(List<Character> chars) {
return chars.stream()
.reduce(new StringBuilder(""), (acc, c) -> acc.append(c)).toString();
}
}
Run Code Online (Sandbox Code Playgroud)
上面代码中的问题是accumulator:new StringBuilder("")
因此,任何人都可以指出我正确的foldLeft/修复我的代码?
我正在学习tensorflow,我从tensorflow网站上获取了以下代码.根据我的理解,axis = 0表示行,axis = 1表示列.
他们如何获得评论中提到的输出?我根据我对##的想法提到了输出.
import tensorflow as tf
x = tf.constant([[1, 1, 1], [1, 1, 1]])
tf.reduce_sum(x, 0) # [2, 2, 2] ## [3, 3]
tf.reduce_sum(x, 1) # [3, 3] ##[2, 2, 2]
tf.reduce_sum(x, [0, 1]) # 6 ## Didn't understood at all.
Run Code Online (Sandbox Code Playgroud) 我经常研究一些JavaScript面试问题,突然间我看到一个关于使用reduce函数进行排序的问题Array,我在MDN中读到了它并在一些medium文章中使用它,但排序Array是如此创新:
const arr = [91,4,6,24,8,7,59,3,13,0,11,98,54,23,52,87,4];
Run Code Online (Sandbox Code Playgroud)
我想了很多,但我不知道如何回答这个问题,这个reduce call back功能一定是怎么回事?是什么initialValue的reduce功能?什么是accumulator和currentValue的call back功能reduce?
最后,这种方式是否比其他排序算法有一些好处?或者改进其他算法是否有用?
我试图了解该reduce()方法如何在java-8中工作。
例如,我有以下代码:
public class App {
public static void main(String[] args) {
String[] arr = {"lorem", "ipsum", "sit", "amet"};
List<String> strs = Arrays.asList(arr);
int ijk = strs.stream().reduce(0,
(a, b) -> {
System.out.println("Accumulator, a = " + a + ", b = " + b);
return a + b.length();
},
(a, b) -> {
System.out.println("Combiner");
return a * b;
});
System.out.println(ijk);
}
}
Run Code Online (Sandbox Code Playgroud)
输出是这样的:
Accumulator, a = 0, b = lorem
Accumulator, a = 5, b = …Run Code Online (Sandbox Code Playgroud) 我在CouchDB中创建了一个小型测试数据库,我在Futon中创建了一个临时视图.我写了mapper和reducer.映射器工作,但reducer的复选框永远不会显示.我知道应该有一个复选框,因为我在播放一个更大的数据库时看到它.
为什么不存在减速器复选框?这是正常的行为吗?减速机复选框有时不会出现吗?也许是因为我的结果集很小或者由于某种原因无法减少?(虽然我认为没有理由不能减少我的结果)
我的映射器就是这个.我添加了这个[1, 2, 3].forEach东西只是因为我认为我需要将结果集放大以获得reduce复选框.
function(doc) {
[1, 2, 3].forEach(function() {
emit(doc.name, 1);
});
}
Run Code Online (Sandbox Code Playgroud)
减速机就是这个.
function(keys, values, rereduce) {
return sum(values);
}
Run Code Online (Sandbox Code Playgroud)
结果如下所示:

在Python中,map()适用于遵循序列协议的任何数据.无论我是用字符串还是列表甚至元组提供它,它都能做出正确的事情.
我不能在OCaml吃蛋糕吗?除了查看我正在使用的集合类型并查找相应的List.map或Array.map或Buffer.map或String.map之外,我真的别无选择吗?其中一些甚至不存在!我要求的是不寻常的吗?我肯定错过了什么.
reduce()方法的大多数用例都可以使用for循环轻松重写.在JSPerf上进行测试表明,reduce()通常会慢60%-75%,具体取决于每次迭代中执行的操作.
除了能够以"功能样式"编写代码之外,还有任何真正的理由使用reduce()吗?如果通过编写更多代码可以获得60%的性能提升,为什么要使用reduce()?
编辑:事实上,其他功能方法如forEach()和map()都表现出类似的性能,比简单的循环慢至少60%.
这是JSPerf测试的链接(带有函数调用):forloop vs forEach
reduce ×10
java ×2
java-8 ×2
javascript ×2
python ×2
algorithm ×1
arrays ×1
clojure ×1
couchdb ×1
foldleft ×1
java-stream ×1
map ×1
mapreduce ×1
maps ×1
ocaml ×1
pandas ×1
performance ×1
polymorphism ×1
recursion ×1
ruby ×1
sorting ×1
tensor ×1
tensorflow ×1