Python style List comprehensions
Have you thought supporting list comprehensions like in python?
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
numbers := [1, 2 , 3, 4, 5, 6, 7, 8, 9, 10]
evenNumbers := [num%2 == 0 for num in numbers]
// Output: [2, 4, 6, 8, 10]
// Correction (thanks sonovice), Should be:
numbers := [1, 2 , 3, 4, 5, 6, 7, 8, 9, 10]
evenNumbers := [num for num in numbers if num%2 == 0]
// Output: [2, 4, 6, 8, 10]
Find common numbers in arrays:
arrayA = [1, 2, 3, 4, 5]
arrayB = [3, 4, 5, 6, 7]
common := [a for a in arrayA for b in arrayB if a == b]
// Output: [3, 4, 5]
even_numbers := [n for num in numbers if n % 2 == 0]
==>
even_numbers := numbers.filter(_ % 2 == 0)
common := [a for a in arrayA for b in arrayB if a == b]
==>
common := arrayA.filter(arrayB.contains(_))
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
common := [a for a in arrayA for b in arrayB if a == b]
==>
common := arrayA.filter(_ in arrayB)
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
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
numbers := [1, 2 , 3, 4, 5, 6, 7, 8, 9, 10]
evenNumbers := [num for num in numbers if num%2 == 0]
// Output: [2, 4, 6, 8, 10]
Thanks for pointing it out
Out of interest, how is the 'in' operator implemented?
does arrayA.filter(_ in arrayB) do a linear search through arrayB for every element in arrayA?
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.
even_numbers := numbers.filter(_ % 2 == 0)
That lambda syntax is not very general, it doesn't allow referring to the first argument more than once, or multiple lambda arguments. _ as the only clue in an expression that it's a lambda is probably unique language syntax. The argument doesn't always come first: 1 / _.
I had thought of fn e! * e or fn x! + y! for very short general syntax, other sigils could work too. But we also need multiple statement function literals.
does (_ in arrayB) do a linear search
Yes, I don't think it's good to encourage this with an operator, see #277.
map and filter will be special methods handled by the compiler. This will allow this syntax and is needed anyway to optimize them.
I saw your concern about O(n) in, but it's pretty clear that checking whether an array contains an element is O(n).
I really want to have if p.token in [.plus, .minus, .div] { that's why I want in to exist :)
[.plus, .minus, .div].contains(p.token) does the job fine, and doesn't encourage programmers to choose a linear search by having special language syntax for it. It's not a common operation.
How does in encourage linear search? How would you avoid linear without in?
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 in forces you to use arrays where maps would be more efficient.