Cheat Sheet

What You'll Learn

  • Quick reference for all Path types, creation, and extraction
  • Common operators at a glance
  • How to get back to standard Java from any Path
  • Type conversion routes between Path types

Path Types

Path TypeForCreateExtract
MaybePath<A>AbsencePath.maybe(v), Path.just(v), Path.nothing().run()Maybe<A>
EitherPath<E, A>Typed errorsPath.right(v), Path.left(e), Path.either(eitherValue).run()Either<E, A>
TryPath<A>ExceptionsPath.tryOf(() -> ...), Path.success(v), Path.failure(ex).run()Try<A>
ValidationPath<E, A>Accumulating errorsPath.valid(v, sg), Path.invalid(e, sg).run()Validated<E, A>
IOPath<A>Deferred side effectsPath.io(() -> ...), Path.ioPure(v).unsafeRun()A
VTaskPath<A>Virtual threadsPath.vtask(() -> ...), Path.vtaskPure(v).unsafeRun()A
ReaderPath<R, A>Dependency injectionPath.reader(r), Path.ask(), Path.asks(fn).run(env)A
WriterPath<W, A>Logging, auditPath.writer(w, m), Path.tell(log, m).run()Writer<W, A>
WithStatePath<S, A>Stateful computationPath.state(s), Path.getState().run(initial)(S, A)
TrampolinePath<A>Stack-safe recursionPath.trampolineDone(v), Path.trampolineDefer(s).run()A
LazyPath<A>Deferred, memoisedPath.lazyDefer(() -> ...), Path.lazyNow(v).run()A
CompletableFuturePath<A>AsyncPath.future(cf), Path.futureAsync(() -> ...).run()CompletableFuture<A>
ListPath<A>Batch processing with positional zippingPath.listPath(list).run()List<A>
StreamPath<A>Lazy sequencesPath.stream(stream).run()Stream<A>
NonDetPath<A>Non-deterministic search, combinationsPath.list(list).run()List<A>
IdPath<A>Pure valuesPath.id(v).run()Id<A>
OptionalPath<A>java.util.Optional bridgePath.optional(opt), Path.present(v).run()Optional<A>
FreePath<F, A>DSL buildingPath.freePure(v, f), Path.freeLift(fa, f).foldMap(nat, m)
GenericPath<F, A>Custom monadsPath.generic(kind, monad).run()Kind<F, A>

Operators

Transformation

OperatorWhat It DoesAvailable On
.map(fn)Transform the success valueAll paths
.peek(fn)Observe the value without changing itAll paths
.focus(lens)Navigate into a nested field via opticsMaybePath, EitherPath, TryPath, IOPath, ValidationPath

Chaining

OperatorWhat It DoesAvailable On
.via(fn)Chain to another path (monadic bind)All chainable paths
.flatMap(fn)Alias for viaAll chainable paths
.then(supplier)Sequence, ignoring previous valueAll chainable paths

Combination

OperatorWhat It DoesAvailable On
.zipWith(other, fn)Combine two paths (fail-fast)All combinable paths
.zipWith3(b, c, fn)Combine three paths (fail-fast)Most paths
.zipWithAccum(other, fn)Combine, accumulating errorsValidationPath
.zipWith3Accum(b, c, fn)Combine three, accumulating errorsValidationPath
.andAlso(other)Accumulate errors, keep left valueValidationPath

Error Handling

OperatorWhat It DoesAvailable On
.recover(fn)Error → success valueMaybePath, EitherPath, TryPath, ValidationPath
.recoverWith(fn)Error → new pathMaybePath, EitherPath, TryPath, ValidationPath
.orElse(supplier)Try an alternative pathMaybePath, EitherPath, TryPath, ValidationPath
.mapError(fn)Transform the error typeEitherPath, TryPath, ValidationPath
.bimap(errFn, okFn)Transform error and success togetherEitherPath
.handleError(fn)Throwable → recovery valueIOPath, VTaskPath (via Effectful)
.handleErrorWith(fn)Throwable → recovery effectIOPath, VTaskPath (via Effectful)
.guarantee(runnable)Run cleanup whether success or failureIOPath, VTaskPath (via Effectful)
.peekLeft(fn)Observe the error without changing itEitherPath
.peekFailure(fn)Observe the exception without changing itTryPath

