Issue #17312 has been updated by zverok (Victor Shepelev).


@p8

> I was really suprised that `#last` isn't implemented in Enumerable while `#first` is.

It is natural.

That's because Enumerable is "uni-directional" (it is not guaranteed that you can iterate through it more than once, and there is no way to go back). Imagine this:
```ruby
lines = File.each_line('foo.txt')
lines.last # -- if it worked, ALL the file is already read here, and you can't do anything reasonable with it
```
Also, `last` is "intuitively" cheap ("just give me last element, what's the problem?"), but as `Enumerable` relies on `each`, and `each` only, `Enumerable#last` would mean "go through entire `each` till it would be exhausted, and give the last value", which might be very pricey.

All the methods I am trying to propose are compatible with uni-directional `#each`

----------------------------------------
Feature #17312: New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
https://bugs.ruby-lang.org/issues/17312#change-88590

* Author: zverok (Victor Shepelev)
* Status: Open
* Priority: Normal
----------------------------------------
(The offspring of #16987, which was too vague/philosophical)

I propose to add to `Enumerable` and `Enumerator::Lazy` the following methods:
* `compact`
* `product`
* `flatten`

All of them can be performed with a one-way enumerator. All of them make sense for situations other than "just an array". All of them can be used for processing large sequences, and therefore meaningful to add to `Lazy`.



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

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>