mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
Improve Enumerator.produce docs
* Add to NEWS; * Add examples of while-alike cycles with practical tasks.
This commit is contained in:
parent
aba23d83f2
commit
417369e0cd
Notes:
git
2019-10-27 18:58:05 +09:00
2 changed files with 22 additions and 0 deletions
7
NEWS
7
NEWS
|
@ -248,6 +248,13 @@ Enumerator::
|
|||
|
||||
New methods::
|
||||
|
||||
* Added Enumerator.produce to generate Enumerator from any custom
|
||||
data-transformation. [Feature #14781]
|
||||
|
||||
require 'date'
|
||||
dates = Enumerator.produce(Date.today, &:succ) #=> infinite sequence of dates
|
||||
dates.detect(&:tuesday?) #=> next tuesday
|
||||
|
||||
* Added Enumerator::Lazy#eager that generates a non-lazy enumerator
|
||||
from a lazy enumerator. [Feature #15901]
|
||||
|
||||
|
|
15
enumerator.c
15
enumerator.c
|
@ -2923,6 +2923,21 @@ producer_size(VALUE obj, VALUE args, VALUE eobj)
|
|||
*
|
||||
* ancestors = Enumerator.produce(node) { |prev| node = prev.parent or raise StopIteration }
|
||||
* enclosing_section = ancestors.find { |n| n.type == :section }
|
||||
*
|
||||
* Using ::produce together with Enumerable methods like Enumerable#detect,
|
||||
* Enumerable#slice, Enumerable#take_while can provide Enumerator-based alternative
|
||||
* for +while+ and +until+ cycles:
|
||||
*
|
||||
* # Find next Tuesday
|
||||
* require 'date'
|
||||
* Enumerator.produce(Date.today, &:succ).detect(&:tuesday?)
|
||||
*
|
||||
* # Simple lexer:
|
||||
* require 'strscan'
|
||||
* scanner = StringScanner.new('7+38/6')
|
||||
* PATTERN = %r{\d+|[-/+*]}
|
||||
* p Enumerator.produce { scanner.scan(PATTERN) }.slice_after { scanner.eos? }.first
|
||||
* # => ["7", "+", "38", "/", "6"]
|
||||
*/
|
||||
static VALUE
|
||||
enumerator_s_produce(int argc, VALUE *argv, VALUE klass)
|
||||
|
|
Loading…
Add table
Reference in a new issue