concept con in category functional programming

This is an excerpt from Manning's book Functional Programming with Kotlin.
Let’s look first at the definition of the data type, which begins with the keywords
sealed class. Usually, we introduce a data type with theclasskeyword. Here we’re declaring aclass, calledList, with no instance methods on it. Addingsealedin front of the class declaration means that all implementations must be declared in this file. A sealed class is also abstract by default, so can not be instantiated by itself. There are two implementations, or data constructors, ofListdeclared next, to represent the two possible forms aListcan take. As the figure shows, aListcan be empty, denoted by the data constructorNil, or it can be nonempty, denoted by the data constructorCons(traditionally short for construct). A nonempty list consists of an initial element,head, followed by aList(possibly empty) of remaining elements (thetail).Figure 3.1. The singly linked list, each tail links to the next list element.
![]()
Just as functions can be polymorphic, data types can be as well, and by adding the type parameter
<out A>aftersealed class Listand then using thatAparameter inside of theConsdata constructor, we declare theListdata type to be polymorphic in the type of elements it contains, which means we can use this same definition for a list ofIntelements (denotedList<Int>),Doubleelements (denotedList<Double>),Stringelements(List<String>), and so on (theoutindicates that the type parameterAis covariant—see sidebar “More about variance” for more information).
This type looks identical to our
Listtype, except that theConsdata constructor takes explicit thunks (() -> Aand() -> Stream<A>) instead of regular strict values. If we wish to examine or traverse theStream, we need to force these thunks as we did earlier in our definition ofif2. For instance, here’s an extenstion function to optionally extract the head of aStream:As we are adding behaviour to a
Streaminstance, we have thehandtvalues available inthiswhen smart-cast toConsin thewhenconstruct. Note that we have to forcehexplicitly viah(), but other than that, the code works the same way as it would forList. But this ability ofStreamto evaluate only the portion actually demanded (we don’t evaluate the tail of theCons) is very useful, as we’ll see in the following section.