- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是 MRI 中的错误还是对此行为有很好的解释?
def pmap(enum)
return to_enum(:pmap, enum) unless block_given?
enum.map { |e| Thread.new { yield e } }.map(&:value)
end
# Returns elements in order, as expected.
pmap(1..10) { |e| e } #=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Returns elements in nondeterministic order on MRI >= 1.9.3.
# Works as expected on JRuby, Rubinius, and earlier versions of MRI.
pmap(1..10).to_a #=> [7, 2, 3, 4, 6, 5, 9, 8, 10, 1]
第一个 map
应该返回一个线程数组,第一个线程产生 1
,等等。
第二个 map
应该收集每个线程的值。
我不明白为什么返回的结果会乱序。
我看过 what I believe is the relevant code in enum.c
,但我仍然不明白为什么会这样。我怀疑这是性能优化出了问题。还是我对 Enumerable#to_a
期望过高(具体来说,它不会改变可枚举的顺序)?
最佳答案
line 3将枚举映射到立即返回且独立于线程 block 完成的线程。 .value
blocks on the completion of the thread在创建很久之后。
这表明对 Thread block 的实际求值并未按顺序发生,但 Thread.new 初始化的结果确实发生得足够快以产生有序的 Thread 实例。
def pmap(enum)
return to_enum(:pmap, enum) unless block_given?
enum.map { |e| Thread.new { sleep(Random.rand); p e; yield e } }.map(&:value)
end
pmap(1..10) { |e| e }
1
2
5
6
8
3
7
9
4
10
以下是如何通过 to_enum
对并行执行的结果进行排序,它使用纤程来执行 block :
def pmap(enum)
return to_enum(:pmap, enum) unless block_given?
enum.each_with_index.map { |e,i| Thread.new { sleep(Random.rand); p e; yield ({index: i, value:e }) } }.map(&:value)
end
p to_enum(:pmap, 1..10).sort_by { |hash| hash[:index] }.map { |hash| hash[:value] }
#p pmap(1..10) { |x| x }
关于ruby - 为什么 Enumerable#to_a 在 Ruby 版本 >= 1.9.3 中以这种方式运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22109717/
我的代码已经通过了所有测试条件(例如,数组、字符串、散列..)除了这个: ValidPalindromeTest.new(81,96).palindrome? 它应该返回 true 但没有。代码是:
请原谅我的问题,但我是 Ruby 的初学者。 我有一段代码,不知道如何将输出代码制作成一个数组。我尝试使用 Ruby to_a 方法但不起作用,我确信这真的很简单,但请帮助我!!! length =
each_slice 和to_a 的链接让我很困惑。我知道 each_slice 是 Enumerable 的成员,因此可以在数组等可枚举对象上调用,而 chars 确实返回一个字符数组。 我还知道
我正在尝试将数组分割为数组对。 例如:["A","B","C","D"] 应该变成 [["A","B"],["C""D"]. 我相信通过 arg.each_slice(2).to_a 我已经成功了。但
我需要将传入的参数(单个对象或集合)转换为数组。我不知道论点是什么。如果它已经是一个数组,我想保留它,否则从中创建一个单元素数组。我希望同时允许 method(:objs => obj) 和 meth
这个函数... class Invoice < ActiveRecord::Base def self.open_subtotal sum{ |i| i.open_amount / (1.
刚好遇到这段代码 Enumerator.new((1..100), :take, 5).to_a # => [] 有谁知道为什么它返回一个空数组而不是一个包含 5 个整数的数组? 最佳答案 来自文档,
to_a 和 to_ary 有什么区别? 最佳答案 to_ary用于隐式转换,而to_a用于显式转换。 例如: class Coordinates attr_accessor :x, :y d
例如: a = [1,2,3,4] b = a c = a.to_a a.insert(0,0) #=> [0,1,2,3,4] b #=> [0,1,2,3,4] c
我可以写一个简短的例子来验证 Enumerable 的 to_a 在内部调用 each。在这里: class MyFooClass include Enumerable def initial
Savon gem我正在使用的是返回单个对象或数组,在 SOAP 响应返回之前我无法知道它会是哪个。 为方便起见,我希望将一个nil 响应转换为[],将单个响应转换为[obj] 和一个数组保持为数组。
#entries 之间的基本区别是什么?和 #to_a Enumerable 的方法ruby 中的模块。两者似乎在 Hash 上返回相同的结果 >> hash = {"name" => "foo" ,
在 ruby 1.9.3 中,我可以获得字符串的代码点: > "foo\u00f6".codepoints.to_a => [102, 111, 111, 246] 是否有一个内置的方法去另一个
如果您有一个 Array 的子类 X,那么执行 X#to_a 会返回一个数组对象,而执行 X#to_ary 会返回一个 x 对象。 虽然我明白 to_a 的意思是“我可以变成一个数组”,而 to_ar
我正在尝试解密一组用于数据库迁移的密码。我有一些旧的 Rails 代码(实际上是一个 Runner 脚本)可以很好地解密它们。但是将相同的代码放入 Rake 任务会导致任务失败,并显示...未定义的方
是否有我们在已经是数组的对象上调用 Array.to_a 和 Array.to_ary 的用例? 如果不是,为什么这些方法存在于 Array 类中? 最佳答案 Are there any use ca
我知道eql? Hashes 使用它来查看对象是否与键* 匹配,而您匹配 def ==(rb) 如果您想支持 == 运算符,但哈希不使用 == 一定有充分的理由。这是为什么?你什么时候会有 == 和
这是 MRI 中的错误还是对此行为有很好的解释? def pmap(enum) return to_enum(:pmap, enum) unless block_given? enum.map
我在 IRB 中玩耍,发现: ("A".."Z").to_a(当然)返回: => ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"
我正在学习 Ruby,我看到了一些让我有点困惑的方法,特别是 to_s 与 to_str(同样,to_i /to_int, to_a/to_ary, & to_h/to_hash ).我读到的内容解释
我是一名优秀的程序员,十分优秀!