v icon indicating copy to clipboard operation
v copied to clipboard

Python style List comprehensions

Open joe-conigliaro opened this issue 7 years ago • 30 comments

Have you thought supporting list comprehensions like in python?

joe-conigliaro avatar Jun 20 '19 04:06 joe-conigliaro

I think it goes against V's philosophy of "only one way to do it". Do you have an example of what you miss?

medvednikov avatar Jun 20 '19 07:06 medvednikov

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.

joe-conigliaro avatar Jun 20 '19 08:06 joe-conigliaro

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.

medvednikov avatar Jun 20 '19 08:06 medvednikov

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]

joe-conigliaro avatar Jun 20 '19 09:06 joe-conigliaro

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(_))

medvednikov avatar Jun 20 '19 09:06 medvednikov

This is an upcoming feature, still not 100% decided on the syntax. But it's going to be something like this.

medvednikov avatar Jun 20 '19 09:06 medvednikov

Awesome I like that! nice one!

joe-conigliaro avatar Jun 20 '19 09:06 joe-conigliaro

Cool :) I think it's a bit simpler and more readable than list comprehensions. I personally always found them a bit confusing.

medvednikov avatar Jun 20 '19 09:06 medvednikov

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)

medvednikov avatar Jun 20 '19 09:06 medvednikov

ahh yeah we already have in for arrays :)

joe-conigliaro avatar Jun 20 '19 09:06 joe-conigliaro

The even numbers example gets it wrong. In python the output of the comprehension would be [False, True, False, True,...]

Just my 2 cents

sonovice avatar Jun 20 '19 09:06 sonovice

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

joe-conigliaro avatar Jun 20 '19 09:06 joe-conigliaro

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?

wilberton avatar Jun 20 '19 10:06 wilberton

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.

ntrel avatar Jun 20 '19 10:06 ntrel

does (_ in arrayB) do a linear search

Yes, I don't think it's good to encourage this with an operator, see #277.

ntrel avatar Jun 20 '19 10:06 ntrel

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 :)

medvednikov avatar Jun 20 '19 11:06 medvednikov

[.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.

ntrel avatar Jun 20 '19 11:06 ntrel

How does in encourage linear search? How would you avoid linear without in?

medvednikov avatar Jun 20 '19 11:06 medvednikov

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.

Undumendil avatar Jun 20 '19 11:06 Undumendil

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.

medvednikov avatar Jun 20 '19 11:06 medvednikov