Module: Enumerable
- Defined in:
- lib/enumerable/lazy.rb
Overview
Enumerable#lazy example implementation
Enumerable#lazy returns an instance of Enumerable::Lazy. You can use it just like as normal Enumerable object, except these methods act as ‘lazy’:
- map collect
- select find_all
- reject
- grep
- drop
- drop_while
- take_while
- flat_map collect_concat
- zip
Example
This code prints the first 100 primes.
require 'prime'
INFINITY = 1.0 / 0
p (1..INFINITY).lazy.select{|m| m.prime?}.take(100)
Acknowledgements
Inspired by https://github.com/antimon2/enumerable_lz
http://jp.rubyist.net/magazine/?0034-Enumerable_lz (ja)
Defined Under Namespace
Classes: Lazy