ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
@ 2020-11-09 13:27 zverok.offline
  2020-11-13 16:32 ` [ruby-core:100833] " daniel
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: zverok.offline @ 2020-11-09 13:27 UTC (permalink / raw
  To: ruby-core

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

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:100833] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
  2020-11-09 13:27 [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact zverok.offline
@ 2020-11-13 16:32 ` daniel
  2020-11-16  8:24 ` [ruby-core:100869] " mame
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: daniel @ 2020-11-13 16:32 UTC (permalink / raw
  To: ruby-core

Issue #17312 has been updated by Dan0042 (Daniel DeLorme).


What would be the interaction between Array#flatten and Enumerable#flatten ?
It's a big compatibility problem if flatten recursively applies to any Enumerable object within an array.

```ruby
x = Struct.new(:a).new(1)  #=> #<struct a=1>
[[[x]]].flatten            #=> [x] currently
Enumerable === x           #=> true
x.to_a                     #=> [1]
[[[x]]].flatten            #=> [1] would be a problem
```

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:100869] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
  2020-11-09 13:27 [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact zverok.offline
  2020-11-13 16:32 ` [ruby-core:100833] " daniel
@ 2020-11-16  8:24 ` mame
  2020-11-17 15:50 ` [ruby-core:100907] " zverok.offline
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: mame @ 2020-11-16  8:24 UTC (permalink / raw
  To: ruby-core

Issue #17312 has been updated by mame (Yusuke Endoh).


I think `Enumerable#compact` is trivial and maybe useful.

In regard to `Enumrable#flatten`, I agree with @Dan0042's concern. I think that it should flatten only Array elements, but it might look unnatural.

I'm unsure if `Enumerable#product` is useful. Its arguments are repeatedly iterated, so the arguments should be Arrays?

It would be good to separate tickets for each method, and a draft patch would be helpful for discussion.

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:100907] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
  2020-11-09 13:27 [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact zverok.offline
  2020-11-13 16:32 ` [ruby-core:100833] " daniel
  2020-11-16  8:24 ` [ruby-core:100869] " mame
@ 2020-11-17 15:50 ` zverok.offline
  2020-11-17 16:14 ` [ruby-core:100909] " daniel
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: zverok.offline @ 2020-11-17 15:50 UTC (permalink / raw
  To: ruby-core

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


@mame @Dan0042 Oh, you are right, starting to think from `Enumerable::Lazy` perspective I've missed a huge incompatibility introduced by `flatten`.

@mame I'll split into several proposals+patches: `Enumerable#compact`, and, I am starting to think now, maybe `Enumerator#flatten` would make some sense. As for `#product`, I just added it for completeness (as a method which also can work with unidirectional enumeration), I can't from the top of my head remember if I needed it some time in the past.

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:100909] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
  2020-11-09 13:27 [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact zverok.offline
                   ` (2 preceding siblings ...)
  2020-11-17 15:50 ` [ruby-core:100907] " zverok.offline
@ 2020-11-17 16:14 ` daniel
  2020-11-17 16:28 ` [ruby-core:100911] " zverok.offline
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: daniel @ 2020-11-17 16:14 UTC (permalink / raw
  To: ruby-core

Issue #17312 has been updated by Dan0042 (Daniel DeLorme).


I understand the thinking behind #flatten; if `ary.flatten` is possible then why not `ary.to_enum.flatten`? It should be isomorphic. But even with Enumerator the recursive aspect still represents a compatibility problem. So as long as the behavior of Array#flatten is not modified I think all this is trivial to implement:

    module Enumerable
      def compact(...); to_a.compact(...); end
      def product(...); to_a.product(...); end
      def flatten(...); to_a.flatten(...); end
    end

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:100911] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
  2020-11-09 13:27 [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact zverok.offline
                   ` (3 preceding siblings ...)
  2020-11-17 16:14 ` [ruby-core:100909] " daniel
@ 2020-11-17 16:28 ` zverok.offline
  2020-11-18 15:31 ` [ruby-core:100934] " petrik
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: zverok.offline @ 2020-11-17 16:28 UTC (permalink / raw
  To: ruby-core

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


> But even with Enumerator the recursive aspect still represents a compatibility problem.

I am not sure about its severity, though. I mean, Universe is big and sure somewhere in it there should be a code which has an *array of enumerators* and then does `flatten` on them... But I am not sure there is much of this code in the wild. 

I believe that this situation has the similar rarity class as the situation with code which does `unless obj.respond_to?(:except)` and will be broken by newly introduced `Hash#except` method... Like, *every* change is incompatibility for somebody, as https://xkcd.com/1172/ points, but `Enumerator#flatten` seems quite innocent.

> So as long as the behavior of Array#flatten is not modified I think all this is trivial to implement:
```ruby
def flatten(...); to_a.flatten(...); end
```

Note that this ticket is a follow-up of #16987. What I interested in, is more usages for `.lazy`, and *eager* implementation of `Enumerator::Lazy#flatten` is definitely a no-go.

So, I actually *could* propose just `Enumerator::Lazy#flatten`, but it seems quite weird that lazy enumerator can be flattened, while regular one can't.


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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:100934] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
  2020-11-09 13:27 [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact zverok.offline
                   ` (4 preceding siblings ...)
  2020-11-17 16:28 ` [ruby-core:100911] " zverok.offline
@ 2020-11-18 15:31 ` petrik
  2020-11-18 16:18 ` [ruby-core:100936] " zverok.offline
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: petrik @ 2020-11-18 15:31 UTC (permalink / raw
  To: ruby-core

Issue #17312 has been updated by p8 (Petrik de Heus).


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


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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:100936] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
  2020-11-09 13:27 [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact zverok.offline
                   ` (5 preceding siblings ...)
  2020-11-18 15:31 ` [ruby-core:100934] " petrik
@ 2020-11-18 16:18 ` zverok.offline
  2020-11-19 20:59 ` [ruby-core:100949] " petrik
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: zverok.offline @ 2020-11-18 16:18 UTC (permalink / raw
  To: ruby-core

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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:100949] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
  2020-11-09 13:27 [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact zverok.offline
                   ` (6 preceding siblings ...)
  2020-11-18 16:18 ` [ruby-core:100936] " zverok.offline
@ 2020-11-19 20:59 ` petrik
  2020-11-20  9:16 ` [ruby-core:100970] " matz
  2020-12-05 11:44 ` [ruby-core:101249] " zverok.offline
  9 siblings, 0 replies; 11+ messages in thread
From: petrik @ 2020-11-19 20:59 UTC (permalink / raw
  To: ruby-core

Issue #17312 has been updated by p8 (Petrik de Heus).


@zverok Thanks for the explanation. That makes a lot of sense!

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:100970] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
  2020-11-09 13:27 [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact zverok.offline
                   ` (7 preceding siblings ...)
  2020-11-19 20:59 ` [ruby-core:100949] " petrik
@ 2020-11-20  9:16 ` matz
  2020-12-05 11:44 ` [ruby-core:101249] " zverok.offline
  9 siblings, 0 replies; 11+ messages in thread
From: matz @ 2020-11-20  9:16 UTC (permalink / raw
  To: ruby-core

Issue #17312 has been updated by matz (Yukihiro Matsumoto).


My opinion for each proposed method.

* `compact` - OK, I can imagine use-cases too
* `product` - Negative; I concern about arguments (array or enumerable)
* `flatten` - Negative; I concern about types of elements (array of enumerable)

If you want `product` and `flatten` in `Enumerable`, submit separate issues to persuade me.

Matz.


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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [ruby-core:101249] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact
  2020-11-09 13:27 [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact zverok.offline
                   ` (8 preceding siblings ...)
  2020-11-20  9:16 ` [ruby-core:100970] " matz
@ 2020-12-05 11:44 ` zverok.offline
  9 siblings, 0 replies; 11+ messages in thread
From: zverok.offline @ 2020-12-05 11:44 UTC (permalink / raw
  To: ruby-core

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


PR for `#compact`: https://github.com/ruby/ruby/pull/3851

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

* 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/

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2020-12-05 11:44 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-11-09 13:27 [ruby-core:100753] [Ruby master Feature#17312] New methods in Enumerable and Enumerator::Lazy: flatten, product, compact zverok.offline
2020-11-13 16:32 ` [ruby-core:100833] " daniel
2020-11-16  8:24 ` [ruby-core:100869] " mame
2020-11-17 15:50 ` [ruby-core:100907] " zverok.offline
2020-11-17 16:14 ` [ruby-core:100909] " daniel
2020-11-17 16:28 ` [ruby-core:100911] " zverok.offline
2020-11-18 15:31 ` [ruby-core:100934] " petrik
2020-11-18 16:18 ` [ruby-core:100936] " zverok.offline
2020-11-19 20:59 ` [ruby-core:100949] " petrik
2020-11-20  9:16 ` [ruby-core:100970] " matz
2020-12-05 11:44 ` [ruby-core:101249] " zverok.offline

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).