common abstract suspend fun <B> modify(f: (A) -> Pair<A, B>): B

Modify allows to inspect the state A of the AtomicRef, update it and extract a different state B.

import arrow.fx.coroutines.*

typealias Id = Int
data class Job(val description: String)

val initialState = (0 until 10).map { i -> Pair(i, Job("Task #$i")) }

suspend fun main(): Unit {
  val jobs = Atomic(initialState)

  val batch = jobs.modify { j ->
    val batch = j.take(5)
    Pair(j.drop(5), batch)

  batch.forEach { (id, job) ->
    println("Going to work on $job with id $id\n")

  println("Remaining: ${jobs.get()}")

Do you like Arrow?

Arrow Org