3 ms·
Hmm, I actually prefer Kotlin's version, but maybe that's just because it's what I'm used to: (0..10).flatMap { x -> (0..10).map { y -> x * y } } The flow o
by lolinder 2y ago
Hmm, I actually prefer Kotlin's version, but maybe that's just because it's what I'm used to:
(0..10).flatMap { x -> (0..10).map { y -> x * y } }
The flow of the data is more intuitive for me because you don't use variables before they're defined.
> Especially the yield keyword, such a wonderful way to write your own iterators.
Maybe I'm missing something about `yield` in Python—can it do something that Kotlin generators can't?
- hughesjj 2y agoIt'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]