TwoWayFunction

trait TwoWayFunction[A, B] extends A => B

Bi-Directional Function

TwoWayFunction allows to convert value to some type and back

It is used in two way binded collections

  val intToChar: TwoWayFunction[Int,Char] = TwoWayFunction(i => ('A' + i).toChar, _ - 'A')

  val list:  Idx.M[Int] = Stream(0, 2, 4).toBuffer
  val view:  Idx.M[Char] = list.mutableMapView(intToChar)

  list.stream.tp     // Prints Stream(0, 2, 4)
  view.stream.tp     // Prints Stream(A, C, E)

  view(1) = 'Z' // Note, only second collection is updated, but both are changed

  list.stream.tp     // Prints Stream(0, 25, 4)
  view.stream.tp     // Prints Stream(A, Z, E)
Source
TwoWayFunction.scala
trait TwoWayFunction.A => TwoWayFunction.B
class java.lang.Object
trait scala.Matchable
class Any

Def

def apply(v: A): B
def undo(v: B): A

Undo map

Undo map

Restores conversion result to original value

Source
TwoWayFunction.scala

Extension

def reverse[A, B]: TwoWayFunction[B, A]

Reverses two functions

Reverses two functions

apply becomes undo and undo becomes apply

Source
TwoWayFunction.scala