S
- the larger type for focusingT
- the larger type for mirrored focusingA
- the smaller type for focusingB
- the smaller type for mirrored focusing@FunctionalInterface public interface Iso<S,T,A,B> extends Optic<Profunctor<?,?,?>,Functor<?,?>,S,T,A,B>, MonadRec<T,Iso<S,?,A,B>>, Profunctor<S,T,Iso<?,?,A,B>>
Iso
(short for "isomorphism") is an invertible Lens
: an Optic
encoding of a
bi-directional focusing of two types, and like Lens
es
, can be View
ed
,
Set
, and updated
.
As an example, consider the isomorphism between valid String
s and Integer
s:
Iso<String, String, Integer, Integer> stringIntIso = Iso.iso(Integer::parseInt, Object::toString);
Integer asInt = view(stringIntIso, "123"); // 123
String asString = view(stringIntIso.mirror(), 123); // "123"
In the previous example, stringIntIso
can be viewed as an
Optic
<String, String, Integer, Integer>
, and can be mirror()
ed
and
viewed as a Optic
<Integer, Integer, String, String>
.
As with Lens
, variance is supported between S/T
and A/B
, and where these pairs do
not vary, a Iso.Simple
iso can be used (for instance, in the previous example, stringIntIso
could
have had the simplified Iso.Simple<String, Integer>
type).
For more information, read about isos.
Modifier and Type | Interface and Description |
---|---|
static interface |
Iso.Simple<S,A>
A convenience type with a simplified type signature for common isos with both unified "larger" values and
unified "smaller" values.
|
Modifier and Type | Method and Description |
---|---|
default <Z,C> Iso<S,T,Z,C> |
andThen(Optic<? super Profunctor<?,?,?>,? super Functor<?,?>,A,B,Z,C> f)
Left-to-right composition of optics.
|
default <R,U> Iso<R,U,A,B> |
compose(Optic<? super Profunctor<?,?,?>,? super Functor<?,?>,R,U,S,T> g)
Right-to-Left composition of optics.
|
default <R> Iso<R,T,A,B> |
contraMap(Fn1<? super R,? extends S> fn)
Contravariantly map
A <- B . |
default <R,U> Iso<R,U,A,B> |
diMap(Fn1<? super R,? extends S> lFn,
Fn1<? super T,? extends U> rFn)
Dually map contravariantly over the left parameter and covariantly over the right parameter.
|
default <R> Iso<R,T,A,B> |
diMapL(Fn1<? super R,? extends S> fn)
Contravariantly map over the left parameter.
|
default <U> Iso<S,U,A,B> |
diMapR(Fn1<? super T,? extends U> fn)
Covariantly map over the right parameter.
|
default <U> Iso<S,U,A,B> |
discardL(Applicative<U,Iso<S,?,A,B>> appB)
Sequence both this
Applicative and appB , discarding this Applicative's
result and returning appB . |
default <U> Iso<S,T,A,B> |
discardR(Applicative<U,Iso<S,?,A,B>> appB)
Sequence both this
Applicative and appB , discarding appB's result and
returning this Applicative . |
default <U> Iso<S,U,A,B> |
flatMap(Fn1<? super T,? extends Monad<U,Iso<S,?,A,B>>> fn)
Chain dependent computations that may continue or short-circuit based on previous results.
|
default <U> Iso<S,U,A,B> |
fmap(Fn1<? super T,? extends U> fn)
Covariantly transmute this functor's parameter using the given mapping function.
|
static <S,T,A,B> Iso<S,T,A,B> |
iso(Fn1<? super S,? extends A> f,
Fn1<? super B,? extends T> g)
Static factory method for creating an iso from a function and it's inverse.
|
static <S,T,A,B> Iso<S,T,A,B> |
iso(Optic<? super Profunctor<?,?,?>,? super Functor<?,?>,S,T,A,B> optic)
Promote an optic with compatible bounds to an
Iso . |
default <C> Iso<S,T,C,B> |
mapA(Fn1<? super A,? extends C> fn)
Covariantly map
A to C , yielding a new optic. |
default <Z> Iso<S,T,A,Z> |
mapB(Fn1<? super Z,? extends B> fn)
Contravariantly map
B to Z , yielding a new optic. |
default <R> Iso<R,T,A,B> |
mapS(Fn1<? super R,? extends S> fn)
Contravariantly map
S to R , yielding a new optic. |
default <U> Iso<S,U,A,B> |
mapT(Fn1<? super T,? extends U> fn)
Covariantly map
T to U , yielding a new optic. |
default Iso<B,A,T,S> |
mirror()
Flip this
Iso around. |
default <U> Iso<S,U,A,B> |
pure(U u)
Lift the value
b into this applicative functor. |
static <S,A,B> Pure<Iso<S,?,A,B>> |
pureIso(Fn1<? super S,? extends A> sa)
|
static <S,A> Iso.Simple<S,A> |
simpleIso(Fn1<? super S,? extends A> f,
Fn1<? super A,? extends S> g)
Static factory method for creating a simple
Iso from a function and its inverse. |
default Lens<S,T,A,B> |
toLens()
|
default <U> Iso<S,U,A,B> |
trampolineM(Fn1<? super T,? extends MonadRec<RecursiveResult<T,U>,Iso<S,?,A,B>>> fn)
Given some operation yielding a
RecursiveResult inside this MonadRec , internally trampoline the
operation until it yields a termination instruction. |
default Tuple2<Fn1<? super S,? extends A>,Fn1<? super B,? extends T>> |
unIso()
|
default <U> Iso<S,U,A,B> |
zip(Applicative<Fn1<? super T,? extends U>,Iso<S,?,A,B>> appFn)
Given another instance of this applicative over a mapping function, "zip" the two instances together using
whatever application semantics the current applicative supports.
|
apply, monomorphize, optic, reframe
default Tuple2<Fn1<? super S,? extends A>,Fn1<? super B,? extends T>> unIso()
default <U> Iso<S,U,A,B> fmap(Fn1<? super T,? extends U> fn)
fmap
in interface Applicative<T,Iso<S,?,A,B>>
fmap
in interface Functor<T,Iso<S,?,A,B>>
fmap
in interface Monad<T,Iso<S,?,A,B>>
fmap
in interface MonadRec<T,Iso<S,?,A,B>>
U
- the new parameter typefn
- the mapping functiondefault <U> Iso<S,U,A,B> pure(U u)
b
into this applicative functor.pure
in interface Applicative<T,Iso<S,?,A,B>>
pure
in interface Monad<T,Iso<S,?,A,B>>
pure
in interface MonadRec<T,Iso<S,?,A,B>>
U
- the type of the returned applicative's parameteru
- the valuedefault <U> Iso<S,U,A,B> zip(Applicative<Fn1<? super T,? extends U>,Iso<S,?,A,B>> appFn)
zip
in interface Applicative<T,Iso<S,?,A,B>>
zip
in interface Monad<T,Iso<S,?,A,B>>
zip
in interface MonadRec<T,Iso<S,?,A,B>>
U
- the resulting applicative parameter typeappFn
- the other applicative instancedefault <U> Iso<S,U,A,B> discardL(Applicative<U,Iso<S,?,A,B>> appB)
Applicative
and appB
, discarding this Applicative's
result and returning appB
. This is generally useful for sequentially performing side-effects.discardL
in interface Applicative<T,Iso<S,?,A,B>>
discardL
in interface Monad<T,Iso<S,?,A,B>>
discardL
in interface MonadRec<T,Iso<S,?,A,B>>
U
- the type of the returned Applicative's parameterappB
- the other Applicativedefault <U> Iso<S,T,A,B> discardR(Applicative<U,Iso<S,?,A,B>> appB)
Applicative
and appB
, discarding appB's
result and
returning this Applicative
. This is generally useful for sequentially performing side-effects.default <U> Iso<S,U,A,B> flatMap(Fn1<? super T,? extends Monad<U,Iso<S,?,A,B>>> fn)
default <U> Iso<S,U,A,B> trampolineM(Fn1<? super T,? extends MonadRec<RecursiveResult<T,U>,Iso<S,?,A,B>>> fn)
RecursiveResult
inside this MonadRec
, internally trampoline the
operation until it yields a termination
instruction.
Stack-safety depends on implementations guaranteeing that the growth of the call stack is a constant factor independent of the number of invocations of the operation. For various examples of how this can be achieved in stereotypical circumstances, see the referenced types.
trampolineM
in interface MonadRec<T,Iso<S,?,A,B>>
U
- the ultimate resulting carrier typefn
- the function to internally trampolineMonadRec
for a basic implementation
,
for a {@link CoProduct2 coproduct} implementation
,
for an implementation leveraging an already stack-safe {@link Monad#flatMap(Fn1)}
,
for a {@link MonadT monad transformer} implementation
default <R> Iso<R,T,A,B> diMapL(Fn1<? super R,? extends S> fn)
default <U> Iso<S,U,A,B> diMapR(Fn1<? super T,? extends U> fn)
diMapR(f) == fmap(f)
.default <R,U> Iso<R,U,A,B> diMap(Fn1<? super R,? extends S> lFn, Fn1<? super T,? extends U> rFn)
diMapL(lFn).diMapR(rFn)
.diMap
in interface Profunctor<S,T,Iso<?,?,A,B>>
R
- the new left parameter typeU
- the new right parameter typelFn
- the left parameter mapping functionrFn
- the right parameter mapping functiondefault <R> Iso<R,T,A,B> contraMap(Fn1<? super R,? extends S> fn)
A <- B
.contraMap
in interface Contravariant<S,Profunctor<?,T,Iso<?,?,A,B>>>
contraMap
in interface Profunctor<S,T,Iso<?,?,A,B>>
R
- the new parameter typefn
- the mapping functiondefault <R> Iso<R,T,A,B> mapS(Fn1<? super R,? extends S> fn)
S
to R
, yielding a new optic.default <U> Iso<S,U,A,B> mapT(Fn1<? super T,? extends U> fn)
T
to U
, yielding a new optic.default <C> Iso<S,T,C,B> mapA(Fn1<? super A,? extends C> fn)
A
to C
, yielding a new optic.default <Z> Iso<S,T,A,Z> mapB(Fn1<? super Z,? extends B> fn)
B
to Z
, yielding a new optic.default <Z,C> Iso<S,T,Z,C> andThen(Optic<? super Profunctor<?,?,?>,? super Functor<?,?>,A,B,Z,C> f)
S
and T
.default <R,U> Iso<R,U,A,B> compose(Optic<? super Profunctor<?,?,?>,? super Functor<?,?>,R,U,S,T> g)
A
and B
.static <S,T,A,B> Iso<S,T,A,B> iso(Fn1<? super S,? extends A> f, Fn1<? super B,? extends T> g)
S
- the larger type for focusingT
- the larger type for mirrored focusingA
- the smaller type for focusingB
- the smaller type for mirrored focusingf
- the functiong
- f's inversestatic <S,T,A,B> Iso<S,T,A,B> iso(Optic<? super Profunctor<?,?,?>,? super Functor<?,?>,S,T,A,B> optic)
Iso
.static <S,A> Iso.Simple<S,A> simpleIso(Fn1<? super S,? extends A> f, Fn1<? super A,? extends S> g)
Iso
from a function and its inverse.S
- one side of the isomorphismA
- the other side of the isomorphismf
- a functiong
- f's inverse