ruby-changes:58465
From: zverok <ko1@a...>
Date: Sun, 27 Oct 2019 18:58:00 +0900 (JST)
Subject: [ruby-changes:58465] 417369e0cd (master): Improve Enumerator.produce docs
https://git.ruby-lang.org/ruby.git/commit/?id=417369e0cd From 417369e0cd6ec96950d2d48f2c94e7b1eb012076 Mon Sep 17 00:00:00 2001 From: zverok <zverok.offline@g...> Date: Sat, 26 Oct 2019 14:02:59 +0300 Subject: Improve Enumerator.produce docs * Add to NEWS; * Add examples of while-alike cycles with practical tasks. diff --git a/NEWS b/NEWS index 5379caa..acc3d0c 100644 --- a/NEWS +++ b/NEWS @@ -248,6 +248,13 @@ Enumerator:: https://github.com/ruby/ruby/blob/trunk/NEWS#L248 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] diff --git a/enumerator.c b/enumerator.c index eee3a34..06e2a9e 100644 --- a/enumerator.c +++ b/enumerator.c @@ -2923,6 +2923,21 @@ producer_size(VALUE obj, VALUE args, VALUE eobj) https://github.com/ruby/ruby/blob/trunk/enumerator.c#L2923 * * 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) -- cgit v0.10.2 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/