# Is there a way to use actual suspended function in \`buildSequence\`?

**URL:** https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893
**Category:** Libraries
**Created:** [December 20, 2017, 3:29pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893 "2017-12-20T15:29:52Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)
#### Post date: [December 20, 2017, 3:29pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893/1 "2017-12-20T15:29:53Z")

</div>

As `buildSequence` accepts suspended function as argument, I would expect something like this to work:

```kotlin
buildSequence {
    yield( next())
}

```

Here `next` is suspended function defined somewhere else. But compiler gives error about restricted coroutine scope. I do not fully understand, what does this message means so I wander if it is possible to actually use suspended function in this block. Of course, I can always wrap it in `runBlocking`, but in this case, I do not see any reason to make internal function suspended.  
In all of the examples I have encountered, `buildSequence` never uses suspended code.

---

<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: [December 20, 2017, 3:52pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893/2 "2017-12-20T15:52:36Z")

</div>

Hope this helps:

> <https://github.com/Kotlin/coroutines-examples/blob/master/kotlin-coroutines-informal.md#restricted-suspension>

[https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/-restricts-suspension/index.html](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/-restricts-suspension/index.html)

---

<div class="post-metadata">

### Author: ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)
#### Post date: [December 20, 2017, 4:03pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893/3 "2017-12-20T16:03:44Z")

</div>

So, correct me if I am wrong, `buildSequence` does not actually support coroutines and uses suspended notation only to add some syntactic sugar to `yield`. At least that is what I understood, since the whole concept is rather hard to understand at once.  
But it still makes sense to add possibility for lazy sequences with actual suspended producers.

---

<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: [December 20, 2017, 4:26pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893/4 "2017-12-20T16:26:20Z")

</div>

> [@darksnake](#):
>
> At least that is what I understood, since the whole concept is rather hard to understand at once.

Yes, I agree.

> [@darksnake](#):
>
> But it still makes sense to add possibility for lazy sequences with actual suspended producers.

I suppose your proposal overlap the `produce`r.

[https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/produce.html](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/produce.html)

_`map`_/_`reduce`_ operators are on the way…

---

<div class="post-metadata">

### Author: ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)
#### Post date: [December 20, 2017, 5:06pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893/5 "2017-12-20T17:06:22Z")

</div>

Tried it just now, and it does not work as expected. The problem is that `ReceiveChannel` relies a lot on `consumeEach` method. This methods consumes only those values which are already in the channel. If I am doing something like:

```kotlin
val channel = produce{
  send(next())
}

channel.takeWhile { ... }.toList()

```

it produces one element.

---

<div class="post-metadata">

### Author: ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)
#### Post date: [December 20, 2017, 5:11pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893/6 "2017-12-20T17:11:21Z")

</div>

My mistake. Should have done

```kotlin
produce {
            while(true) {
                sendBlocking(next())
            }
        }

```

---

<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: [December 20, 2017, 9:10pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893/7 "2017-12-20T21:10:22Z")

</div>

`sendBlocking` is not appropriate here, `send` is enought.

---

<div class="post-metadata">

### Author: ![fgaule](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fgaule/32/4255_2.png) [@fgaule](https://discuss.kotlinlang.org/u/fgaule)
#### Post date: [December 27, 2017, 3:55pm UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893/8 "2017-12-27T15:55:57Z")

</div>

_suspendingSequence_ seems to be very interesting but its only available on examples module. Is there any reason to be only there?

---

<div class="post-metadata">

### Author: ![ilya.gorbunov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/ilya.gorbunov/32/1645_2.png) [@ilya.gorbunov](https://discuss.kotlinlang.org/u/ilya.gorbunov)
#### Post date: [December 28, 2017, 4:20am UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893/9 "2017-12-28T04:20:42Z")

</div>

> [@fgaule](#):
>
> suspendingSequence seems to be very interesting but its only available on examples module. Is there any reason to be only there?

[Channels](https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md#channels) in kotlinx.coroutines represent the same concept, but more elaborate.

---

<div class="post-metadata">

### Author: ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)
#### Post date: [December 28, 2017, 6:46am UTC](https://discuss.kotlinlang.org/t/is-there-a-way-to-use-actual-suspended-function-in-buildsequence/5893/10 "2017-12-28T06:46:54Z")

</div>

Currently there are at least three entities with similar functionality: Java Streams, Sequences and Channels. It is a source of some confusion. I understand that current kotlin type system does not allow to replace Channel calls with suspended functions with Sequence calls with regular functions, but there could be a situation, when you want to use suspended function in sequence manipulation and vice versa.
