June 2018
Intermediate to advanced
316 pages
6h 34m
English
The best way to explain the difference between declarative and imperative programming styles is to show examples. Let's assume that you need to write a function that retrieves only odd numbers from a list. If you write that code in an imperative style, it might look like this:
val numbers = listOf(1, 2, 3, 4, 5, 6, 7)val odds = ArrayList<Int>()for (i in 0..numbers.lastIndex) { val item = numbers[i] if (item % 2 != 0) { odds.add(item) }}
This code describes the control flow in detail and contains a lot of statements. A statement is an element of imperative programming that describes some control action. Let's rewrite this code using a declarative style:
val numbers = listOf(1, 2, 3, 4, 5, 6, 7)val odds = numbers. ...
Read now
Unlock full access