Package com.jnape.palatable.lambda.io
Class IO<A>
- java.lang.Object
-
- com.jnape.palatable.lambda.io.IO<A>
-
- Type Parameters:
A- the result type
- All Implemented Interfaces:
Applicative<A,IO<?>>,Functor<A,IO<?>>,Monad<A,IO<?>>,MonadError<java.lang.Throwable,A,IO<?>>,MonadRec<A,IO<?>>
- Direct Known Subclasses:
IO.Compose
public abstract class IO<A> extends java.lang.Object implements MonadRec<A,IO<?>>, MonadError<java.lang.Throwable,A,IO<?>>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classIO.Compose<A>
-
Constructor Summary
Constructors Modifier Constructor Description privateIO()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Deprecated Methods Modifier and Type Method Description IO<A>catchError(Fn1<? super java.lang.Throwable,? extends Monad<A,IO<?>>> recoveryFn)<B> IO<B>discardL(Applicative<B,IO<?>> appB)Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.<B> IO<A>discardR(Applicative<B,IO<?>> appB)Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative.IO<A>ensuring(IO<?> ensureIO)IO<A>exceptionally(Fn1<? super java.lang.Throwable,? extends A> recoveryFn)Deprecated.in favor of canonicalcatchError(Fn1)static <A> IO<A>externallyManaged(Fn0<java.util.concurrent.CompletableFuture<A>> supplier)Static factory method for creating anIOfrom an externally managed source ofcompletable futures.<B> IO<B>flatMap(Fn1<? super A,? extends Monad<B,IO<?>>> f)Chain dependent computations that may continue or short-circuit based on previous results.<B> IO<B>fmap(Fn1<? super A,? extends B> fn)Covariantly transmute this functor's parameter using the given mapping function.static <A> IO<A>fuse(IO<A> io)Fuse all fork opportunities of a givenIOsuch that, unless it ispinned(or is originallyexternally managed), no parallelism will be used when running it, regardless of what semantics are used when it is executed.static <A> IO<A>interruptible(IO<A> io)static <A> IO<A>io(A a)Static factory method for creating anIOthat just returnsawhen performed.static <A> IO<A>io(Fn0<? extends A> fn0)Static factory method for coercing a lambda to anIO.static IO<Unit>io(SideEffect sideEffect)<B> Lazy<IO<B>>lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,IO<?>>> lazyAppFn)Given alazyinstance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.static <A> IO<A>memoize(IO<A> io)static <A> IO<A>monitorSync(java.lang.Object lock, IO<A> io)Synchronize the givenIOusing the provided lock object.static <A> IO<A>pin(IO<A> io, java.util.concurrent.Executor executor)Pin anIOto anExecutorsuch that regardless of what future decisions are made, when it runs, it will run using whatever parallelism is supported by theExecutor's threading model.<B> IO<B>pure(B b)Lift the valuebinto this applicative functor.static Pure<IO<?>>pureIO()IO<Either<java.lang.Throwable,A>>safe()IO<A>throwError(java.lang.Throwable throwable)Throw an error value of typeEinto themonad.static <A> IO<A>throwing(java.lang.Throwable t)Produce anIOthat throws the givenThrowablewhen executed.<B> IO<B>trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,IO<?>>> fn)Given some operation yielding aRecursiveResultinside thisMonadRec, internally trampoline the operation until it yields aterminationinstruction.java.util.concurrent.CompletableFuture<A>unsafePerformAsyncIO()Returns aCompletableFuturerepresenting the result of this eventual effect.abstract java.util.concurrent.CompletableFuture<A>unsafePerformAsyncIO(java.util.concurrent.Executor executor)Returns aCompletableFuturerepresenting the result of this eventual effect.abstract AunsafePerformIO()Run the effect represented by thisIOinstance, blocking the current thread until the effect terminates.<B> IO<B>zip(Applicative<Fn1<? super A,? extends B>,IO<?>> appFn)Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.
-
-
-
Method Detail
-
unsafePerformIO
public abstract A unsafePerformIO()
Run the effect represented by thisIOinstance, blocking the current thread until the effect terminates.- Returns:
- the result of the effect
-
unsafePerformAsyncIO
public final java.util.concurrent.CompletableFuture<A> unsafePerformAsyncIO()
Returns aCompletableFuturerepresenting the result of this eventual effect. By default, this will immediately run the effect in terms of the implicitExecutoravailable toCompletableFuture(usually theForkJoinPool). Note that specificIOconstructions may allow this method to delegate to externally-managedCompletableFutureinstead of synthesizing their own.- Returns:
- the
CompletableFuturerepresenting thisIO's eventual result - See Also:
unsafePerformAsyncIO(Executor)
-
unsafePerformAsyncIO
public abstract java.util.concurrent.CompletableFuture<A> unsafePerformAsyncIO(java.util.concurrent.Executor executor)
Returns aCompletableFuturerepresenting the result of this eventual effect. By default, this will immediately run the effect in terms of the providedExecutor. Note that specificIOconstructions may allow this method to delegate to externally-managedCompletableFutureinstead of synthesizing their own.- Parameters:
executor- theExecutorto run theCompletableFuturefrom- Returns:
- the
CompletableFuturerepresenting thisIO's eventual result - See Also:
unsafePerformAsyncIO()
-
exceptionally
@Deprecated public final IO<A> exceptionally(Fn1<? super java.lang.Throwable,? extends A> recoveryFn)
Deprecated.in favor of canonicalcatchError(Fn1)Given a function from anyThrowableto the result typeA, if thisIOsuccessfully yields a result, return it; otherwise, map theThrowableto the result type and return that.- Parameters:
recoveryFn- the recovery function- Returns:
- the guarded
IO
-
safe
public final IO<Either<java.lang.Throwable,A>> safe()
Return a safeIOthat will never throw by lifting the result of thisIOintoEither, catching anyThrowableand wrapping it in aleft.- Returns:
- the safe
IO
-
pure
public final <B> IO<B> pure(B b)
Lift the valuebinto this applicative functor.- Specified by:
purein interfaceApplicative<A,IO<?>>- Specified by:
purein interfaceMonad<A,IO<?>>- Specified by:
purein interfaceMonadError<java.lang.Throwable,A,IO<?>>- Specified by:
purein interfaceMonadRec<A,IO<?>>- Type Parameters:
B- the type of the returned applicative's parameter- Parameters:
b- the value- Returns:
- an instance of this applicative over b
-
fmap
public final <B> IO<B> fmap(Fn1<? super A,? extends B> fn)
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<A,IO<?>>- Specified by:
fmapin interfaceFunctor<A,IO<?>>- Specified by:
fmapin interfaceMonad<A,IO<?>>- Specified by:
fmapin interfaceMonadError<java.lang.Throwable,A,IO<?>>- Specified by:
fmapin interfaceMonadRec<A,IO<?>>- Type Parameters:
B- the new parameter type- Parameters:
fn- the mapping function- Returns:
- a functor over B (the new parameter type)
-
zip
public final <B> IO<B> zip(Applicative<Fn1<? super A,? extends B>,IO<?>> appFn)
Given another instance of this applicative over a mapping function, "zip" the two instances together using whatever application semantics the current applicative supports.- Specified by:
zipin interfaceApplicative<A,IO<?>>- Specified by:
zipin interfaceMonad<A,IO<?>>- Specified by:
zipin interfaceMonadError<java.lang.Throwable,A,IO<?>>- Specified by:
zipin interfaceMonadRec<A,IO<?>>- Type Parameters:
B- the resulting applicative parameter type- Parameters:
appFn- the other applicative instance- Returns:
- the mapped applicative
-
lazyZip
public final <B> Lazy<IO<B>> lazyZip(Lazy<? extends Applicative<Fn1<? super A,? extends B>,IO<?>>> 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<A,IO<?>>- Specified by:
lazyZipin interfaceMonad<A,IO<?>>- Specified by:
lazyZipin interfaceMonadError<java.lang.Throwable,A,IO<?>>- Specified by:
lazyZipin interfaceMonadRec<A,IO<?>>- Type Parameters:
B- the resulting applicative parameter type- Parameters:
lazyAppFn- the lazy other applicative instance- Returns:
- the mapped applicative
- See Also:
Maybe,Either
-
discardL
public final <B> IO<B> discardL(Applicative<B,IO<?>> appB)
Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB. This is generally useful for sequentially performing side-effects.- Specified by:
discardLin interfaceApplicative<A,IO<?>>- Specified by:
discardLin interfaceMonad<A,IO<?>>- Specified by:
discardLin interfaceMonadError<java.lang.Throwable,A,IO<?>>- Specified by:
discardLin interfaceMonadRec<A,IO<?>>- Type Parameters:
B- the type of the returned Applicative's parameter- Parameters:
appB- the other Applicative- Returns:
- appB
-
discardR
public final <B> IO<A> discardR(Applicative<B,IO<?>> appB)
Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative. This is generally useful for sequentially performing side-effects.- Specified by:
discardRin interfaceApplicative<A,IO<?>>- Specified by:
discardRin interfaceMonad<A,IO<?>>- Specified by:
discardRin interfaceMonadError<java.lang.Throwable,A,IO<?>>- Specified by:
discardRin interfaceMonadRec<A,IO<?>>- Type Parameters:
B- the type of appB's parameter- Parameters:
appB- the other Applicative- Returns:
- this Applicative
-
flatMap
public final <B> IO<B> flatMap(Fn1<? super A,? extends Monad<B,IO<?>>> f)
Chain dependent computations that may continue or short-circuit based on previous results.- Specified by:
flatMapin interfaceMonad<A,IO<?>>- Specified by:
flatMapin interfaceMonadError<java.lang.Throwable,A,IO<?>>- Specified by:
flatMapin interfaceMonadRec<A,IO<?>>- Type Parameters:
B- the resulting monad parameter type- Parameters:
f- the dependent computation over A- Returns:
- the new monad instance
-
trampolineM
public <B> IO<B> trampolineM(Fn1<? super A,? extends MonadRec<RecursiveResult<A,B>,IO<?>>> 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<A,IO<?>>- Type Parameters:
B- the ultimate resulting carrier type- Parameters:
fn- the function to internally trampoline- Returns:
- the trampolined
MonadRec - See Also:
for a basic implementation,for a implementation,for an implementation leveraging an already stack-safe,for a implementation
-
throwError
public final IO<A> throwError(java.lang.Throwable throwable)
Throw an error value of typeEinto themonad.- Specified by:
throwErrorin interfaceMonadError<java.lang.Throwable,A,IO<?>>- Parameters:
throwable- the error type- Returns:
- the
monad
-
catchError
public final IO<A> catchError(Fn1<? super java.lang.Throwable,? extends Monad<A,IO<?>>> recoveryFn)
- Specified by:
catchErrorin interfaceMonadError<java.lang.Throwable,A,IO<?>>- Parameters:
recoveryFn- the catch function- Returns:
- the recovered
Monad
-
throwing
public static <A> IO<A> throwing(java.lang.Throwable t)
Produce anIOthat throws the givenThrowablewhen executed.- Type Parameters:
A- any result type- Parameters:
t- theThrowable- Returns:
- the
IO
-
interruptible
public static <A> IO<A> interruptible(IO<A> io)
Wrap the givenIOin anIOthat first checks if thethreadtheIOruns on isinterrupted. If it is, anInterruptedExceptionis thrown; otherwise the givenIOis executed as usual. Note that forIOs supporting parallelism, the thread that is checked for interruption may not necessarily be the same thread that theIOultimately runs on.
-
monitorSync
public static <A> IO<A> monitorSync(java.lang.Object lock, IO<A> io)
Synchronize the givenIOusing the provided lock object. Note that to ensure that the entirety of theIO's computation actually runs inside the synchronized region, theIOis executedsynchronouslyinside the synchronized block regardless of the caller's chosen execution strategy.
-
fuse
public static <A> IO<A> fuse(IO<A> io)
Fuse all fork opportunities of a givenIOsuch that, unless it ispinned(or is originallyexternally managed), no parallelism will be used when running it, regardless of what semantics are used when it is executed.- Type Parameters:
A- theIOresult type- Parameters:
io- theIO- Returns:
- the fused
IO - See Also:
pin(IO, Executor)
-
pin
public static <A> IO<A> pin(IO<A> io, java.util.concurrent.Executor executor)
Pin anIOto anExecutorsuch that regardless of what future decisions are made, when it runs, it will run using whatever parallelism is supported by theExecutor's threading model. Note that if thisIOhas already been pinned (or is originallyexternally managed), pinning to an additionalExecutorhas no meaningful effect.
-
memoize
public static <A> IO<A> memoize(IO<A> io)
Given anIO, return anIOthat wraps it, caches its first successful result, and guarantees that no subsequent interactions will happen with it afterwards, returning the cached result thereafter. Note that if the underlyingIOthrows, the failure will not be cached, so subsequent interactions with the memoizedIOwill again call through to the delegate until it completes normally.
-
io
public static <A> IO<A> io(A a)
Static factory method for creating anIOthat just returnsawhen performed.- Type Parameters:
A- the result type- Parameters:
a- the result- Returns:
- the
IO
-
io
public static <A> IO<A> io(Fn0<? extends A> fn0)
Static factory method for coercing a lambda to anIO.- Type Parameters:
A- the result type- Parameters:
fn0- the lambda to coerce- Returns:
- the
IO
-
io
public static IO<Unit> io(SideEffect sideEffect)
- Parameters:
sideEffect- theSideEffect- Returns:
- the
IO
-
externallyManaged
public static <A> IO<A> externallyManaged(Fn0<java.util.concurrent.CompletableFuture<A>> supplier)
Static factory method for creating anIOfrom an externally managed source ofcompletable futures.Note that constructing an
IOthis way results in no intermediate futures being constructed by eitherunsafePerformAsyncIO()orunsafePerformAsyncIO(Executor), andunsafePerformIO()is synonymous with invokingCompletableFuture.get()on the externally managed future.- Type Parameters:
A- the result type- Parameters:
supplier- the source of externally managedcompletable futures- Returns:
- the
IO
-
-