Hullaballoonatic
04/17/2019, 7:51 PMfor loop vs forEach lambda ?ghedeon
04/17/2019, 7:54 PMforEach is closer to FP and chained functions.Luke
04/17/2019, 7:58 PMfor(i in min..max) than (min..max).forEach. Nevertheless, I think it’s up to your liking more than actual conventionsHullaballoonatic
04/17/2019, 8:07 PMfor (i in 0..lastIndex) { foo++ } -1
(i in 0..lastIndex).forEach { foo++ } +1Hullaballoonatic
04/17/2019, 8:08 PMrakeeb
04/17/2019, 8:17 PMbreak in a forEach (you could but you need to run it inside a run block). for me if you have a condition where you need to break out i’d go with for otherwise forEachgildor
04/18/2019, 1:08 AMBut this is invalid Kotlin code. Valid version would be just:(i in 0..lastIndex).forEach { foo++ }
(0..lastIndex).forEach { foo++ }gildor
04/18/2019, 1:09 AMlastIndex probably you have some collection (even some custom container, you can easily add extension for iterator), and in this case I don’t think that you need range for this, just .forEach() or for (i in collection)Hullaballoonatic
04/18/2019, 1:10 AMHullaballoonatic
04/18/2019, 1:11 AMgildor
04/18/2019, 1:13 AMrepeat(max) { }gildor
04/18/2019, 1:14 AMbreak, so for or while loops are the only choice in many casesgildor
04/18/2019, 2:41 AMi’m willing to write lambda’s in a single lineIn case of
for you don’t have lambda, so because of this you can omit curly braces, so +1 to for
for (i in 0..lastIndex) foo++kenkyee
10/29/2021, 2:20 PM