concept input in category kotlin

This is an excerpt from Manning's book Functional Programming with Kotlin.
Formalizing the notion of purity this way gives insight into why functional programs are often more modular. Modular programs consist of components that can be understood and reused independently of the whole, such that the meaning of the whole depends only on the meaning of the components and the rules governing their composition; that is, they are composable. A pure function is modular and composable because it separates the logic of the computation itself from “what to do with the result” and “how to obtain the input”; it’s a black box. Input is obtained in exactly one way: via the argument(s) to the function. And the output is simply computed and returned. By keeping each of these concerns separate, the logic of the computation is more reusable; we may reuse the logic wherever we want without worrying about whether the side effect being done with the result or the side effect requesting the input are appropriate in all contexts. We saw this in the
buyCoffeeexample—by eliminating the side effect of payment processing being done with the output, we were more easily able to reuse the logic of the function, both for purposes of testing and for purposes of further composition (like when we wrotebuyCoffeesandcoalesce).
What have we done here? We’ve conjured up a type called
Parserwhich is parameterized on a single parameter indicating the result type of theParser. That is, running a parser shouldn’t simply yield a yes/no response—if it succeeds, we want to get a result that has some useful type, and if it fails, we expect information about the failure. Thechar('a')parser will succeed only if the input is exactly the character'a'and it will return that same character'a'as its result.

This is an excerpt from Manning's book The Joy of Kotlin.
The first thing you need to do before even starting coding (whether it be the main program or the tests) is to think about the problem in terms of the properties that should be verified when considering the input and the output of the function. You can immediately see that a function with no side effects is much easier to deal with in that case.
This is neat and safe. No bad things can happen and no exceptions can be thrown. This is the beauty of programming with pure functions: you have a program that always works, whatever data you use as input. But the question is, how can you use this result? Suppose you want to display the result on the console—how can you do this?
You could write a concrete implementation for this interface, but first you’ll write an abstract one (because you might want to read data from some other source, such as a file). You’ll put the common code in an abstract class and extend it for each type of input. The following listing shows this implementation.
Listing 12.3 The
AbstractReaderimplementationabstract class AbstractReader ( private val reader: BufferedReader): Input { #1 override fun readString(): Result<Pair<String, Input>> = try { #2 reader.readLine().let { when { it.isEmpty() -> Result() else -> Result(Pair(it, this)) } } } catch (e: Exception) { Result.failure(e) } override fun readInt(): Result<Pair<Int, Input>> = try { reader.readLine().let { when { it.isEmpty() -> Result() else -> Result(Pair(it.toInt(), this)) } } } catch (e: Exception) { Result.failure(e) } override fun close(): Unit = reader.close() #3 } #1 Builds this class with a reader, allowing for different sources of input #2 Reads a line from the reader and returns a Result.Empty if the line is empty, a Result.Success if some data is obtained, or a Result.Failure if something goes wrong #3 Delegates to the close function of the BufferedReader