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<Throwable, A, IO<?>>, MonadRec<A, IO<?>>
- Direct Known Subclasses:
IO.Compose
-
Nested Class Summary
Nested Classes -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal <B> IO<B> discardL(Applicative<B, IO<?>> appB) Sequence both thisApplicativeandappB, discarding thisApplicative'sresult and returningappB.discardR(Applicative<B, IO<?>> appB) Sequence both thisApplicativeandappB, discardingappB'sresult and returning thisApplicative.exceptionally(Fn1<? super Throwable, ? extends A> recoveryFn) Deprecated.static <A> IO<A> externallyManaged(Fn0<CompletableFuture<A>> supplier) Static factory method for creating anIOfrom an externally managed source ofcompletable futures.final <B> IO<B> Chain dependent computations that may continue or short-circuit based on previous results.final <B> IO<B> Covariantly transmute this functor's parameter using the given mapping function.static <A> IO<A> 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> Static factory method for coercing a lambda to anIO.io(SideEffect sideEffect) 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> static <A> IO<A> monitorSync(Object lock, IO<A> io) Synchronize the givenIOusing the provided lock object.static <A> IO<A> final <B> IO<B> pure(B b) Lift the valuebinto this applicative functor.pureIO()safe()throwError(Throwable throwable) Throw an error value of typeEinto themonad.static <A> IO<A> <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.final CompletableFuture<A> Returns aCompletableFuturerepresenting the result of this eventual effect.abstract CompletableFuture<A> unsafePerformAsyncIO(Executor executor) Returns aCompletableFuturerepresenting the result of this eventual effect.abstract ARun the effect represented by thisIOinstance, blocking the current thread until the effect terminates.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.
-
Constructor Details
-
IO
private IO()
-
-
Method Details
-
unsafePerformIO
-
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
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:
-
exceptionally
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
-
ensuring
-
safe
-
pure
Lift the valuebinto this applicative functor.- Specified by:
purein interfaceApplicative<A,IO<?>> - Specified by:
purein interfaceMonad<A,IO<?>> - Specified by:
purein interfaceMonadError<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
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<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
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<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<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:
-
discardL
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<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
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<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
Chain dependent computations that may continue or short-circuit based on previous results.- Specified by:
flatMapin interfaceMonad<A,IO<?>> - Specified by:
flatMapin interfaceMonadError<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
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:
-
throwError
Throw an error value of typeEinto themonad.- Specified by:
throwErrorin interfaceMonadError<Throwable, A, IO<?>>- Parameters:
throwable- the error type- Returns:
- the
monad
-
catchError
- Specified by:
catchErrorin interfaceMonadError<Throwable, A, IO<?>>- Parameters:
recoveryFn- the catch function- Returns:
- the recovered
Monad
-
throwing
-
interruptible
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.- Type Parameters:
A- theIOresult type- Parameters:
io- theIOto wrap- Returns:
- an
IOthat first checks forthread interrupts
-
monitorSync
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
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. -
pin
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
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
-
io
-
io
- Parameters:
sideEffect- theSideEffect- Returns:
- the
IO
-
externallyManaged
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
-
pureIO
-
catchError(Fn1)