From: zverok.offline@... Date: 2018-10-18T18:30:30+00:00 Subject: [ruby-core:89460] [Ruby trunk Feature#14781] Enumerator#generate Issue #14781 has been updated by zverok (Victor Shepelev). @knu The _ultimate_ goal for my proposal is, in fact, promoting Enumerator as a "Ruby way" for doing all-the-things with loops; not just "new useful feature". That's why I feel really uneasy about your changes to the proposal. **drop** ```ruby # from: `drop: 2` is part of Enumerator.from API Enumerator.from([node], drop: 2, &:parent).map(&:name) # generate: `drop(2)` is part of standard Enumerator API Enumerator.generate(node, &:parent).take(6).map(&:name).drop(2) ``` **allow_nil** (by default `false`: `nil` stops enumeration) ```ruby # from: # implicit "stop on nil" is part of Enumerator.from convention that code reader should be aware of Enumerator.from([node], &:parent).map(&:name) # don't stop on nil is explicit part of the API Enumerator.from([node], allow_nil: true) { |n| raise StopIteration if n.nil? n.parent }.map { |n| n&.name } # generate: "stop on nil" is explicit and obvious Enumerator.generate(node, &:parent).take_while(&:itself).map(&:name) # no mentioning of unnecessary "we don't need to stop on nil", no additional thinking p Enumerator.generate(node) { |n| raise StopIteration if n.nil? n.parent }.map { |n| n&.name } ``` **start with array** (I believe 1 and 0 initial values are the MOST used cases) ```ruby # from: we should start from empty array, expression nothing but Enumerator.from API limitation Enumerator.from([]) { 0 }.take(10) # generate: no start value Enumerator.generate { 0 }.take(10) # from: work with one value requires not forgetting to arrayify it Enumerator.from([1], &:succ).take(10) # generate: just use the value Enumerator.generate(1, &:succ).take(10) # from: "we pass as much of previous values as initial array had" convention Enumerator.from([0, 1]) { |i, j| i + j }.take(10) # generate: regular value enumeration, next block receives exactly what previous returns Enumerator.generate([0, 1]) { |i, j| [j, i + j] }.take(10).map(&:last) # ^ yes, it will require additional trick to include 0 in final result, but I believe this is worthy sacrifice ``` The problem with "API complication" is inconsistency. Like, a newcomer may ask: Why `Enumerator.from` has "this handy `drop: 2` initial arg", and `each` don't? Use cases could exist, too! ---------------------------------------- Feature #14781: Enumerator#generate https://bugs.ruby-lang.org/issues/14781#change-74500 * Author: zverok (Victor Shepelev) * Status: Feedback * Priority: Normal * Assignee: * Target version: ---------------------------------------- This is alternative proposal to `Object#enumerate` (#14423), which was considered by many as a good idea, but with unsure naming and too radical (`Object` extension). This one is _less_ radical, and, at the same time, more powerful. **Synopsys**: * `Enumerator.generate(initial, &block)`: produces infinite sequence where each next element is calculated by applying block to previous; `initial` is first sequence element; * `Enumerator.generate(&block)`: the same; first element of sequence is a result of calling the block with no args. This method allows to produce enumerators replacing a lot of common `while` and `loop` cycles in the same way `#each` replaces `for`. **Examples:** With initial value ```ruby # Infinite sequence p Enumerator.generate(1, &:succ).take(5) # => [1, 2, 3, 4, 5] # Easy Fibonacci p Enumerator.generate([0, 1]) { |f0, f1| [f1, f0 + f1] }.take(10).map(&:first) #=> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] require 'date' # Find next Tuesday p Enumerator.generate(Date.today, &:succ).detect { |d| d.wday == 2 } # => # # Tree navigation # --------------- require 'nokogiri' require 'open-uri' # Find some element on page, then make list of all parents p Nokogiri::HTML(open('https://www.ruby-lang.org/en/')) .at('a:contains("Ruby 2.2.10 Released")') .yield_self { |a| Enumerator.generate(a, &:parent) } .take_while { |node| node.respond_to?(:parent) } .map(&:name) # => ["a", "h3", "div", "div", "div", "div", "div", "div", "body", "html"] # Pagination # ---------- require 'octokit' Octokit.stargazers('rails/rails') # ^ this method returned just an array, but have set `.last_response` to full response, with data # and pagination. So now we can do this: p Enumerator.generate(Octokit.last_response) { |response| response.rels[:next].get # pagination: `get` fetches next Response } .first(3) # take just 3 pages of stargazers .flat_map(&:data) # `data` is parsed response content (stargazers themselves) .map { |h| h[:login] } # => ["wycats", "brynary", "macournoyer", "topfunky", "tomtt", "jamesgolick", ... ``` Without initial value ```ruby # Random search target = 7 p Enumerator.generate { rand(10) }.take_while { |i| i != target }.to_a # => [0, 6, 3, 5,....] # External while condition require 'strscan' scanner = StringScanner.new('7+38/6') p Enumerator.generate { scanner.scan(%r{\d+|[-+*/]}) }.slice_after { scanner.eos? }.first # => ["7", "+", "38", "/", "6"] # Potential message loop system: Enumerator.generate { Message.receive }.take_while { |msg| msg != :exit } ``` **Reference implementation**: https://github.com/zverok/enumerator_generate I want to **thank** all peers that participated in the discussion here, on Twitter and Reddit. ---Files-------------------------------- enumerator_from.rb (3.16 KB) -- https://bugs.ruby-lang.org/ Unsubscribe: