Forum: Ruby-core [ruby-trunk - Feature #7994][Open] Make iterators pass an implicit named parameter `iteration` to th

C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 alexeymuranov (Alexey Muranov) (Guest)
on 2013-03-01 16:23
(Received via mailing list)
Issue #7994 has been reported by alexeymuranov (Alexey Muranov).

----------------------------------------
Feature #7994: Make iterators pass an implicit named parameter
`iteration` to the executed block
https://bugs.ruby-lang.org/issues/7994

Author: alexeymuranov (Alexey Muranov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin
I think it would be nice to be able to write something like this:

  items.each do |item|
    unless iteration.first?
      # do something that is not applicable to the first iteration
    end
    # do whatever is to be done to all items
  end
=end
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 alexeymuranov (Alexey Muranov) (Guest)
on 2013-03-01 16:30
(Received via mailing list)
Issue #7994 has been updated by alexeymuranov (Alexey Muranov).


=begin
As a use case, i wanted to create a pagination navigation element on a
web page in ((*Haml*)) template. What i ended up with looks redundant to
me. With the suggested feature, i would be able to do:

  %nav.pagination
    - [page_ranges_before, page_ranges_after].each do |page_ranges|
      - unless iteration.first?
        %span.page.active
          = active_page
      - page_ranges.each do |range|
        - unless iteration.first?
          %span.ellipsis
            ⋯
        - range.each do |page|
          %button.page{ :type  => :submit, :name  => :page, :value =>
page }
            = page
=end

----------------------------------------
Feature #7994: Make iterators pass an implicit named parameter
`iteration` to the executed block
https://bugs.ruby-lang.org/issues/7994#change-37226

Author: alexeymuranov (Alexey Muranov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin
I think it would be nice to be able to write something like this:

  items.each do |item|
    unless iteration.first?
      # do something that is not applicable to the first iteration
    end
    # do whatever is to be done to all items
  end
=end
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2013-03-01 16:32
(Received via mailing list)
On 01/03/13 15:21, alexeymuranov (Alexey Muranov) wrote:
>
> Issue #7994 has been reported by alexeymuranov (Alexey Muranov).

What advantages does this have over:

   items.drop(1).each do |item|

or

   items[1..-1].each do |item|

or to invert it:

   items.first.tap do |item|

?

--
Alex
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 alexeymuranov (Alexey Muranov) (Guest)
on 2013-03-01 17:03
(Received via mailing list)
Issue #7994 has been updated by alexeymuranov (Alexey Muranov).


regularfry (Alex Young) wrote:
>     items[1..-1].each do |item|
>
>  or to invert it:
>
>     items.first.tap do |item|
>
>  ?

Sorry, i didn't understand, how do you plan to use this?
----------------------------------------
Feature #7994: Make iterators pass an implicit named parameter
`iteration` to the executed block
https://bugs.ruby-lang.org/issues/7994#change-37229

Author: alexeymuranov (Alexey Muranov)
Status: Open
Priority: Normal
Assignee:
Category:
Target version:


=begin
I think it would be nice to be able to write something like this:

  items.each do |item|
    unless iteration.first?
      # do something that is not applicable to the first iteration
    end
    # do whatever is to be done to all items
  end
=end
0ec4920185b657a03edf01fff96b4e9b?d=identicon&s=25 matz (Yukihiro Matsumoto) (Guest)
on 2013-03-01 19:52
(Received via mailing list)
Issue #7994 has been updated by matz (Yukihiro Matsumoto).

Status changed from Open to Rejected

Nice idea.

But I reject the idea for Ruby for two reasons:

* I personally don't like implicit parameters
* That might cause incompatibility by conflicting with existing local
variable name

Maybe good for a new language without existing set of programs.

Matz.

----------------------------------------
Feature #7994: Make iterators pass an implicit named parameter
`iteration` to the executed block
https://bugs.ruby-lang.org/issues/7994#change-37235

Author: alexeymuranov (Alexey Muranov)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


=begin
I think it would be nice to be able to write something like this:

  items.each do |item|
    unless iteration.first?
      # do something that is not applicable to the first iteration
    end
    # do whatever is to be done to all items
  end
=end
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 alexeymuranov (Alexey Muranov) (Guest)
on 2013-03-01 20:28
(Received via mailing list)
Issue #7994 has been updated by alexeymuranov (Alexey Muranov).


=begin
Ok, then i make a last try: what about adding it in the end of parameter
list as an optional parameter?

 items.each do |item, iteration|
   unless iteration.first?
     # do something that is not applicable to the first iteration
   end
   # do whatever is to be done to all items
 end
=end

----------------------------------------
Feature #7994: Make iterators pass an implicit named parameter
`iteration` to the executed block
https://bugs.ruby-lang.org/issues/7994#change-37236

Author: alexeymuranov (Alexey Muranov)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


=begin
I think it would be nice to be able to write something like this:

  items.each do |item|
    unless iteration.first?
      # do something that is not applicable to the first iteration
    end
    # do whatever is to be done to all items
  end
=end
0e610136db92027148906c92d57fdb36?d=identicon&s=25 marcandre (Marc-Andre Lafortune) (Guest)
on 2013-03-01 20:35
(Received via mailing list)
Issue #7994 has been updated by marcandre (Marc-Andre Lafortune).


Please think about the number of incompatibilities this would bring...
not all enumerables are sequences of single items.

Just use `each_with_index` and `index.zero?` instead of
`iteration.first?`
----------------------------------------
Feature #7994: Make iterators pass an implicit named parameter
`iteration` to the executed block
https://bugs.ruby-lang.org/issues/7994#change-37237

Author: alexeymuranov (Alexey Muranov)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


=begin
I think it would be nice to be able to write something like this:

  items.each do |item|
    unless iteration.first?
      # do something that is not applicable to the first iteration
    end
    # do whatever is to be done to all items
  end
=end
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 alexeymuranov (Alexey Muranov) (Guest)
on 2013-03-01 20:46
(Received via mailing list)
Issue #7994 has been updated by alexeymuranov (Alexey Muranov).


Marc-Andre, thanks for the idea, i did not realize that
`each_with_index` works with all enumerables.

However i was hoping that something like `unless iteration.first?` could
be some day optimized away by a compiler to not check on every
iteration.  I also do not see right away an incompatibility introduced
by my second proposal (about the original one you are right).
----------------------------------------
Feature #7994: Make iterators pass an implicit named parameter
`iteration` to the executed block
https://bugs.ruby-lang.org/issues/7994#change-37238

Author: alexeymuranov (Alexey Muranov)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


=begin
I think it would be nice to be able to write something like this:

  items.each do |item|
    unless iteration.first?
      # do something that is not applicable to the first iteration
    end
    # do whatever is to be done to all items
  end
=end
0e610136db92027148906c92d57fdb36?d=identicon&s=25 marcandre (Marc-Andre Lafortune) (Guest)
on 2013-03-01 21:11
(Received via mailing list)
Issue #7994 has been updated by marcandre (Marc-Andre Lafortune).


> I also do not see right away an incompatibility introduced by my second proposal
(about the original one you are right).

  #before
  [[1,2]].each{|a, b| p a, b} # => 1, then 2
  # after
  [[1,2]].each{|a, b| p a, b} # => [1, 2], then<#your iteration object>

BTW, all hashes would be affected like this!
----------------------------------------
Feature #7994: Make iterators pass an implicit named parameter
`iteration` to the executed block
https://bugs.ruby-lang.org/issues/7994#change-37240

Author: alexeymuranov (Alexey Muranov)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


=begin
I think it would be nice to be able to write something like this:

  items.each do |item|
    unless iteration.first?
      # do something that is not applicable to the first iteration
    end
    # do whatever is to be done to all items
  end
=end
C4e88907313843cf07f6d85ba8162120?d=identicon&s=25 alexeymuranov (Alexey Muranov) (Guest)
on 2013-03-01 21:31
(Received via mailing list)
Issue #7994 has been updated by alexeymuranov (Alexey Muranov).


Ok, i see.
----------------------------------------
Feature #7994: Make iterators pass an implicit named parameter
`iteration` to the executed block
https://bugs.ruby-lang.org/issues/7994#change-37241

Author: alexeymuranov (Alexey Muranov)
Status: Rejected
Priority: Normal
Assignee:
Category:
Target version:


=begin
I think it would be nice to be able to write something like this:

  items.each do |item|
    unless iteration.first?
      # do something that is not applicable to the first iteration
    end
    # do whatever is to be done to all items
  end
=end
This topic is locked and can not be replied to.