Understandings in Python and Javascript are just basic?

Looking at the understanding in Python and Javascript, so far I do not see some of the basic functions that I consider the most powerful in understanding languages ​​such as Haskell.

Do they allow things like multiple generators? Or is it just the basic form of a map filter?

If they don't allow multiple generators, I find them pretty disappointing - why don't such things count?

+6
javascript python haskell list-comprehension
source share
4 answers

Python allows you to create multiple generators:

>>> [(x,y,x*y) for x in range(1,5) for y in range(1,5)] [(1, 1, 1), (1, 2, 2), (1, 3, 3), (1, 4, 4), (2, 1, 2), (2, 2, 4), (2, 3, 6), (2, 4, 8), (3, 1, 3), (3, 2, 6), (3, 3, 9), (3, 4, 12), (4, 1, 4), (4, 2, 8), (4, 3, 12), (4, 4, 16)] 

And also limitations:

 >>> [(x,y,x*y) for x in range(1,5) for y in range(1,5) if x*y > 8] [(3, 3, 9), (3, 4, 12), (4, 3, 12), (4, 4, 16)] 

Update : Javascript syntax is similar (result of using javascript shell in firefox):

 var nums = [1, 2, 3, 21, 22, 30]; var s = eval('[[i,j] for each (i in nums) for each (j in [3,4]) if (i%2 == 0)]'); s.toSource(); [[2, 3], [2, 4], [22, 3], [22, 4], [30, 3], [30, 4]] 

(For some reason, something about context material is evaluated in a javascript shell, which requires the eval pointer to have list functions. Javascript inside the <script> does not require this, of course)

+12
source share

Yes, you can have several iterations in the Python list comprehension :

 >>> [(x,y) for x in range(2) for y in range(3)] [(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)] 
+3
source share

Add an if statement too ...

 >>> [(x,y) for x in range(5) for y in range(6) if x % 3 == 0 and y % 2 == 0] [(0, 0), (0, 2), (0, 4), (3, 0), (3, 2), (3, 4)] 
+1
source share

Haskell understands very strong points because Haskell is functional, which is why it is very important for them. Python doesn't work, so it makes sense.

You can do a lot of complex things with understanding in Python, but it quickly becomes difficult to read, thus defeating the whole goal (this means you have to do it differently).

However, as stated here, python allows for multiple generators in understanding.

+1
source share

All Articles