Extraction

OperatorWhat It DoesAvailable On
.run()Extract the underlying valueAll paths
.unsafeRun()Execute a deferred effect (may throw)IOPath, VTaskPath
.runSafe()Execute safely, returning TryIOPath, VTaskPath
.fold(onErr, onOk)Handle both tracksEitherPath, TryPath, ValidationPath
.fold(identity, op)Reduce a sequence to one valueListPath, StreamPath, VStreamPath
.foldLeft(seed, fn) / .foldRight(seed, fn)Directional reduce (type-changing)ListPath, StreamPath
.foldMap(monoid, fn)Map then combine via a MonoidListPath, StreamPath, VStreamPath
.getOrElse(default)Extract or use defaultMaybePath, EitherPath, TryPath, ValidationPath
.getOrElseGet(supplier)Extract or compute defaultMaybePath, EitherPath, TryPath, ValidationPath

Escape Hatches

Getting back to standard Java from any Path:

FromToHow
MaybePath<A>Maybe<A>.run()
MaybePath<A>Optional<A>.run().toOptional()
MaybePath<A>A (or default).getOrElse(defaultValue)
EitherPath<E, A>Either<E, A>.run()
EitherPath<E, A>A (or default).getOrElse(defaultValue)
EitherPath<E, A>B.run().fold(onErr, onOk)
TryPath<A>Try<A>.run()
TryPath<A>A (or default).getOrElse(defaultValue)
ValidationPath<E, A>Validated<E, A>.run()
ValidationPath<E, A>B.fold(onInvalid, onValid)
IOPath<A>A.unsafeRun()
IOPath<A>Try<A>.runSafe()
VTaskPath<A>A.unsafeRun()
VTaskPath<A>Try<A>.runSafe()

Type-Class Instances

One entry point, Instances.x(...), for every type-class instance. See Obtaining Instances.

import org.higherkindedj.hkt.instances.Instances;
import static org.higherkindedj.hkt.instances.Witnesses.*;
NeedCall
Functor / Applicative / MonadInstances.monad(maybe()) (also functor, applicative)
MonadError (Maybe, Optional, Try, Either, ...)Instances.monadError(optional()) (E inferred from target)
MonadZero / Alternative (Maybe, Optional, List, Stream)Instances.monadZero(list()) / Instances.alternative(maybe())
Phantom-typed (Either, Reader, Context, State)Instances.monad(either()) (L/R/S inferred from target)
Validated (needs a Semigroup)Instances.validated(Semigroups.list())
Writer (needs a Monoid)Instances.writer(Monoids.string())
Transformers (need the outer Monad)Instances.eitherT(outer), maybeT, optionalT, readerT, stateT, writerT(outer, monoid)

Tokens: maybe() io() list() optional() try_() vtask() vstream() lazy() stream() completableFuture() trampoline() id() either() reader() context() state(). The monadError/monadZero/alternative lookups are partial — only valid where the type implements that capability.


Type Conversions

FromToMethod
MaybePath<A>EitherPath<E, A>.toEitherPath(error)
MaybePath<A>TryPath<A>.toTryPath(exceptionSupplier)
MaybePath<A>ValidationPath<E, A>.toValidationPath(error, semigroup)
MaybePath<A>OptionalPath<A>.toOptionalPath()
EitherPath<E, A>MaybePath<A>.toMaybePath()
EitherPath<E, A>TryPath<A>.toTryPath(errorToException)
EitherPath<E, A>ValidationPath<E, A>.toValidationPath(semigroup)
EitherPath<E, A>OptionalPath<A>.toOptionalPath()
TryPath<A>EitherPath<E, A>.toEitherPath(exceptionToError)
TryPath<A>MaybePath<A>.toMaybePath()
ValidationPath<E, A>EitherPath<E, A>.toEitherPath()
ValidationPath<E, A>MaybePath<A>.toMaybePath()
FocusPath<S, A>MaybePath<A>.toMaybePath()
FocusPath<S, A>EitherPath<E, A>.toEitherPath(errorFn)
AffinePath<S, A>MaybePath<A>.toMaybePath()
AffinePath<S, A>EitherPath<E, A>.toEitherPath(errorFn)

Previous: Quickstart Next: Effect Path API