# Do we need Consumer/Supplier in Kotlin

**URL:** https://discuss.kotlinlang.org/t/do-we-need-consumer-supplier-in-kotlin/5547
**Category:** Support
**Created:** [November 24, 2017, 10:46am UTC](https://discuss.kotlinlang.org/t/do-we-need-consumer-supplier-in-kotlin/5547 "2017-11-24T10:46:29Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![snoobvn](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@snoobvn](https://discuss.kotlinlang.org/u/snoobvn)
#### Post date: [November 24, 2017, 10:46am UTC](https://discuss.kotlinlang.org/t/do-we-need-consumer-supplier-in-kotlin/5547/1 "2017-11-24T10:46:29Z")

</div>

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 ?

---

<div class="post-metadata">

### Author: ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)
#### Post date: [November 24, 2017, 10:53am UTC](https://discuss.kotlinlang.org/t/do-we-need-consumer-supplier-in-kotlin/5547/2 "2017-11-24T10:53:06Z")

</div>

Yes, them are not required.

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

```

---

<div class="post-metadata">

### Author: ![tiagopereirafonseca](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@tiagopereirafonseca](https://discuss.kotlinlang.org/u/tiagopereirafonseca)
#### Post date: [October 30, 2021, 9:57pm UTC](https://discuss.kotlinlang.org/t/do-we-need-consumer-supplier-in-kotlin/5547/3 "2021-10-30T21:57:42Z")

</div>

Required for Stream reuse cases.

To avoid the exception: “java.lang.IllegalStateException: stream has already been operated upon or closed”

---

<div class="post-metadata">

### Author: ![luhtonen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luhtonen/32/6825_2.png) [@luhtonen](https://discuss.kotlinlang.org/u/luhtonen)
#### Post date: [October 31, 2021, 8:26pm UTC](https://discuss.kotlinlang.org/t/do-we-need-consumer-supplier-in-kotlin/5547/4 "2021-10-31T20:26:39Z")

</div>

> [@tiagopereirafonseca](#):
>
> Required for Stream reuse cases.

What do you mean by this? Why do we need to reuse Java Streams in Kotlin?

---

<div class="post-metadata">

### Author: ![tiagopereirafonseca](https://avatars.discourse-cdn.com/v4/letter/t/c37758/32.png) [@tiagopereirafonseca](https://discuss.kotlinlang.org/u/tiagopereirafonseca)
#### Post date: [November 1, 2021, 1:01am UTC](https://discuss.kotlinlang.org/t/do-we-need-consumer-supplier-in-kotlin/5547/5 "2021-11-01T01:01:54Z")

</div>

When it is necessary to use stream in kotlin, reuse it is a option.  
How do you read a file on kotlin?

---

<div class="post-metadata">

### Author: ![broot](https://avatars.discourse-cdn.com/v4/letter/b/a88e57/32.png) [@broot](https://discuss.kotlinlang.org/u/broot)
#### Post date: [November 1, 2021, 3:03am UTC](https://discuss.kotlinlang.org/t/do-we-need-consumer-supplier-in-kotlin/5547/6 "2021-11-01T03:03:19Z")

</div>

I don’t really understand what do you mean. How is `Consumer`/`Supplier` required to reuse a `Stream`?

Also, I think this case is really irrelevant. `Stream` is only available on JVM target and if we are targeting JVM then we have `Consumer`/`Supplier` as well, because they’re part of the Java stdlib. The question is: do we need `Consumer`/`Supplier` interfaces in Kotlin stdlib? I guess no, it would be redundant to function types.

---

<div class="post-metadata">

### Author: ![luhtonen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/luhtonen/32/6825_2.png) [@luhtonen](https://discuss.kotlinlang.org/u/luhtonen)
#### Post date: [November 1, 2021, 8:53am UTC](https://discuss.kotlinlang.org/t/do-we-need-consumer-supplier-in-kotlin/5547/7 "2021-11-01T08:53:26Z")

</div>

> [@tiagopereirafonseca](#):
>
> When it is necessary to use stream in kotlin, reuse it is a option.  
> How do you read a file on kotlin?

Sorry, I’m misunderstood your message. I thought you was talking about Java Stream API.
