ruby-changes:57532
From: Akinori <ko1@a...>
Date: Wed, 4 Sep 2019 16:17:15 +0900 (JST)
Subject: [ruby-changes:57532] f6da4a5447 (master): Describe #eager in the Enumerator::Lazy section
https://git.ruby-lang.org/ruby.git/commit/?id=f6da4a5447 From f6da4a544760e00d932fea9a586bd869e82ad339 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA <knu@i...> Date: Wed, 4 Sep 2019 16:07:40 +0900 Subject: Describe #eager in the Enumerator::Lazy section diff --git a/enumerator.c b/enumerator.c index 7a0a17b..a8d14bb 100644 --- a/enumerator.c +++ b/enumerator.c @@ -1676,15 +1676,34 @@ lazy_generator_init(VALUE enumerator, VALUE procs) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L1676 * * # This will fetch all URLs before selecting * # necessary data - * URLS.map { |u| JSON.parse(open(u).read) }. - * select { |data| data.key?('stats') }. - * first(5) + * URLS.map { |u| JSON.parse(open(u).read) } + * .select { |data| data.key?('stats') } + * .first(5) * * # This will fetch URLs one-by-one, only till * # there is enough data to satisfy the condition - * URLS.lazy.map { |u| JSON.parse(open(u).read) }. - * select { |data| data.key?('stats') }. - * first(5) + * URLS.lazy.map { |u| JSON.parse(open(u).read) } + * .select { |data| data.key?('stats') } + * .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) * */ -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/