Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: Go 2: expand power of range for slices/arrays #36816

Closed
pjebs opened this issue Jan 27, 2020 · 7 comments
Closed

proposal: Go 2: expand power of range for slices/arrays #36816

pjebs opened this issue Jan 27, 2020 · 7 comments
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Milestone

Comments

@pjebs
Copy link
Contributor

pjebs commented Jan 27, 2020

This proposal wishes to expand the power of the range keyword when applied to slices and arrays.

Note: If you don't want to use the features, your code will LOOK EXACTLY THE SAME.

Currently:

s := []string{}

for idx, v := range s {}

Proposal:

  1. Iterate forwards (with superfluous +)
for idx, v := range +s {}

*Equivalent:
for idx := 0; idx < len(s); idx++ {
	v := s[idx]
} 
  1. Iterate backwards
for idx, v := range -s {}

*Equivalent:
for idx := len(s) - 1; idx >= 0; idx-- {
	v := s[idx]
} 
  1. Iterate from middle outwards
for idx, v := range ±s {}

*Equivalent:
mid := (len(s) - 1) >> 1
for j := 0; j < len(s); j++ {
	var idx int
	if j%2 == 0 {
		idx = mid - j/2
	} else {
		idx = mid + (j/2 + 1)
	}

	v := s[idx]
}
@gopherbot gopherbot added this to the Proposal milestone Jan 27, 2020
@pjebs
Copy link
Contributor Author

pjebs commented Jan 27, 2020

Additional Notes:

  1. ± is used to start from middle and go to right initially
  2. is used to start from middle and go to the left initially
  3. Iterating from middle outwards has (rare) but important use cases such as in divide and conquer algorithms.
  4. I found use for it in my strconv.ParseComplex PR.
  5. The use case is rare. But when a developer needs it, they will find the character for it, despite it not being readily available on the keyboard.

@ianlancetaylor ianlancetaylor changed the title Proposal: Go2?: expand power of range for slices/arrays proposal: Go 2: expand power of range for slices/arrays Jan 28, 2020
@ianlancetaylor ianlancetaylor added v2 An incompatible library change LanguageChange Suggested changes to the Go language labels Jan 28, 2020
@ianlancetaylor
Copy link
Member

Would you mind filling out the template in https://go.googlesource.com/proposal/+/bd3ac287ccbebb2d12a386f1f1447876dd74b54d/go2-language-changes.md ?

Thanks.

@pjebs
Copy link
Contributor Author

pjebs commented Jan 29, 2020

where do I save it?

@ianlancetaylor
Copy link
Member

Add it as a comment to this issue. Thanks.

@dpinela
Copy link
Contributor

dpinela commented Jan 31, 2020

Iterating from the middle seems like too rare a use case to bake into the language - I've personally never encountered it. Iterating in reverse is something I've needed a few times myself; it may be worth adding, if only to help avoid off-by-one errors.

I think the syntax you're proposing is too cryptic, though. I would rather implement this as a reversed built-in (not a keyword, because that'd be a breaking change) instead. Also, if we had generics plus some way to implement custom iterators that work with range, it wouldn't even have to be in the language itself.

@deanveloper
Copy link

While this is clever, I'm not sure if I like it. Go has very few symbols that aren't universally understood (:= and <- are the only ones off the top of my head) so adding -slice doesn't seem right. I sortof like @dpinela's idea of a reversed(slice) builtin though.

@ianlancetaylor
Copy link
Member

The middle-out range is rarely useful, and the syntax is cryptic. That doesn't seem like a good addition to the language.

How often does one want to iterate backward? It's already possible to do so, in a way that is clear and easy to understand. The single - character seems small for something that will be rarely used.

Also, the - will only be meaningful for slices and arrays, not for maps or channels.

Iterating backward can be done with the usual range, with just one extra line.

    for i := range s {
        v := s[len(s) - 1 - i] // reverse iteration
        ...
    }

This proposal does not seem compelling.

@pjebs pjebs closed this as completed Feb 4, 2020
@golang golang locked and limited conversation to collaborators Feb 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge LanguageChange Suggested changes to the Go language Proposal v2 An incompatible library change
Projects
None yet
Development

No branches or pull requests

5 participants