//arrow-fx-coroutines/arrow.fx.coroutines/sequence

sequence

common inline fun <A> Iterable<Resource<A».sequence(): Resource<List<A»

Sequences this Iterable of Resources. Iterable.map and sequence is equivalent to traverseResource.

import arrow.fx.coroutines.*

class File(url: String) {
  suspend fun open(): File = this
  suspend fun close(): Unit {}
  override fun toString(): String = "This file contains some interesting content!"
}

suspend fun openFile(uri: String): File = File(uri).open()
suspend fun closeFile(file: File): Unit = file.close()
suspend fun fileToString(file: File): String = file.toString()

suspend fun main(): Unit {
  //sampleStart
  val res: List<String> = listOf(
    "data.json",
    "user.json",
    "resource.json"
  ).map { uri ->
    resource {
     openFile(uri)
    } release { file ->
      closeFile(file)
    }
  }.sequence().use { files ->
    files.map { fileToString(it) }
  }
  //sampleEnd
  res.forEach(::println)
}

Do you like Arrow?

Arrow Org
<