You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Does an extensions like this make sense to be included?
require 'enumerable/lazy'
class Array
def lazy_slice(range)
Enumerator.new do |yielder|
range.each do |index|
yielder << self[index]
end
end.lazy
end
end
big_array = (0..10000).to_a # fake array, it won't generally be a range
p big_array.lazy_slice(9995..10000).map { |x| 2*x }.to_a
#=> [19990, 19992, 19994, 19996, 19998, 20000]
In this example I am trying to highlight that the currently available big_array.lazy.drop(9995).take(6) wouldn't be efficient.
The text was updated successfully, but these errors were encountered:
It does make sense when the range is large enough. When the range is small, you can write just some_big_array.slice(range).
But we can't add lazy_slice to Enumerable::Lazy because an Enumerable class does not always have
the indexing method '[]' (For example, the IO class includes Enumerable but does not have IO#[].)
By the way, Enumerable#lazy is accepted as Ruby 2.0 feature. I've updated the readme.
Does an extensions like this make sense to be included?
In this example I am trying to highlight that the currently available
big_array.lazy.drop(9995).take(6)
wouldn't be efficient.The text was updated successfully, but these errors were encountered: