Example Index
Every runnable example in org.higherkindedj.example.basic, with what it shows and the page that explains it.
Each example is a class with a main method in the hkj-examples module. Open the class in your IDE and run it, or run it from the command line:
./gradlew :hkj-examples:run -PmainClass=org.higherkindedj.example.basic.either.EitherExample
A test checks that every runnable class in the package has a row. The build compiles these examples; the code on the book's own pages, in org.higherkindedj.example.book, is tested as well.
Failure
| Example | What it shows | Page |
|---|---|---|
EitherExample | Right and Left, a chain of flatMaps that stops at the first Left, fold, and recovery with handleErrorWith | Either |
EitherOrBothExample | A Left, a Right, or Both at once: a result that carries warnings | EitherOrBoth |
NonEmptyListExample | A list that always holds at least one element | NonEmptyList |
TryExample | Success and Failure, flatMap, which passes a Failure through, and recovery with recover and recoverWith | Try |
TryAttemptExample | Try.attempt around Java methods that throw checked exceptions | Try |
ValidatedMonadExample | Valid and Invalid, flatMap stopping at the first Invalid, and recovery with handleErrorWith | Validated |
ValidatedAssemblyExample | A record built from fields validated one by one, with every error kept in field order | Accumulating Assembly |
GeneratedAssemblyExample | The companion that @GenerateAssembly generates, with one method per component | Accumulating Assembly |
Absence
| Example | What it shows | Page |
|---|---|---|
MaybeExample | Just and Nothing, map, flatMap, and recovery from Nothing with handleErrorWith | Maybe |
OptionalExample | Java's Optional through the Kind encoding: map, flatMap, and a default with handleErrorWith | Optional |
Deferred
| Example | What it shows | Page |
|---|---|---|
IOExample | Console effects described with delay, composed with map and flatMap, and run with unsafeRunSync | IO |
LazyExample | A computation deferred until force, then kept, so that it runs once | Lazy |
CompletableFutureExample | map and flatMap over futures, and a failed future recovered with handleErrorWith | CompletableFuture |
Virtual threads
| Example | What it shows | Page |
|---|---|---|
VStreamBasicExample | Building a stream, transforming it and running it, its laziness, an effectful unfold, and recovery | VStream |
VStreamHKTExample | VStream's instances: code written for any Monad, Foldable's foldMap, and Alternative's orElse and guard | HKT and Type Classes |
VStreamAdvancedExample | bracket and onFinalize, StreamTraversal, Flow.Publisher, and natural transformations between List and VStream | Advanced Features |
Environment and state
| Example | What it shows | Page |
|---|---|---|
ReaderExample | Values read from a configuration, chained with flatMap and run with runReader | Reader |
StateExample | A stack's push and pop as State steps | State |
BankAccountWorkflow | Deposits and withdrawals threaded through State and composed with For | State |
WriterExample | A log kept beside the result with tell, and joined across flatMap | Writer |
Collections
| Example | What it shows | Page |
|---|---|---|
ListMonadExample | map and flatMap on a List, with a step that gives several results or none | List |
StreamExample | Java's Stream through the type classes: map, flatMap, taking and dropping, and laziness | Stream |
PCollectionsExample | PCollections' persistent lists through ListKind, since each is a java.util.List | PCollections Integration |
Programs as data
| Example | What it shows | Page |
|---|---|---|
IdExample | Id, which adds nothing: map, flatMap, and StateT over Id | Identity |
TrampolineExample | Recursion that does not overflow the stack | Trampoline |
FreeApplicativeExample | A Free Applicative program built and then interpreted | Free Applicative |
StaticAnalysisExample | A Free Applicative program read before it runs | Free Applicative |
PermissionCheckingExample | The permissions a program of file operations needs, checked before it runs | Free Applicative |
ParallelDataFetchExample | Independent fetches held as one program, which an interpreter could run in parallel | Free Applicative |
CoyonedaExample | maps held by Coyoneda and applied together by lower | Coyoneda |
ConstExample | Const, which holds one value that second leaves alone, and its Bifunctor | Const |
Type classes
| Example | What it shows | Page |
|---|---|---|
GenericExample | One function written for any Functor, called on a List and an Optional | Working with Kind |
InstancesFacadeExample | Instances fetched through the Instances facade | Obtaining Instances |
AlternativeConfigExample | Configuration read from the first source that has it, with Alternative | Alternative |
BifunctorExample | bimap, first and second on two-sided types | Bifunctor |
ProfunctorExample | lmap, rmap and dimap adapting a function's input and output | Profunctor |
FoldableExample | One structure reduced several ways, with foldMap and different Monoids | Foldable and Traverse |
OptionalMonoidExample | Data aggregated with firstOptional, maximum and minimum | Semigroup and Monoid |
UpdateMonoidExample | Named Updates composed with Monoids.update() and folded | Semigroup and Monoid |
NaturalTransformationExample | Natural transformations from one container to another | Natural Transformation |
For comprehensions
| Example | What it shows | Page |
|---|---|---|
ForComprehensionExample | For over several monads, among them List, Maybe and StateT: from, when, let and yield | For Comprehension |
ForTraverseComprehensionExample | traverse, sequence and flatTraverse inside For and ForPath | For Comprehension |
ForStateExample | ForState workflows that update their state through lenses | ForState Comprehension |
MTLCapabilitiesExample | MonadReader, MonadState and MonadWriter, each in a function written against that capability alone | MTL Capabilities |
EnhancedOpticsIntegrationExample | ForState steps through traversals and Isos, and For's through, on a department's records | ForState Comprehension |
Monad transformers
| Example | What it shows | Page |
|---|---|---|
EitherTExample | Validation with Either over asynchronous CompletableFuture steps | EitherT |
MaybeTExample | Asynchronous lookups that may find nothing: a user, then their preferences | MaybeT |
OptionalTExample | Asynchronous lookups with Optional: a user, their profile, then their preferences, with a default | OptionalT |
ReaderTExample | The four ways to build a ReaderT: of, liftF, reader and ask | ReaderT |
ReaderTAsyncExample | An asynchronous workflow that reads its configuration, run against two configurations | ReaderT |
ReaderTAsyncUnitExample | An initialisation step that reads its configuration and gives Unit | ReaderT |
StateTExample | StateT over Optional: building one, map and flatMap, and a negative starting state that gives Optional.empty() | StateT |
StateTStackExample | A stack over Optional, where popping an empty stack gives Optional.empty() | StateT |
WriterTExample | WriterT over Id: tell, listen and censor | WriterT |
For Effect Path, optics and whole-application examples, see the Examples Gallery.
Previous: Laws and Where HKJ Bends Them