This is the tenth part of the video series on Kotlin language.
Read and watch the other parts here.
1. | Hello Kotlin |
2. | Declaring Variables |
3. | Data Types |
4. | String (Part 1) |
5. | String (Part 1) |
6. | Conditional Expressions |
7. | Functions (Part I) |
8. | Functions (Part II) |
9. | Lambda |
We discuss range operators in Kotlin language.
The examples used in the video are given below
fun main(args:Array<String>){ val range = 1 .. 10 println(range.javaClass)//IntRange val rangeWithStep = 1 .. 10 step 2 println(rangeWithStep.first) println(rangeWithStep.last) println(rangeWithStep.step) val range2 = 1 until 10 val range3 = 10 downTo 1 step 2 println(range3.first) println(range3.last) }
range can be declared using .. operator or using until and downTo keywords with an option incremental step. In the example above range is an instance of IntRange class. So, we can use properties like first, last, step etc., We can also use methods like min(), max(), average(), filter(), map(), reduce() etc.,
fun print(message:String){ val range = 1 .. 10 println(range.max()) println(range.min()) println(range.average()) println(range.filter { num -> num % 2 == 0 }) println(range.map { num -> num * num }) println(range.reduce { total, num -> total + num }) }
You can find the video here.