4 ms·
It's mostly ease of semantics -- in your example you use two layers of map and as a result need to do flatMap instead of just map twice In py, for list/set/dic
by hughesjj 2y ago
It's mostly ease of semantics -- in your example you use two layers of map and as a result need to do flatMap instead of just map twice
In py, for list/set/dictionary/generator comprehensions, the format is always the same and always the same as if you were to do it as a normal nested loop, save for the statement being first instead of last (you can also do filters using normal if statement syntax, these go at the end/after all your loops).
I actually like statement first because it gets to the "meat" of the semantics before the context (which loop etc), but end do the day it's all a bit arbitrary
@ yield, there's literally no difference between Python and kotlin. Python also offers a generator comprehension, which is nice, but it has nothing to do with yield
i_am_a_generator = (
x+1
for x in range(10*100)
)
- Larrikin 2y agoTheres the zip function builtin, which I actually would have preferred oneToTen.zip(oneToTen2) { x, y -> x * y }
- lolinder 2y agoThat's not actually equivalent. The initial version they included produces: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 2, 4, 6, 8, 10, 12, 14, 16, 18 ...] Whereas yours is just: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100]