Skip to main content
Version: 1.0.18

TArray

TArray is an array of mutable references that can participate in transactions in STM.

Create a TArrayโ€‹

Creating an empty TArray:

import zio._
import zio.stm._

val emptyTArray: STM[Nothing, TArray[Int]] = TArray.empty[Int]

Or creating a TArray with specified values:

import zio._
import zio.stm._

val specifiedValuesTArray: STM[Nothing, TArray[Int]] = TArray.make(1, 2, 3)

Alternatively, you can create a TArray by providing a collection of values:

import zio._
import zio.stm._

val iterableTArray: STM[Nothing, TArray[Int]] = TArray.fromIterable(List(1, 2, 3))

Retrieve the value from a TArrayโ€‹

The n-th element of the array can be obtained as follows:

import zio._
import zio.stm._

val tArrayGetElem: UIO[Int] = (for {
tArray <- TArray.make(1, 2, 3, 4)
elem <- tArray(2)
} yield elem).commit

Accessing the non-existing indexes aborts the transaction with ArrayIndexOutOfBoundsException.

Update the value of a TArrayโ€‹

Updating the n-th element of an array can be done as follows:

import zio._
import zio.stm._

val tArrayUpdateElem: UIO[TArray[Int]] = (for {
tArray <- TArray.make(1, 2, 3, 4)
_ <- tArray.update(2, el => el + 10)
} yield tArray).commit

Updating the n-th element of an array can be done effectfully via updateM:

import zio._
import zio.stm._

val tArrayUpdateMElem: UIO[TArray[Int]] = (for {
tArray <- TArray.make(1, 2, 3, 4)
_ <- tArray.updateM(2, el => STM.succeed(el + 10))
} yield tArray).commit

Updating the non-existing indexes aborts the transaction with ArrayIndexOutOfBoundsException.

Transform elements of a TArrayโ€‹

The transform function A => A allows computing a new value for every element in the array:

import zio._
import zio.stm._

val transformTArray: UIO[TArray[Int]] = (for {
tArray <- TArray.make(1, 2, 3, 4)
_ <- tArray.transform(a => a * a)
} yield tArray).commit

The elements can be mapped effectfully via transformM:

import zio._
import zio.stm._

val transformMTArray: UIO[TArray[Int]] = (for {
tArray <- TArray.make(1, 2, 3, 4)
_ <- tArray.transformM(a => STM.succeed(a * a))
} yield tArray).commit

Folds the elements of a TArray using the specified associative binary operator:

import zio._
import zio.stm._

val foldTArray: UIO[Int] = (for {
tArray <- TArray.make(1, 2, 3, 4)
sum <- tArray.fold(0)(_ + _)
} yield sum).commit

The elements can be folded effectfully via foldM:

import zio._
import zio.stm._

val foldMTArray: UIO[Int] = (for {
tArray <- TArray.make(1, 2, 3, 4)
sum <- tArray.foldM(0)((acc, el) => STM.succeed(acc + el))
} yield sum).commit

Perform side-effect for TArray elementsโ€‹

foreach is used for performing side-effect for each element in the array:

import zio._
import zio.stm._

val foreachTArray = (for {
tArray <- TArray.make(1, 2, 3, 4)
_ <- tArray.foreach(a => STM.succeed(println(a)))
} yield tArray).commit

ZIO Chat

๐ŸŽ‰ Hey there, ZIO aficionado! ๐ŸŽ‰ Step into the ZIO wonderland with me as your guide. Whether you're a functional fanatic or just dipping your toes into the ZIO ecosystem, I'm here to make your journey smooth. Got ZIO questions? I've got answers! Let's navigate the ZIO universe together and unlock its infinite possibilities!