Lou*_*uis 6 ruby arrays iterator enumerator
关于创建单向,延迟评估,可能无限的迭代器,我遇到了Ruby的问题.基本上,我正在尝试使用Ruby,就像我使用Haskell列表,以及在较小程度上使用Python生成器.
并不是我本身不理解它们; 我只是不知道如何像其他语言一样随意使用它们,而且我也不确定Ruby中的哪些方法会将它们变成背后的数组,将整个序列卸载到内存中是不必要的.
是的,我一直在研究Ruby参考手册.其实半小时,专注.或者显然不是.
例如,如果我要实现卡片组,它在Python中看起来像这样(未经测试):
# Python 3
from itertools import chain, count
face_ranks =
dict(
zip(
('jack', 'queen', 'king', 'ace'),
count(11)))
sorted_deck =
map(
lambda suit:
map(
lambda rank:
{
'rank' : rank,
'suit' : suit
},
chain(
range(2, 11),
face_ranks.keys())),
('clubs', 'diamonds', 'hearts', 'spades'))
Run Code Online (Sandbox Code Playgroud)
那么,我如何在Ruby中完成这项工作,完全避免使用数组呢?请注意,据我所知,上面的代码只使用元组和生成器:在任何时候都不会将整个序列转储到内存中,就像我使用了数组一样.我对上面的代码可能是错的,但是你得到了我想要的东西.
我如何链接迭代器(如Python的chain())?如何生成无限范围的迭代器(如Python的count())?如何在迭代器中添加一个数组(比如将元组传递给Python的链())而不将整个过程转换为数组?
我见过解决方案,但它们涉及阵列或不必要的复杂性,如光纤.
在Python中,我可以像数组一样简单地操作和抛出迭代器.我几乎可以把它们当作Haskell列表来对待,我最熟悉的就是它,而且在编码时我的想法就是这样.我对Ruby数组感到不舒服,这就是为什么我寻求其替代品的帮助.
我已经设法在互联网上获取有关它的信息,但我找不到任何涵盖Ruby中这种数据结构的基本操作?有帮助吗?
Ruby 似乎没有很多内置方法来执行您想要使用枚举器执行的不同操作,但您可以创建自己的方法。这就是我在这里所做的,使用 Ruby 1.9:
迭代器rb
def get_enums_from_args(args)
args.collect { |e| e.is_a?(Enumerator) ? e.dup : e.to_enum }
end
def build(y, &block)
while true
y << (begin yield; rescue StopIteration; break; end)
end
end
def zip(*args)
enums = get_enums_from_args args
Enumerator.new do |y|
build y do
enums.collect { |e| e.next }
end
end
end
def chain(*args)
enums = get_enums_from_args args
Enumerator.new do |y|
enums.each do |e|
build y do
e.next
end
end
end
end
def multiply(*args)
enums = get_enums_from_args args
duped_enums = enums.collect { |e| e.dup }
Enumerator.new do |y|
begin
while true
y << (begin; enums.collect { |e| e.peek }; rescue StopIteration; break; end )
index = enums.length - 1
while true
begin
enums[index].next
enums[index].peek
break
rescue StopIteration
# Some iterator ran out of items.
# If it was the first iterator, we are done,
raise if index == 0
# If it was a different iterator, reset it
# and then look at the iterator before it.
enums[index] = duped_enums[index].dup
index -= 1
end
end
end
rescue StopIteration
end
end
end
Run Code Online (Sandbox Code Playgroud)
我使用 rspec 编写了一个规范来测试这些函数并演示它们的作用:
iter_spec.rb:
require_relative 'iter'
describe "zip" do
it "zips together enumerators" do
e1 = "Louis".chars
e2 = "198".chars
zip(e1,e2).to_a.should == [ ['L','1'], ['o','9'], ['u','8'] ]
end
it "works with arrays too" do
zip([1,2], [:a, nil]).to_a.should == [ [1,:a], [2,nil] ]
end
end
describe "chain" do
it "chains enumerators" do
e1 = "Jon".chars
e2 = 0..99999999999
e = chain(e1, e2)
e.next.should == "J"
e.next.should == "o"
e.next.should == "n"
e.next.should == 0
e.next.should == 1
end
end
describe "multiply" do
it "multiplies enumerators" do
e1 = "ABC".chars
e2 = 1..3
multiply(e1, e2).to_a.should == [["A", 1], ["A", 2], ["A", 3], ["B", 1], ["B", 2], ["B", 3], ["C", 1], ["C", 2], ["C", 3]]
end
it "is lazily evalutated" do
e1 = 0..999999999
e2 = 1..3
e = multiply(e1, e2)
e.next.should == [0, 1]
e.next.should == [0, 2]
e.next.should == [0, 3]
e.next.should == [1, 1]
e.next.should == [1, 2]
end
it "resulting enumerator can not be cloned effectively" do
ranks = chain(2..10, [:jack, :queen, :king, :ace])
suits = [:clubs, :diamonds, :hearts, :spades]
cards = multiply(suits, ranks)
c2 = cards.clone
cards.next.should == [:clubs, 2]
c2.next.should == [:clubs, 2]
c2.next.should == [:clubs, 3]
c2.next.should == [:clubs, 4]
c2.next.should == [:clubs, 5]
cards.next.should == [:clubs, 6]
end
it "resulting enumerator can not be duplicated after first item is evaluated" do
ranks = chain(2..10, [:jack, :queen, :king, :ace])
suits = [:clubs, :diamonds, :hearts, :spades]
cards = multiply(ranks, suits)
cards.peek
lambda { cards.dup }.should raise_error TypeError
end
end
Run Code Online (Sandbox Code Playgroud)
如上面的规范所示,这些方法使用惰性求值。
zip此外,此处定义的、chain和函数的主要弱点multiply是生成的枚举器无法轻松复制或克隆,因为我们没有编写任何代码来复制这些新枚举器所依赖的枚举参数。您可能需要创建一个子类Enumerator或创建一个包含Enumerable模块或类似内容的类才能dup正常工作。
| 归档时间: |
|
| 查看次数: |
902 次 |
| 最近记录: |