Project

General

Profile

Feature #11797

`Enumerator#with_object` with multiple objects

Added by sawa (Tsuyoshi Sawada) over 3 years ago. Updated 3 months ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:72001]

Description

Sometimes, when working with Enumerator#with_object, I want to keep some additional temporary objects besides the one to return. A use case is as follows (I got this from this StackOverflow question: http://stackoverflow.com/questions/3418123). Suppose I have an enumerator created from an array:

e = ["a", "b", "c", "c", "a", "c"].to_enum

and want to get an array of its repeated elements in the order they are repeated (i.e., appears for the second time):

# => ["c", "a"]

I can do it using Enumerator#with_object like this:

e.to_enum.with_object([{}, []]){|c, (h, a)| h[c] ? a.push(c) : h.store(c, true)}.last.uniq

Here, I am getting the array referred to as a in the block, but besides that, I am using a temporal hash h. I thought it would be nice if Enumerator#with_object accepts one or more objects, pass them individually as block arguments, and returns only the last argument so that I can do this:

e.to_enum.with_object({}, []){|c, h, a| h[c] ? a.push(c) : h.store(c, true)}.uniq

History

Updated by sawa (Tsuyoshi Sawada) over 3 years ago

The StackOverflow question link was wrong. It should be: http://stackoverflow.com/questions/34181231.

Updated by shugo (Shugo Maeda) over 3 years ago

Hello,

I've removed his address from ruby-core and ruby-talk.

2015-12-18 13:27 GMT+09:00 Joseph Jones joeyi5216@gmail.com:

Joseph Jones liked your message with Boxer.

On December 9, 2015 at 09:15:54 MST, sawadatsuyoshi@gmail.com wrote:

Issue #11797 has been reported by Tsuyoshi Sawada.


Feature #11797: Enumerator#with_object with multiple objects
https://bugs.ruby-lang.org/issues/11797

  • Author: Tsuyoshi Sawada
  • Status: Open
  • Priority: Normal

* Assignee:

Sometimes, when working with Enumerator#with_object, I want to keep some
additional temporary objects besides the one to return. A use case is as
follows (I got this from this StackOverflow question:
http://stackoverflow.com/questions/3418123). Suppose I have an enumerator
created from an array:

e = ["a", "b", "c", "c", "a", "c"].to_enum

and want to get an array of its repeated elements in the order they are
repeated (i.e., appears for the second time):

=> ["c", "a"]

I can do it using Enumerator#with_object like this:

e.to_enum.with_object([{}, []]){|c, (h, a)| h[c] ? a.push(c) : h.store(c,
true)}.last.uniq

Here, I am getting the array referred to as a in the block, but besides
that, I am using a temporal hash h. I thought it would be nice if
Enumerator#with_object accepts one or more objects, pass them individually
as block arguments, and returns only the last argument so that I can do
this:

e.to_enum.with_object({}, []){|c, h, a| h[c] ? a.push(c) : h.store(c,
true)}.uniq

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

--
Shugo Maeda

Updated by sawa (Tsuyoshi Sawada) 3 months ago

I now realize that, for the given use case, I can use with_object multiple times as follows:

e
.each.with_object({}).with_object([]) {|(c, h), a| h[c] ? a.push(c) : h.store(c, true)}
.uniq

So I withdraw this feature request. Please close it.

#4

Updated by jeremyevans0 (Jeremy Evans) 3 months ago

  • Status changed from Open to Closed

Also available in: Atom PDF