concept integer in category kotlin

appears as: integers, n integer, integer, integers, integer
The Joy of Kotlin

This is an excerpt from Manning's book The Joy of Kotlin.

One way to do this is through abstraction. Take the example of a program that finds all elements in a list of integers that are multiples of a given one and takes the maximum of them. In traditional programming, you could do this with an indexed loop as in the following example (bugs are intentional):

Figures 11.1 through 11.7 illustrate insertion of integers 1 through 7 into an initially empty tree. This is the worst possible case, where elements are inserted in order. If using an ordinary binary tree, this would result in a totally unbalanced tree. Figure 11.1 shows the insertion of element 1 into the empty tree. Because you’re inserting into an empty tree, the initial color is red. Once the element is inserted, the root is blackened.

Figure 11.1 Step 1: insertion of integer 1 into an initially empty tree

Fig11_01.png
Listing 12.5 A complete program, from input to output
fun main(args: Array<String>) {

    val input = ConsoleReader()    #1  

    val rString = input.readString("Enter your name:")    #2  
                                    .map { t -> t.first }

    val nameMessage = rString.map { "Hello, $it!" }    #3  

    nameMessage.forEach(::println, onFailure =
                            { println(it.message)})    #4  

    val rInt = input.readInt("Enter your age:").map  { t -> t.first }

    val ageMessage = rInt.map { "You look younger than $it!" }

    ageMessage.forEach(::println, onFailure =    #5  
      { println("Invalid age. Please enter an integer")})
}

#1   Creates the reader
#2   Calls readString with a user prompt and returns a Result<Tuple<String, Input>>, which is mapped to produce a Result<String>
#3   The business part of the program (what the program is supposed to do from the user’s point of view). It can be functionally pure.
#4   Applies the pattern from the previous section to either the result or an error message
#5   Prints a different message than the one in the exception