February 2018
Intermediate to advanced
350 pages
7h 35m
English
Back in Chapter 2, Getting Started with Functional Programming, in the section First-class and high-order functions, when we introduced lambda functions, we show the definition of Function1:
/** A function that takes 1 argument. */public interface Function1<in P1, out R> : Function<R> { /** Invokes the function with the specified argument. */ public operator fun invoke(p1: P1): R}
The invoke function is an operator, a curious one. The invoke operator can be called without name.
The class Wolf has an invoke operator:
enum class WolfActions { SLEEP, WALK, BITE}class Wolf(val name:String) { operator fun invoke(action: WolfActions) = when (action) { WolfActions.SLEEP -> "$name is sleeping" WolfActions.WALK -> "$name is walking" WolfActions. ...
Read now
Unlock full access