bugla.blogg.se

Range kotlin
Range kotlin









range kotlin

The lambda is inlined so likely there is no real difference in performance. NOTE: the helper functions use flatMap() since there is no flatten() method or extension for Array which is the type received from the vararg.

range kotlin

First, let to see a basic example of a range operator. The range operators can be used with for loops, if conditions or even in when operator. A range can be defined with a start value and an end value with and without inclusion. and the usage for those helpers: val validLetters = sparseListOf('a'.'z', 'A'.'Z') Range Operator in Kotlin is a basic operator that is used to operate over a range. In Kotlin people typically create a helper function to wrap things like this up nicely if you happen to re-use this code a lot: // helper functionsįun sparseListOf(vararg ranges: CharRange): List = ranges.flatMap Or go more lazy as a Sequence (not sure it matters here at all to be lazier): sequenceOf('a'.'z','A'.'Z').flatten() Each + will make a copy of the list: val validLetters = ('a'.'z') + ('A'.'Z')

range kotlin

Or a shorter form (from is to use the plus() + operator of Iterable ( an interface that ranges implement). You can go slightly shorter for a list by using flatten() instead of flatMap(): listOf('a'.'z','A'.'Z').flatten()











Range kotlin