mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Describe #eager in the Enumerator::Lazy section
This commit is contained in:
parent
1d4bd229b8
commit
f6da4a5447
1 changed files with 25 additions and 6 deletions
31
enumerator.c
31
enumerator.c
|
@ -1676,15 +1676,34 @@ lazy_generator_init(VALUE enumerator, VALUE procs)
|
||||||
*
|
*
|
||||||
* # This will fetch all URLs before selecting
|
* # This will fetch all URLs before selecting
|
||||||
* # necessary data
|
* # necessary data
|
||||||
* URLS.map { |u| JSON.parse(open(u).read) }.
|
* URLS.map { |u| JSON.parse(open(u).read) }
|
||||||
* select { |data| data.key?('stats') }.
|
* .select { |data| data.key?('stats') }
|
||||||
* first(5)
|
* .first(5)
|
||||||
*
|
*
|
||||||
* # This will fetch URLs one-by-one, only till
|
* # This will fetch URLs one-by-one, only till
|
||||||
* # there is enough data to satisfy the condition
|
* # there is enough data to satisfy the condition
|
||||||
* URLS.lazy.map { |u| JSON.parse(open(u).read) }.
|
* URLS.lazy.map { |u| JSON.parse(open(u).read) }
|
||||||
* select { |data| data.key?('stats') }.
|
* .select { |data| data.key?('stats') }
|
||||||
* first(5)
|
* .first(5)
|
||||||
|
*
|
||||||
|
* Ending a chain with ".eager" generates a non-lazy enumerator, which
|
||||||
|
* is suitable for returning or passing to another method that expects
|
||||||
|
* a normal enumerator.
|
||||||
|
*
|
||||||
|
* def active_items
|
||||||
|
* groups
|
||||||
|
* .lazy
|
||||||
|
* .flat_map(&:items)
|
||||||
|
* .reject(&:disabled)
|
||||||
|
* .eager
|
||||||
|
* end
|
||||||
|
*
|
||||||
|
* # This works lazily; if a checked item is found, it stops
|
||||||
|
* # iteration and does not look into remaining groups.
|
||||||
|
* first_checked = active_items.find(&:checked)
|
||||||
|
*
|
||||||
|
* # This returns an array of items like a normal enumerator does.
|
||||||
|
* all_checked = active_items.select(&:checked)
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue