Ho un tipo Either ad es. Either[A, B] ma voglio Either[X, Y] utilizzando due funzioni A => X e B => Y .
Posso farlo con fold:
val myEither: Either[A, B] = ...
myEither.fold(
a => Left(createNewX()),
b => Right(createNewY())
)
Ma mi sembra ridondante, visto che devo ripetere Left e Right . Preferisco qualcosa del tipo:
val myEither: Either[A, B] = ...
myEither.transformToEither(
a => createNewX(),
b => createNewY()
)
che trasforma un Either[A, B] in un Either[X, Y] creando un Left per il risultato della prima funzione e un Right per il secondo. Qual è il modo più semplice per farlo?