Documentation / dataflow / com.femastudios.dataflow / MutableField / twoWayTransform
Go to reference

twoWayTransform

open fun <O> twoWayTransform(transformToOther: (T) -> O, transformToMe: (O) -> T): MutableField<O>

Returns a new Field<O>, whose value is transformToOther applied to the value of this Field.

Changes to the returned Field are propagated back to this Field using transformToMe.

For example:

val number = mutableFieldOf(5)
val numberTwoTimes /* : MutableField<Int> */= number.twoWayTransform({ it * 2 }, { it / 2
})
numberTwoTimes.value //Yields 10
number.value = 10
numberTwoTimes.value //Yields 20
numberTwoTimes.value = 50
number.value //Yields 25

In order to work correctly, transformToOther and transformToMe should be perfectly symmetric. This means that for each x, x = transformToMe(transformToOther(x)).

See Also

Field.transform


open fun twoWayTransform(transform: (T) -> T): MutableField<T>

As twoWayTransform, but with a single transformation function.

This is useful when the transformation is symmetric. For example:

val f = mutableFieldOf(true)
val notF = f.twoWayTransform { !it }

notF.value //yields false

notF.value = true
f.value //yields false

f.value = true
notF.value //yields false