Do we need Consumer/Supplier in Kotlin


#1

I am new to FP, In Java 8, we have consumer/supplier like this:

fun main(args: Array<String>) {
    randomProvier(
            Consumer {
                numberPrinter(Supplier {
                    it
                })
            }
    )
    //Or this
    numberPrinter(Supplier {
        randomProvier(Consumer { })
    })
}

fun randomProvier(c: Consumer<Double>): Double {
    var n = Math.random()
    c.accept(n)
    return n
}

fun numberPrinter(s: Supplier<Double>) {
    println(s.get())
}

But in Kotlin we can do just like this:

fun main(args: Array<String>) {
    randomProvier { numberPrinter {it} }
    //Or this
    numberPrinter { randomProvier{} }
}

fun randomProvier(c: (Double) -> Unit) : Double{
    var n =Math.random()
    c(n)
    return n
}

fun numberPrinter(s: () -> Double){
    println(s())
}

Does that mean that we don’t need Consumer/Supplier anymore ?


#2

Yes, them are not required.

typealias Producer<T> = () -> T
typealias Consumer<T> = (T) -> Unit