Project

General

Profile

Actions

Feature #8506

closed

Object#iter_for / Object#to_iter

Added by alindeman (Andy Lindeman) over 11 years ago. Updated 10 months ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:55388]

Description

=begin
Ruby's (({Enumerator})) and (({#enum_for})) methods are very powerful and I use them very often. However, (({Object#enum_for})) requires a method that yields, usually in some sort of loop.

Many objects in Ruby have methods that iterate to a "next value," but do not yield. For example, (({Fixnum#next.})) There is no way to use (({Fixnum#next})) with (({#enum_for})) directly that I am aware of.

I propose the introduction of (({Object#iter_for})) which--given a method--generates a lazy sequence by continually invoking the method on successive values. I call it (({iter})) or (({iterate})) because it is very similar to clojure's iterate: http://clojure.github.io/clojure/clojure.core-api.html#clojure.core/iterate

Proposed API:

0.iter_for(:next).take(5) # => [0, 1, 2, 3, 4]

require 'date'
Date.new(2013, 1, 1).iter_for(:next_month).take(3) # => [Tue, 01 Jan 2013, Fri, 01 Feb 2013, Fri, 01 Mar 2013]

I am especially excited about (({0.iter_for(:next)})) as I find myself using infinite lazy numeric sequences more often lately to solve specific kinds of problems. Right now you are required to write something like: (({Enumerator.new { |y| i = 0; loop { y << i; i += 1 } }})) or (({(0..Float::INFINITY).each})). Neither is especially elegant or happy to the developers' eyes in my opinion.

Thank you all :) Ruby is an amazing tool.
=end


Related issues 1 (1 open0 closed)

Related to Ruby master - Feature #20625: Object#chain_ofOpenActions

Updated by Anonymous over 11 years ago

+1

Updated by Anonymous over 11 years ago

Btw., regarding Qbject#to_enum, what is your opinion? Do you use it often? Or is there something about it that makes it less useful?

Updated by nobu (Nobuyoshi Nakada) over 11 years ago

  • Description updated (diff)

You may want to show the implementation in ruby (and tests)?

Updated by phluid61 (Matthew Kerwin) over 11 years ago

nobu (Nobuyoshi Nakada) wrote:

You may want to show the implementation in ruby (and tests)?

Here is an implementation: https://gist.github.com/phluid61/5747216

Updated by p8 (Petrik de Heus) 10 months ago

The following examples:

0.iter_for(:next).take(5) # => [0, 1, 2, 3, 4]
Date.new(2013, 1, 1).iter_for(:next_month).take(3) # => [Tue, 01 Jan 2013, Fri, 01 Feb 2013, Fri, 01 Mar 2013]

... can now be created with Enumerator.produce:

Enumerator.produce(0, &:succ).take(5) => [0, 1, 2, 3, 4]
Enumerator.produce(Date.new(2013, 1, 1), &:next_month).take(3) => [Tue, 01 Jan 2013, Fri, 01 Feb 2013, Fri, 01 Mar 2013]

Updated by Eregon (Benoit Daloze) 10 months ago

  • Status changed from Open to Rejected

Right, so given they do the same or very similar, let's close this.

Actions #8

Updated by mame (Yusuke Endoh) 4 months ago

Actions

Also available in: Atom PDF

Like1
Like0Like0Like0Like0Like0Like1Like0Like0