S
- the input that might fail to map to its outputT
- the guaranteed outputA
- the output that might fail to be producedB
- the input that guarantees its output@FunctionalInterface public interface Prism<S,T,A,B> extends ProtoOptic<Cocartesian<?,?,?>,S,T,A,B>, MonadRec<T,Prism<S,?,A,B>>, Profunctor<S,T,Prism<?,?,A,B>>
Isos
that can fail in one direction. Example:
Prism<String, String, Integer, Integer> parseInt =
prism(str -> Either.trying(() -> Integer.parseInt(str),
constantly(str)),
Object::toString);
String str = view(re(parseInt), 123); // "123"
Maybe<Integer> works = view(pre(parseInt), "123"); // Just 123
Maybe<Integer> fails = view(pre(parseInt), "foo"); // Nothing
Note that because a Prism
might fail in one direction, it cannot be immediately used for
viewing
; however, the combinators re
, pre
, and matching
can all
be used to provide the additional context to a Prism
so it can be used for viewing.
Modifier and Type | Interface and Description |
---|---|
static interface |
Prism.Simple<S,A>
A convenience type with a simplified type signature for common
prism with unified S/T
and A/B types. |
Modifier and Type | Method and Description |
---|---|
default <Z,C> Prism<S,T,Z,C> |
andThen(ProtoOptic<? super Cocartesian<?,?,?>,A,B,Z,C> f)
Left-to-right composition of proto-optics.
|
default <CoP extends Profunctor<?,?,? extends Cocartesian<?,?,?>>,CoF extends Functor<?,? extends Identity<?>>,FB extends Functor<B,? extends CoF>,FT extends Functor<T,? extends CoF>,PAFB extends Profunctor<A,FB,? extends CoP>,PSFT extends Profunctor<S,FT,? extends CoP>> |
apply(PAFB pafb)
The polymorphic arrow between profunctors in this optic interface.
|
default <R,U> Prism<R,U,A,B> |
compose(ProtoOptic<? super Cocartesian<?,?,?>,R,U,S,T> g)
Right-to-Left composition of proto-optics.
|
default <R> Prism<R,T,A,B> |
contraMap(Fn1<? super R,? extends S> fn)
Contravariantly map
A <- B . |
default <R,U> Prism<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> Prism<R,T,A,B> |
diMapL(Fn1<? super R,? extends S> fn)
Contravariantly map over the left parameter.
|
default <U> Prism<S,U,A,B> |
diMapR(Fn1<? super T,? extends U> fn)
Covariantly map over the right parameter.
|
default <U> Prism<S,U,A,B> |
discardL(Applicative<U,Prism<S,?,A,B>> appB)
Sequence both this
Applicative and appB , discarding this Applicative's
result and returning appB . |
default <U> Prism<S,T,A,B> |
discardR(Applicative<U,Prism<S,?,A,B>> appB)
Sequence both this
Applicative and appB , discarding appB's result and
returning this Applicative . |
default <U> Prism<S,U,A,B> |
flatMap(Fn1<? super T,? extends Monad<U,Prism<S,?,A,B>>> f)
Chain dependent computations that may continue or short-circuit based on previous results.
|
default <U> Prism<S,U,A,B> |
fmap(Fn1<? super T,? extends U> fn)
Covariantly transmute this functor's parameter using the given mapping function.
|
static <S,A,B> Prism<S,S,A,B> |
fromPartial(Fn1<? super S,? extends A> partialSa,
Fn1<? super B,? extends S> bs)
Static factory method for creating a
Prism from a partial function S -> A and a total
function B -> S . |
default <U> Lazy<Prism<S,U,A,B>> |
lazyZip(Lazy<? extends Applicative<Fn1<? super T,? extends U>,Prism<S,?,A,B>>> lazyAppFn)
Given a
lazy instance of this applicative over a mapping function, "zip" the two instances together
using whatever application semantics the current applicative supports. |
default <C> Prism<S,T,C,B> |
mapA(Fn1<? super A,? extends C> fn)
Covariantly map
A to C , yielding a new optic. |
default <Z> Prism<S,T,A,Z> |
mapB(Fn1<? super Z,? extends B> fn)
Contravariantly map
B to Z , yielding a new optic. |
default <R> Prism<R,T,A,B> |
mapS(Fn1<? super R,? extends S> fn)
Contravariantly map
S to R , yielding a new optic. |
default <U> Prism<S,U,A,B> |
mapT(Fn1<? super T,? extends U> fn)
Covariantly map
T to U , yielding a new optic. |
static <S,T,A,B> Prism<S,T,A,B> |
prism(Fn1<? super S,? extends CoProduct2<T,A,?>> sta,
Fn1<? super B,? extends T> bt)
|
static <S,T,A,B> Prism<S,T,A,B> |
prism(Optic<? super Cocartesian<?,?,?>,? super Functor<?,?>,S,T,A,B> optic)
|
static <S,T,A,B> Prism<S,T,A,B> |
prism(ProtoOptic<? super Cocartesian<?,?,?>,S,T,A,B> protoOptic)
Promote a
ProtoOptic with compatible bounds to an Prism . |
default <U> Prism<S,U,A,B> |
pure(U u)
Lift the value
b into this applicative functor. |
static <S,A,B> Pure<Prism<S,?,A,B>> |
purePrism()
|
static <S,A> Prism.Simple<S,A> |
simplePrism(Fn1<? super S,? extends Maybe<A>> sMaybeA,
Fn1<? super A,? extends S> as)
Static factory method for creating a simple
Prism from a function and its potentially failing inverse. |
default <U> Prism<S,U,A,B> |
trampolineM(Fn1<? super T,? extends MonadRec<RecursiveResult<T,U>,Prism<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 B,? extends T>,Fn1<? super S,? extends Either<T,A>>> |
unPrism()
|
default <U> Prism<S,U,A,B> |
zip(Applicative<Fn1<? super T,? extends U>,Prism<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.
|
toOptic
default <CoP extends Profunctor<?,?,? extends Cocartesian<?,?,?>>,CoF extends Functor<?,? extends Identity<?>>,FB extends Functor<B,? extends CoF>,FT extends Functor<T,? extends CoF>,PAFB extends Profunctor<A,FB,? extends CoP>,PSFT extends Profunctor<S,FT,? extends CoP>> PSFT apply(PAFB pafb)
apply
in interface Optic<Cocartesian<?,?,?>,Identity<?>,S,T,A,B>
apply
in interface ProtoOptic<Cocartesian<?,?,?>,S,T,A,B>
CoP
- the profunctor type constraint witnessed by the application of this opticCoF
- the functor type constraint witnessed by the application of this opticFB
- the covariant parameter type of the input profunctorFT
- the covariant parameter type of the output profunctorPAFB
- the full input typePSFT
- the full output typepafb
- the inputdefault <Z,C> Prism<S,T,Z,C> andThen(ProtoOptic<? super Cocartesian<?,?,?>,A,B,Z,C> f)
S
and T
.andThen
in interface ProtoOptic<Cocartesian<?,?,?>,S,T,A,B>
Z
- the new left side of the input profunctorC
- the new right side's functor embedding of the input profunctorf
- the other proto-opticdefault <R,U> Prism<R,U,A,B> compose(ProtoOptic<? super Cocartesian<?,?,?>,R,U,S,T> g)
A
and B
.compose
in interface ProtoOptic<Cocartesian<?,?,?>,S,T,A,B>
R
- the new left side of the output profunctorU
- the new right side's functor embedding of the output profunctorg
- the other proto-opticdefault <R> Prism<R,T,A,B> mapS(Fn1<? super R,? extends S> fn)
S
to R
, yielding a new optic.mapS
in interface Optic<Cocartesian<?,?,?>,Identity<?>,S,T,A,B>
mapS
in interface ProtoOptic<Cocartesian<?,?,?>,S,T,A,B>
R
- the new left side of the output profunctorfn
- the mapping functiondefault <U> Prism<S,U,A,B> mapT(Fn1<? super T,? extends U> fn)
T
to U
, yielding a new optic.mapT
in interface Optic<Cocartesian<?,?,?>,Identity<?>,S,T,A,B>
mapT
in interface ProtoOptic<Cocartesian<?,?,?>,S,T,A,B>
U
- the new right side's functor embedding of the output profunctorfn
- the mapping functiondefault <C> Prism<S,T,C,B> mapA(Fn1<? super A,? extends C> fn)
A
to C
, yielding a new optic.mapA
in interface Optic<Cocartesian<?,?,?>,Identity<?>,S,T,A,B>
mapA
in interface ProtoOptic<Cocartesian<?,?,?>,S,T,A,B>
C
- the new left side of the input profunctorfn
- the mapping functiondefault <Z> Prism<S,T,A,Z> mapB(Fn1<? super Z,? extends B> fn)
B
to Z
, yielding a new optic.mapB
in interface Optic<Cocartesian<?,?,?>,Identity<?>,S,T,A,B>
mapB
in interface ProtoOptic<Cocartesian<?,?,?>,S,T,A,B>
Z
- the new right side's functor embedding of the input profunctorfn
- the mapping functiondefault <U> Prism<S,U,A,B> pure(U u)
b
into this applicative functor.pure
in interface Applicative<T,Prism<S,?,A,B>>
pure
in interface Monad<T,Prism<S,?,A,B>>
pure
in interface MonadRec<T,Prism<S,?,A,B>>
U
- the type of the returned applicative's parameteru
- the valuedefault <U> Prism<S,U,A,B> flatMap(Fn1<? super T,? extends Monad<U,Prism<S,?,A,B>>> f)
default <U> Prism<S,U,A,B> fmap(Fn1<? super T,? extends U> fn)
fmap
in interface Applicative<T,Prism<S,?,A,B>>
fmap
in interface Functor<T,Prism<S,?,A,B>>
fmap
in interface Monad<T,Prism<S,?,A,B>>
fmap
in interface MonadRec<T,Prism<S,?,A,B>>
U
- the new parameter typefn
- the mapping functiondefault <U> Prism<S,U,A,B> zip(Applicative<Fn1<? super T,? extends U>,Prism<S,?,A,B>> appFn)
zip
in interface Applicative<T,Prism<S,?,A,B>>
zip
in interface Monad<T,Prism<S,?,A,B>>
zip
in interface MonadRec<T,Prism<S,?,A,B>>
U
- the resulting applicative parameter typeappFn
- the other applicative instancedefault <U> Lazy<Prism<S,U,A,B>> lazyZip(Lazy<? extends Applicative<Fn1<? super T,? extends U>,Prism<S,?,A,B>>> lazyAppFn)
lazy
instance of this applicative over a mapping function, "zip" the two instances together
using whatever application semantics the current applicative supports. This is useful for applicatives that
support lazy evaluation and early termination.lazyZip
in interface Applicative<T,Prism<S,?,A,B>>
lazyZip
in interface Monad<T,Prism<S,?,A,B>>
lazyZip
in interface MonadRec<T,Prism<S,?,A,B>>
U
- the resulting applicative parameter typelazyAppFn
- the lazy other applicative instanceMaybe
,
Either
default <U> Prism<S,U,A,B> discardL(Applicative<U,Prism<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,Prism<S,?,A,B>>
discardL
in interface Monad<T,Prism<S,?,A,B>>
discardL
in interface MonadRec<T,Prism<S,?,A,B>>
U
- the type of the returned Applicative's parameterappB
- the other Applicativedefault <U> Prism<S,T,A,B> discardR(Applicative<U,Prism<S,?,A,B>> appB)
Applicative
and appB
, discarding appB's
result and
returning this Applicative
. This is generally useful for sequentially performing side-effects.discardR
in interface Applicative<T,Prism<S,?,A,B>>
discardR
in interface Monad<T,Prism<S,?,A,B>>
discardR
in interface MonadRec<T,Prism<S,?,A,B>>
U
- the type of appB's parameterappB
- the other Applicativedefault <U> Prism<S,U,A,B> trampolineM(Fn1<? super T,? extends MonadRec<RecursiveResult<T,U>,Prism<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,Prism<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,U> Prism<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,Prism<?,?,A,B>>
R
- the new left parameter typeU
- the new right parameter typelFn
- the left parameter mapping functionrFn
- the right parameter mapping functiondefault <R> Prism<R,T,A,B> diMapL(Fn1<? super R,? extends S> fn)
default <U> Prism<S,U,A,B> diMapR(Fn1<? super T,? extends U> fn)
diMapR(f) == fmap(f)
.default <R> Prism<R,T,A,B> contraMap(Fn1<? super R,? extends S> fn)
A <- B
.contraMap
in interface Contravariant<S,Profunctor<?,T,Prism<?,?,A,B>>>
contraMap
in interface Profunctor<S,T,Prism<?,?,A,B>>
R
- the new parameter typefn
- the mapping functionstatic <S,T,A,B> Prism<S,T,A,B> prism(Fn1<? super S,? extends CoProduct2<T,A,?>> sta, Fn1<? super B,? extends T> bt)
static <S,T,A,B> Prism<S,T,A,B> prism(ProtoOptic<? super Cocartesian<?,?,?>,S,T,A,B> protoOptic)
ProtoOptic
with compatible bounds to an Prism
.S
- the input that might fail to map to its outputT
- the guaranteed outputA
- the output that might fail to be producedB
- the input that guarantees its outputprotoOptic
- the ProtoOptic
Prism
static <S,T,A,B> Prism<S,T,A,B> prism(Optic<? super Cocartesian<?,?,?>,? super Functor<?,?>,S,T,A,B> optic)
static <S,A> Prism.Simple<S,A> simplePrism(Fn1<? super S,? extends Maybe<A>> sMaybeA, Fn1<? super A,? extends S> as)
Prism
from a function and its potentially failing inverse.S
- the input that might fail to map to its output and the guaranteed output from the other directionA
- the output that might fail to be produced and the input that guarantees its output in the other
directionsMaybeA
- a partial mapping from S -> A
as
- a total mapping from A -> S
simple prism
static <S,A,B> Prism<S,S,A,B> fromPartial(Fn1<? super S,? extends A> partialSa, Fn1<? super B,? extends S> bs)
Prism
from a partial function S -> A
and a total
function B -> S
.S
- the input that might fail to map to its output and the guaranteed output from the other
directionA
- the output that might fail to be producedB
- the input that guarantees its output in the other directionpartialSa
- the partial directionbs
- the reverse total directionPrism