ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:91613] [Ruby trunk Feature#15618] Implement Enumerator::Yielder#to_proc
       [not found] <redmine.issue-15618.20190224155516@ruby-lang.org>
@ 2019-02-24 15:55 ` knu
  2019-02-24 17:16 ` [ruby-core:91614] " shevegen
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: knu @ 2019-02-24 15:55 UTC (permalink / raw)
  To: ruby-core

Issue #15618 has been reported by knu (Akinori MUSHA).

----------------------------------------
Feature #15618: Implement Enumerator::Yielder#to_proc
https://bugs.ruby-lang.org/issues/15618

* Author: knu (Akinori MUSHA)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
When writing an Enumerator block, you often want to delegate iteration to another method like this:

```ruby
enum = Enumerator.new { |y|
  Dir.glob("*.rb") { |file|
    File.open(file) { |f| f.each_line { |line| y << line } }
  }
}
```

I think this is such a common pattern, but the `{ |var| y << var }` part looks redundant compared to a normal iterator method being able to delegate to another method as simply as follows:

```
  def each(&block)
    @children.each(&block)
  end
```

So, I propose adding #to_proc to Yielder so you can directly pass a yielder object to another method as a block argument.

```ruby
enum = Enumerator.new { |y|
  Dir.glob("*.rb") { |file|
    File.open(file) { |f| f.each_line(&y) }
  }
}
```

Yielder is all about yielding, so I think it's pretty obvious what it means.

---Files--------------------------------
0001-Implement-Enumerator-Yielder-to_proc.patch (3.21 KB)


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

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

* [ruby-core:91614] [Ruby trunk Feature#15618] Implement Enumerator::Yielder#to_proc
       [not found] <redmine.issue-15618.20190224155516@ruby-lang.org>
  2019-02-24 15:55 ` [ruby-core:91613] [Ruby trunk Feature#15618] Implement Enumerator::Yielder#to_proc knu
@ 2019-02-24 17:16 ` shevegen
  2019-03-11  6:53 ` [ruby-core:91759] " matz
  2019-03-11  9:54 ` [ruby-core:91771] " knu
  3 siblings, 0 replies; 4+ messages in thread
From: shevegen @ 2019-02-24 17:16 UTC (permalink / raw)
  To: ruby-core

Issue #15618 has been updated by shevegen (Robert A. Heiler).


I have no particularly strong pro or con opinion on the functionality itself.

I have only one comment about syntax, though. While { |line| y << line } }
may or may not be redundant (let's leave that open for the moment), and is
definitely longer than the other variant suggested, I think it is actually a
simpler-to-understand syntax compared to the .each_line(&y) variant.

----------------------------------------
Feature #15618: Implement Enumerator::Yielder#to_proc
https://bugs.ruby-lang.org/issues/15618#change-76880

* Author: knu (Akinori MUSHA)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
When writing an Enumerator block, you often want to delegate iteration to another method like this:

```ruby
enum = Enumerator.new { |y|
  Dir.glob("*.rb") { |file|
    File.open(file) { |f| f.each_line { |line| y << line } }
  }
}
```

I think this is such a common pattern, but the `{ |var| y << var }` part looks redundant compared to a normal iterator method being able to delegate to another method as simply as follows:

```
  def each(&block)
    @children.each(&block)
  end
```

So, I propose adding #to_proc to Yielder so you can directly pass a yielder object to another method as a block argument.

```ruby
enum = Enumerator.new { |y|
  Dir.glob("*.rb") { |file|
    File.open(file) { |f| f.each_line(&y) }
  }
}
```

Yielder is all about yielding, so I think it's pretty obvious what it means.

---Files--------------------------------
0001-Implement-Enumerator-Yielder-to_proc.patch (3.21 KB)


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

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

* [ruby-core:91759] [Ruby trunk Feature#15618] Implement Enumerator::Yielder#to_proc
       [not found] <redmine.issue-15618.20190224155516@ruby-lang.org>
  2019-02-24 15:55 ` [ruby-core:91613] [Ruby trunk Feature#15618] Implement Enumerator::Yielder#to_proc knu
  2019-02-24 17:16 ` [ruby-core:91614] " shevegen
@ 2019-03-11  6:53 ` matz
  2019-03-11  9:54 ` [ruby-core:91771] " knu
  3 siblings, 0 replies; 4+ messages in thread
From: matz @ 2019-03-11  6:53 UTC (permalink / raw)
  To: ruby-core

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


Sounds reasonable.

Matz.

----------------------------------------
Feature #15618: Implement Enumerator::Yielder#to_proc
https://bugs.ruby-lang.org/issues/15618#change-77036

* Author: knu (Akinori MUSHA)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
When writing an Enumerator block, you often want to delegate iteration to another method like this:

```ruby
enum = Enumerator.new { |y|
  Dir.glob("*.rb") { |file|
    File.open(file) { |f| f.each_line { |line| y << line } }
  }
}
```

I think this is such a common pattern, but the `{ |var| y << var }` part looks redundant compared to a normal iterator method being able to delegate to another method as simply as follows:

```
  def each(&block)
    @children.each(&block)
  end
```

So, I propose adding #to_proc to Yielder so you can directly pass a yielder object to another method as a block argument.

```ruby
enum = Enumerator.new { |y|
  Dir.glob("*.rb") { |file|
    File.open(file) { |f| f.each_line(&y) }
  }
}
```

Yielder is all about yielding, so I think it's pretty obvious what it means.

---Files--------------------------------
0001-Implement-Enumerator-Yielder-to_proc.patch (3.21 KB)


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

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

* [ruby-core:91771] [Ruby trunk Feature#15618] Implement Enumerator::Yielder#to_proc
       [not found] <redmine.issue-15618.20190224155516@ruby-lang.org>
                   ` (2 preceding siblings ...)
  2019-03-11  6:53 ` [ruby-core:91759] " matz
@ 2019-03-11  9:54 ` knu
  3 siblings, 0 replies; 4+ messages in thread
From: knu @ 2019-03-11  9:54 UTC (permalink / raw)
  To: ruby-core

Issue #15618 has been updated by knu (Akinori MUSHA).

Status changed from Open to Closed

Merged with [Feature #15618].

----------------------------------------
Feature #15618: Implement Enumerator::Yielder#to_proc
https://bugs.ruby-lang.org/issues/15618#change-77048

* Author: knu (Akinori MUSHA)
* Status: Closed
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
When writing an Enumerator block, you often want to delegate iteration to another method like this:

```ruby
enum = Enumerator.new { |y|
  Dir.glob("*.rb") { |file|
    File.open(file) { |f| f.each_line { |line| y << line } }
  }
}
```

I think this is such a common pattern, but the `{ |var| y << var }` part looks redundant compared to a normal iterator method being able to delegate to another method as simply as follows:

```
  def each(&block)
    @children.each(&block)
  end
```

So, I propose adding #to_proc to Yielder so you can directly pass a yielder object to another method as a block argument.

```ruby
enum = Enumerator.new { |y|
  Dir.glob("*.rb") { |file|
    File.open(file) { |f| f.each_line(&y) }
  }
}
```

Yielder is all about yielding, so I think it's pretty obvious what it means.

---Files--------------------------------
0001-Implement-Enumerator-Yielder-to_proc.patch (3.21 KB)


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

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

end of thread, other threads:[~2019-03-11  9:54 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-15618.20190224155516@ruby-lang.org>
2019-02-24 15:55 ` [ruby-core:91613] [Ruby trunk Feature#15618] Implement Enumerator::Yielder#to_proc knu
2019-02-24 17:16 ` [ruby-core:91614] " shevegen
2019-03-11  6:53 ` [ruby-core:91759] " matz
2019-03-11  9:54 ` [ruby-core:91771] " knu

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).