lens
Creates an AtomicRef for B based on provided a get and set operation.
This is useful when you have an AtomicRef of a data class
and need to work with with certain properties individually, or want to hide parts of your domain from a dependency.
import arrow.fx.coroutines.*
data class Preference(val isEnabled: Boolean)
data class User(val name: String, val age: Int, val preference: Preference)
data class ViewState(val user: User)
suspend fun main(): Unit {
//sampleStart
val state: Atomic<ViewState> = Atomic(ViewState(User("Simon", 27, Preference(false))))
val isEnabled: Atomic<Boolean> =
state.lens(
{ it.user.preference.isEnabled },
{ state, isEnabled ->
state.copy(
user =
state.user.copy(
preference =
state.user.preference.copy(isEnabled = isEnabled)
)
)
}
)
isEnabled.set(true)
println(state.get())
}
Content copied to clipboard