-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Python style List comprehensions #283
Comments
I think it goes against V's philosophy of "only one way to do it". Do you have an example of what you miss? |
It's not that I miss them that much it's just they are kind of cool. You can really cut down on code verbosity turn a 5-6liner into one line. But v is similar to go in that i's a verbose language. |
Well it's a bit less verbose than Go, but yes, saving keystrokes is not its goal. Can you post a simple example of what you'd like to do? Perhaps it can be done with the upcoming map/filter functions. |
Here are some examples Get even numbers from array
Find common numbers in arrays:
|
|
This is an upcoming feature, still not 100% decided on the syntax. But it's going to be something like this. |
Awesome I like that! nice one! |
Cool :) I think it's a bit simpler and more readable than list comprehensions. I personally always found them a bit confusing. |
Actually the second one is going to be
|
ahh yeah we already have in for arrays :) |
The even numbers example gets it wrong. In python the output of the comprehension would be [False, True, False, True,...] Just my 2 cents |
Ohh yeah it should have been
Thanks for pointing it out |
Out of interest, how is the 'in' operator implemented? |
Filter doesn't need to create an array, allocating is less efficient. Returning an iterator allows to compute elements as requested, and not all elements may be needed depending on later runtime tests, so this also can be more efficient. This is what D does for filter.
That lambda syntax is not very general, it doesn't allow referring to the first argument more than once, or multiple lambda arguments. |
Yes, I don't think it's good to encourage this with an operator, see #277. |
I saw your concern about O(n) I really want to have |
|
How does |
You could store a hash table or binary tree with element hashes for every list. It would be reasonable to have a special type for that since it requires additional memory. |
Hash table with fewer than ~100 elements is always going to be slower than linear search. I tested it. Well depends on the algorithm. If you have an array, you search an array, if you have a map, you search a map. I don't think having |
Lambdas are needed for all kinds of generic algorithms, please don't make these two special cases.
Why is it needed, many other high performance languages use functions for map and filter. Making algorithms return lazy iterators is much better than hoping the compiler can optimize away the allocations (and unnecessary possible calculations for elements not read). That's the explicit, reliable way to write V code, otherwise the programmer has to be an expert on implementation optimizations to know whether the code will do what they intended. |
Because I want to keep the language simple, and iterators are not simple. |
But I do see your points, I'll think about this. I can't really focus on this right now. I have the big release in 2 days :) |
Using linear search is fine. Building it into the language with special short syntax encourages programmers to reach for that instead of considering what other functions they might use. |
Ok, I can agree with that. |
interface Iterable<E> {
next() ?E
} Seems pretty simple.
Thanks, good luck with the release :-) |
@medvednikov what about numbers := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evenNumbers := for num in numbers {
if num%2 == 0 {
num
} else {
continue
}
} The numbers := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evenNumbers := for num in numbers {
if num%2 == 0 {
num
}
} |
@olegmisar Yeah, maybe. Looks good to me. |
The complexity is not in the interface but in the implementation. Specially if you want easy to implement generators, i.e. |
@aguspiza The beauty of iterators is that they are just structs with data and a next method, so if those features are well designed and safe then iterators automatically are too. |
Have you thought supporting list comprehensions like in python?
The text was updated successfully, but these errors were encountered: