Chilli
04/16/2021, 10:36 PMfor(element in collection) { ... }
🔷 forEach extension
collection.forEach { element -> ... }
React with 🔴 or 🔷 to vote
Feel free to reply and tell why you prefer that optionnanodeath
04/16/2021, 10:40 PMNir
04/16/2021, 10:43 PMfor
gives you break/continue, but forEach
chains far more nicely. So ultimately everyone probably uses both sometimes, makes it harder for the community to settle on one perhapsYoussef Shoaib [MOD]
04/16/2021, 10:43 PMrun { collection.forEach {} }
break: return@run
continue: return@forEach
nanodeath
04/16/2021, 10:44 PMChilli
04/16/2021, 10:44 PMnanodeath
04/16/2021, 10:45 PMYoussef Shoaib [MOD]
04/16/2021, 10:45 PMNir
04/16/2021, 10:45 PMsomething.map { ... }. map { .... }. map { ... }. let { for (element in it) { .... } }
Nir
04/16/2021, 10:45 PMNir
04/16/2021, 10:46 PMbreak
continue
were allowed in lambdas passed to inline
functions we could have solutions with the best of both worldsYoussef Shoaib [MOD]
04/16/2021, 10:46 PMChilli
04/16/2021, 10:47 PMnanodeath
04/16/2021, 10:47 PMNir
04/16/2021, 10:47 PM.forEach
if the collection/sequence is the result of a perhaps non-trivial amount of chainingNir
04/16/2021, 10:48 PMnanodeath
04/16/2021, 10:54 PMYoussef Shoaib [MOD]
04/16/2021, 10:56 PMfor(int i = 0; i < blablabla; i++)
java loop), using for-in is definitely superior to forEachNir
04/16/2021, 10:58 PMNir
04/16/2021, 10:58 PMforEachIndexed
Youssef Shoaib [MOD]
04/16/2021, 11:13 PMfor(i in 0 until count)
then you never write it as (0 until count).forEach { }
. I did explain it horribly sorry, but yeah I just mean if you're looping over an IntRange
then you never use forEachTravis Griggs
04/16/2021, 11:23 PMokarm
04/17/2021, 9:45 AMfor
In a chain? Nullable? Need a quick index? --> forEach(Indexed)
Kotlin style guide encourages the usage of the good ol' for
unless you have a specific need for forEach
.
https://kotlinlang.org/docs/coding-conventions.html#loopsglenkpeterson
04/17/2021, 12:07 PMif
, break
, continue
, and return
statements, like some kind of mad obstacle race that involves as many state changes as possible. Different kinds of collections require different looping constructs - which adds more complexity. None of that has anything to do with why the loop was created in the first place which is to transform the underlying data in a collection (or array).
With functional transformations, all those concerns disappear. Instead of thinking, “Oh, I need to process that stuff, I'll add an inner loop!” I'm thinking “What data structure would have the best performance characteristics for this transformation?”
I can only think about a limited number of things at once. Each unnecessary concern takes precious thought power away from a more important one.
https://github.com/GlenKPeterson/Paguro/wiki/Looping-vs.-Functional-TransformationsNir
04/17/2021, 3:13 PMMichael Böiers
04/17/2021, 4:09 PM