FAQ
Is there a simple way to reverse range over any rangeable item?

for k, v := reverse range fooBar {
         // blah
}

For arrays/slices I know I can just as easily do a for loop counting
down, but for other types that can be ranged over, it's not as obvious
what is the easy equivalent is. Do I need to rewrite my own copy of
Len/Less/Swap for every type I want to use and sort of build my own
Reverse sort.Interface? (i.e.
http://golang.org/pkg/sort/#example_Interface_reverse )

   - Robert

--

Search Discussions

  • Rémy Oudompheng at Oct 24, 2012 at 8:04 pm

    On 2012/10/24 Robert Snedegar wrote:
    Is there a simple way to reverse range over any rangeable item?

    for k, v := reverse range fooBar {
    // blah
    }

    For arrays/slices I know I can just as easily do a for loop counting
    down, but for other types that can be ranged over, it's not as obvious
    what is the easy equivalent is.
    What are these "other types" you are thinking of?

    Rémy.

    --
  • Matt Kane's Brain at Oct 24, 2012 at 8:10 pm
    Strings you can do just fine as long as you convert them to []rune
    instead of []byte
    Maps have no defined order.
    Channels in reverse order? Violates causality.

    On Wed, Oct 24, 2012 at 4:04 PM, Rémy Oudompheng
    wrote:
    For arrays/slices I know I can just as easily do a for loop counting
    down, but for other types that can be ranged over, it's not as obvious
    what is the easy equivalent is.

    --
    matt kane's brain
    http://hydrogenproject.com

    --
  • Sanjay at Oct 24, 2012 at 8:14 pm
    You don't need to convert to []rune. You can use
    http://tip.golang.org/pkg/unicode/utf8/#DecodeLastRuneInString and slicing
    to do this. See the example for that function.

    Sanjay

    --
  • 小菜 at Sep 4, 2014 at 2:00 am
    I want to know too!


    。。。

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Sep 4, 2014 at 9:02 am
    Nope. Write a loop.
    On Thursday, 4 September 2014 12:00:06 UTC+10, 小菜 wrote:

    I want to know too!



    。。。
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Egon at Sep 4, 2014 at 7:05 am

    On Wednesday, 24 October 2012 23:02:48 UTC+3, Robert wrote:
    Is there a simple way to reverse range over any rangeable item?

    for k, v := reverse range fooBar {
    // blah
    }
    It makes only sense for arrays/slices...

    for i := len(foobar) -1; i >= 0; i-- {
        v := foobar[i]
        // blah
    }

    ... or

    for i := range foobar {
        v := foobar[len(foobar) - i - 1]
        // blah
    }

    It does not make sense to provide such operator.

    For arrays/slices I know I can just as easily do a for loop counting
    down, but for other types that can be ranged over, it's not as obvious
    what is the easy equivalent is. Do I need to rewrite my own copy of
    Len/Less/Swap for every type I want to use and sort of build my own
    Reverse sort.Interface? (i.e.
    http://golang.org/pkg/sort/#example_Interface_reverse )

    - Robert
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rjeczalik at Sep 4, 2014 at 9:27 am

    On 4 September 2014 09:05, egon wrote:

    It does not make sense to provide such operator.
    Theoretically it may, especially when 'for _, v := range ...' does not
    perform bound checks, and hand-crafted loops do.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Egon at Sep 4, 2014 at 9:31 am

    On Thursday, 4 September 2014 12:28:28 UTC+3, rjeczalik wrote:
    On 4 September 2014 09:05, egon <egon...@gmail.com <javascript:>> wrote:

    It does not make sense to provide such operator.
    Theoretically it may, especially when 'for _, v := range ...' does not
    perform bound checks, and hand-crafted loops do.
    I'm guessing you'll get a bigger performance hit from traversing memory
    backwards than from bounds checks. But it's just a guess, I don't have data
    to back that up.

    + egon

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Dave Cheney at Sep 4, 2014 at 9:37 am
    Unless all you are doing is walking backwards through the slice, any work you do on a element in the slice will probably eclipse any bounds checking cost -- which the processor has probably eliminated via branch prediction.


    On 4 Sep 2014, at 19:31, egon wrote:


    On Thursday, 4 September 2014 12:28:28 UTC+3, rjeczalik wrote:
    On 4 September 2014 09:05, egon wrote:
    It does not make sense to provide such operator.
    Theoretically it may, especially when 'for _, v := range ...' does not perform bound checks, and hand-crafted loops do.
    I'm guessing you'll get a bigger performance hit from traversing memory backwards than from bounds checks. But it's just a guess, I don't have data to back that up.

    + egon
    --
    You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
    To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/8POFxbr_bAg/unsubscribe.
    To unsubscribe from this group and all its topics, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jan Mercl at Sep 4, 2014 at 9:36 am

    On Thu, Sep 4, 2014 at 11:27 AM, rjeczalik wrote:
    Theoretically it may, especially when 'for _, v := range ...' does not
    perform bound checks, and hand-crafted loops do.
    The language specification has no guarantees about bounds check in a
    for statement. The implementation is free to remove them when
    possible. Hand-crafted loops may not apply always, but often they can.

    -j

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Lewis Dackam at Sep 5, 2014 at 1:02 am
    You could do it elegantly with the defer keyword.

    package main

    import "fmt"

    func main() {
    fmt.Println("Reverse a string with a for range")
    str2 := "string2"
    for index, _ := range str2 {
    defer fmt.Printf("%c", str2[index])
    }
    }

    http://play.golang.org/p/4zE2NmRjFL
    On Wednesday, October 24, 2012 4:02:48 PM UTC-4, Robert wrote:

    Is there a simple way to reverse range over any rangeable item?

    for k, v := reverse range fooBar {
    // blah
    }

    For arrays/slices I know I can just as easily do a for loop counting
    down, but for other types that can be ranged over, it's not as obvious
    what is the easy equivalent is. Do I need to rewrite my own copy of
    Len/Less/Swap for every type I want to use and sort of build my own
    Reverse sort.Interface? (i.e.
    http://golang.org/pkg/sort/#example_Interface_reverse )

    - Robert
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rob Pike at Sep 5, 2014 at 1:16 am
    I snorted.

    -rob

    On Thu, Sep 4, 2014 at 6:01 PM, Lewis Dackam wrote:
    You could do it elegantly with the defer keyword.

    package main

    import "fmt"

    func main() {
    fmt.Println("Reverse a string with a for range")
    str2 := "string2"
    for index, _ := range str2 {
    defer fmt.Printf("%c", str2[index])
    }
    }

    http://play.golang.org/p/4zE2NmRjFL
    On Wednesday, October 24, 2012 4:02:48 PM UTC-4, Robert wrote:

    Is there a simple way to reverse range over any rangeable item?

    for k, v := reverse range fooBar {
    // blah
    }

    For arrays/slices I know I can just as easily do a for loop counting
    down, but for other types that can be ranged over, it's not as obvious
    what is the easy equivalent is. Do I need to rewrite my own copy of
    Len/Less/Swap for every type I want to use and sort of build my own
    Reverse sort.Interface? (i.e.
    http://golang.org/pkg/sort/#example_Interface_reverse )

    - Robert
    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Ian Lance Taylor at Sep 5, 2014 at 2:45 am

    On Thu, Sep 4, 2014 at 6:01 PM, Lewis Dackam wrote:
    You could do it elegantly with the defer keyword.
    Nice technique, and easy to use a named result to gather the
    characters.

    http://play.golang.org/p/0IYOHrfkUh

    Ian

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rob Pike at Sep 5, 2014 at 8:04 pm
    There's always the LISP way, updated for Unicode.

    http://play.golang.org/p/Yx8ZU87N6m

    The same method without recursion.

    http://play.golang.org/p/ntBtXHmwWK

    But you wanted a range loop.

    http://play.golang.org/p/8yy7CQn5l3

    And now we have diverged so far from the original question that we can
    safely ignore it.

    -rob

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Adonovan at Sep 5, 2014 at 8:14 pm

    On Thursday, 4 September 2014 22:46:28 UTC-4, Ian Lance Taylor wrote:
    On Thu, Sep 4, 2014 at 6:01 PM, Lewis Dackam <lewis....@gmail.com
    <javascript:>> wrote:
    You could do it elegantly with the defer keyword.
    Nice technique, and easy to use a named result to gather the
    characters.

    http://play.golang.org/p/0IYOHrfkUh

    Ian
    The efficiency of this program troubles me... that 'return' statement is
    too efficient.
    Fortunately we can do without it: http://play.golang.org/p/I-I2WzrXPG

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Rob Pike at Sep 5, 2014 at 8:21 pm
    Not Unicode-safe. Bad.

    -rob

    On Fri, Sep 5, 2014 at 1:14 PM, wrote:

    On Thursday, 4 September 2014 22:46:28 UTC-4, Ian Lance Taylor wrote:
    On Thu, Sep 4, 2014 at 6:01 PM, Lewis Dackam wrote:

    You could do it elegantly with the defer keyword.
    Nice technique, and easy to use a named result to gather the
    characters.

    http://play.golang.org/p/0IYOHrfkUh

    Ian

    The efficiency of this program troubles me... that 'return' statement is
    too efficient.
    Fortunately we can do without it: http://play.golang.org/p/I-I2WzrXPG

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedOct 24, '12 at 8:02p
activeSep 5, '14 at 8:21p
posts17
users13
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase