Interface Prism<S,T,A,B>
- Type Parameters:
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
- All Superinterfaces:
Applicative<T,,Prism<S, ?, A, B>> Contravariant<S,,Profunctor<?, T, Prism<?, ?, A, B>>> Functor<T,,Prism<S, ?, A, B>> Monad<T,,Prism<S, ?, A, B>> MonadRec<T,,Prism<S, ?, A, B>> Optic<Cocartesian<?,,?, ?>, Identity<?>, S, T, A, B> Profunctor<S,,T, Prism<?, ?, A, B>> ProtoOptic<Cocartesian<?,?, ?>, S, T, A, B>
- All Known Subinterfaces:
Prism.Simple<S,A>
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@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>>
Prisms are
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.
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceA convenience type with a simplified type signature for commonprismwith unifiedS/TandA/Btypes. -
Method Summary
Modifier and TypeMethodDescriptionandThen(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>>
PSFTapply(PAFB pafb) The polymorphic arrow between profunctors in this optic interface.compose(ProtoOptic<? super Cocartesian<?, ?, ?>, R, U, S, T> g) Right-to-Left composition of proto-optics.Contravariantly mapA <- B.Dually map contravariantly over the left parameter and covariantly over the right parameter.Contravariantly map over the left parameter.Covariantly map over the right parameter.Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative.Chain dependent computations that may continue or short-circuit based on previous results.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 aPrismfrom a partial functionS -> Aand a total functionB -> S.Given alazyinstance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.Covariantly mapAtoC, yielding a new optic.Contravariantly mapBtoZ, yielding a new optic.Contravariantly mapStoR, yielding a new optic.Covariantly mapTtoU, 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 aProtoOpticwith compatible bounds to anPrism.pure(U u) Lift the valuebinto this applicative functor.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 simplePrismfrom a function and its potentially failing inverse.trampolineM(Fn1<? super T, ? extends MonadRec<RecursiveResult<T, U>, Prism<S, ?, A, B>>> fn) Given some operation yielding aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.unPrism()Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.Methods inherited from interface com.jnape.palatable.lambda.optics.Optic
andThen, compose, monomorphizeMethods inherited from interface com.jnape.palatable.lambda.optics.ProtoOptic
toOptic
-
Method Details
-
unPrism
-
apply
default <CoP extends Profunctor<?,?, PSFT apply? 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>> (PAFB pafb) The polymorphic arrow between profunctors in this optic interface.- Specified by:
applyin interfaceOptic<Cocartesian<?,?, ?>, Identity<?>, S, T, A, B> - Specified by:
applyin interfaceProtoOptic<Cocartesian<?,?, ?>, S, T, A, B> - Type Parameters:
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 type- Parameters:
pafb- the input- Returns:
- the output profunctor
-
andThen
Left-to-right composition of proto-optics. Requires compatibility betweenSandT.- Specified by:
andThenin interfaceProtoOptic<Cocartesian<?,?, ?>, S, T, A, B> - Type Parameters:
Z- the new left side of the input profunctorC- the new right side's functor embedding of the input profunctor- Parameters:
f- the other proto-optic- Returns:
- the composed proto-optic
-
compose
Right-to-Left composition of proto-optics. Requires compatibility betweenAandB.- Specified by:
composein interfaceProtoOptic<Cocartesian<?,?, ?>, S, T, A, B> - Type Parameters:
R- the new left side of the output profunctorU- the new right side's functor embedding of the output profunctor- Parameters:
g- the other proto-optic- Returns:
- the composed proto-optic
-
mapS
Contravariantly mapStoR, yielding a new optic.- Specified by:
mapSin interfaceOptic<Cocartesian<?,?, ?>, Identity<?>, S, T, A, B> - Specified by:
mapSin interfaceProtoOptic<Cocartesian<?,?, ?>, S, T, A, B> - Type Parameters:
R- the new left side of the output profunctor- Parameters:
fn- the mapping function- Returns:
- the new optic
-
mapT
Covariantly mapTtoU, yielding a new optic.- Specified by:
mapTin interfaceOptic<Cocartesian<?,?, ?>, Identity<?>, S, T, A, B> - Specified by:
mapTin interfaceProtoOptic<Cocartesian<?,?, ?>, S, T, A, B> - Type Parameters:
U- the new right side's functor embedding of the output profunctor- Parameters:
fn- the mapping function- Returns:
- the new optic
-
mapA
Covariantly mapAtoC, yielding a new optic.- Specified by:
mapAin interfaceOptic<Cocartesian<?,?, ?>, Identity<?>, S, T, A, B> - Specified by:
mapAin interfaceProtoOptic<Cocartesian<?,?, ?>, S, T, A, B> - Type Parameters:
C- the new left side of the input profunctor- Parameters:
fn- the mapping function- Returns:
- the new optic
-
mapB
Contravariantly mapBtoZ, yielding a new optic.- Specified by:
mapBin interfaceOptic<Cocartesian<?,?, ?>, Identity<?>, S, T, A, B> - Specified by:
mapBin interfaceProtoOptic<Cocartesian<?,?, ?>, S, T, A, B> - Type Parameters:
Z- the new right side's functor embedding of the input profunctor- Parameters:
fn- the mapping function- Returns:
- the new optic
-
pure
Lift the valuebinto this applicative functor. -
flatMap
Chain dependent computations that may continue or short-circuit based on previous results. -
fmap
Covariantly transmute this functor's parameter using the given mapping function. Generally this method is specialized to return an instance of the class implementing Functor.- Specified by:
fmapin interfaceApplicative<S,T> - Specified by:
fmapin interfaceFunctor<S,T> - Specified by:
fmapin interfaceMonad<S,T> - Specified by:
fmapin interfaceMonadRec<S,T> - Type Parameters:
U- the new parameter type- Parameters:
fn- the mapping function- Returns:
- a functor over B (the new parameter type)
-
zip
Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports. -
lazyZip
default <U> Lazy<Prism<S,U, lazyZipA, B>> (Lazy<? extends Applicative<Fn1<? super T, ? extends U>, Prism<S, ?, A, B>>> lazyAppFn) Given alazyinstance 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.- Specified by:
lazyZipin interfaceApplicative<S,T> - Specified by:
lazyZipin interfaceMonad<S,T> - Specified by:
lazyZipin interfaceMonadRec<S,T> - Type Parameters:
U- the resulting applicative parameter type- Parameters:
lazyAppFn- the lazy other applicative instance- Returns:
- the mapped applicative
- See Also:
-
discardL
Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB. This is generally useful for sequentially performing side-effects. -
discardR
Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative. This is generally useful for sequentially performing side-effects. -
trampolineM
default <U> Prism<S,U, trampolineMA, B> (Fn1<? super T, ? extends MonadRec<RecursiveResult<T, U>, Prism<S, ?, A, B>>> fn) Given some operation yielding aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.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.
- Specified by:
trampolineMin interfaceMonadRec<S,T> - Type Parameters:
U- the ultimate resulting carrier type- Parameters:
fn- the function to internally trampoline- Returns:
- the trampolined
MonadRec - See Also:
-
diMap
Dually map contravariantly over the left parameter and covariantly over the right parameter. This is isomorphic todiMapL(lFn).diMapR(rFn).- Specified by:
diMapin interfaceProfunctor<S,T, A> - Type Parameters:
R- the new left parameter typeU- the new right parameter type- Parameters:
lFn- the left parameter mapping functionrFn- the right parameter mapping function- Returns:
- a profunctor over Z (the new left parameter type) and C (the new right parameter type)
-
diMapL
Contravariantly map over the left parameter.- Specified by:
diMapLin interfaceProfunctor<S,T, A> - Type Parameters:
R- the new left parameter type- Parameters:
fn- the mapping function- Returns:
- a profunctor over Z (the new left parameter type) and C (the same right parameter type)
-
diMapR
Covariantly map over the right parameter. For all profunctors that are also functors, it should hold thatdiMapR(f) == fmap(f).- Specified by:
diMapRin interfaceProfunctor<S,T, A> - Type Parameters:
U- the new right parameter type- Parameters:
fn- the mapping function- Returns:
- a profunctor over A (the same left parameter type) and C (the new right parameter type)
-
contraMap
Contravariantly mapA <- B.- Specified by:
contraMapin interfaceContravariant<S,T> - Specified by:
contraMapin interfaceProfunctor<S,T, A> - Type Parameters:
R- the new parameter type- Parameters:
fn- the mapping function- Returns:
- the mapped Contravariant functor instance
-
prism
static <S,T, Prism<S,A, B> T, prismA, B> (Fn1<? super S, ? extends CoProduct2<T, A, ?>> sta, Fn1<? super B, ? extends T> bt) -
prism
Promote aProtoOpticwith compatible bounds to anPrism.- Type Parameters:
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- Parameters:
protoOptic- theProtoOptic- Returns:
- the
Prism
-
prism
static <S,T, Prism<S,A, B> T, prismA, B> (Optic<? super Cocartesian<?, ?, ?>, ? super Functor<?, ?>, S, T, A, B> optic) -
simplePrism
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 simplePrismfrom a function and its potentially failing inverse.- Type Parameters:
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 direction- Parameters:
sMaybeA- a partial mapping fromS -> Aas- a total mapping fromA -> S- Returns:
- the
simple prism
-
fromPartial
static <S,A, Prism<S,B> S, fromPartialA, B> (Fn1<? super S, ? extends A> partialSa, Fn1<? super B, ? extends S> bs) Static factory method for creating aPrismfrom a partial functionS -> Aand a total functionB -> S.- Type Parameters:
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 direction- Parameters:
partialSa- the partial directionbs- the reverse total direction- Returns:
- the
Prism
-
purePrism
- Type Parameters:
S- the input that might fail to map to its outputA- the output that might fail to be producedB- the input that guarantees its output- Returns:
- the
Pureinstance
-