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

Add slice comprehensions #15

Closed
themeeman opened this issue Nov 3, 2018 · 2 comments
Closed

Add slice comprehensions #15

themeeman opened this issue Nov 3, 2018 · 2 comments
Labels

Comments

@themeeman
Copy link

In python, you can do [i ** 2 for i in range(10)] to iterate through each value in range and square each one and get a list of squares all in one expression. I propose a similar syntax for expr:

[v ** 2 for _, v := range 0..9]

This would return the slice of all the numbers from 0 to 9 squared.
Another thing you could do is a conditional comprehension, for example

[[i, v] for i, v := range [1, 4, 7, 2, 3] if v <= 5]

This would return a [][]int of the numbers and indexes of those numbers where the number is less than or equal to 5. In this case it would be [[0, 1], [1, 4], [3, 2], [4, 3].

This feature would make this package more powerful and greatly increase the potential of it, as currently there is no way to return a slice outside of the range operator, which is limited.

@antonmedv
Copy link
Member

I'm thinking about adding comprehensions too. But I don't like nested comprehensions, as it difficult to read it.
Also I think before adding comprehensions will be cool to think about map/apply functionality and closures/lambdas.

On opposite side, without comprehensions expressions can stay simple. In our project we heavily rely on expr and implemented a lot of useful helpers for iterating or complex login. In expressions just combining them.

@antonmedv
Copy link
Member

Now we have all any one etc builtins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants