From: "marcandre (Marc-Andre Lafortune)" <ruby-core@...>
Date: 2012-07-22T06:39:00+09:00
Subject: [ruby-core:46598] [ruby-trunk - Feature #6636] Enumerable#size


Issue #6636 has been updated by marcandre (Marc-Andre Lafortune).


Hi,

On Sat, Jul 21, 2012 at 2:43 AM, naruse (Yui NARUSE) <naruse@airemix.jp> wrote:
> How about adding Enumerator#receiver and define yourself with it.

I agree `receiver` could be helpful. One would also need the `method` and the `arguments`, as in my request #3714.

Still, it doesn't really address the issue.

If someone wants to write a library to output the progression, for example, it is still not possible for a general enumerable/enumerator.

The proposal is so that:
- it is standard so anyone can depend on it and also create their own enumerables/enumerators
- it can help in some calculations
- it can help to have generic progression reports
- etc.

Thanks
----------------------------------------
Feature #6636: Enumerable#size
https://bugs.ruby-lang.org/issues/6636#change-28263

Author: marcandre (Marc-Andre Lafortune)
Status: Assigned
Priority: Normal
Assignee: matz (Yukihiro Matsumoto)
Category: core
Target version: 2.0.0


Now that it has been made clear that `Enumerable#count` never calls `#size` and that we have `Enumerable#lazy`, let me propose again an API for a lazy way to get the size of an Enumerable: `Enumerable#size`.

* call-seq:
*   enum.size  # => nil, Integer or Float::INFINITY
* 
* Returns the number of elements that will be yielded, without going through
* the iteration (i.e. lazy), or +nil+ if it can't be calculated lazily.
* 
*   perm = (1..100).to_a.permutation(4)
*   perm.size              # => 94109400
*   perm.each_cons(2).size # => 94109399
*   loop.size              # => Float::INFINITY
*   [42].drop_while.size   # => nil

About 66 core methods returning enumerators would have a lazy `size`, like `each_slice`, `permutation` or `lazy.take`.

A few would have `size` return `nil`:
  Array#{r}index, {take|drop}_while
  Enumerable#find{_index}, {take|drop}_while
  IO: all methods

Sized enumerators can also be created naturally by providing a block to `to_enum`/`enum_for` or a lambda to `Enumerator.new`.

Example for `to_enum`:

    class Integer
      def composition
        return to_enum(:composition){ 1 << (self - 1) } unless block_given?
        yield [] if zero?
        downto(1) do |i|
          (self - i).composition do |comp|
            yield [i, *comp]
          end
        end
      end
    end

    4.composition.to_a
    # => [[4], [3, 1], [2, 2], [2, 1, 1], [1, 3], [1, 2, 1], [1, 1, 2], [1, 1, 1, 1]]
    42.composition.size # => 2199023255552

Example for `Enumerator.new`:

    def lazy_product(*enums)
      sizer = ->{
        enums.inject(1) do |product, e|
          break if (size = e.size).nil?
          product * size
        end
      }
      Enumerator.new(sizer) do |yielder|
        # ... generate combinations
      end
    end

    lazy_product(1..4, (1..3).each_cons(2)).size # => 8
    lazy_product(1..4, (1..3).cycle).size # => Float::INFINITY



-- 
http://bugs.ruby-lang.org/