Monad
- It is a concept of functional programming
- They are containers that allow us to handle the effects outside the function’s scope, preserving the function’s purity.
- They enable us to focus on the main scenario and handle the corner cases outside of the main logic.
- Examples:
Optional
Stream or List
Future or Promise
Mono or CompletableFuture
Either
- References
Monad
- Monadic Type
- Operations
- Unit — Type converter to Monadic Type
- Bind — Can be chained together to give another Monads
Example
- Monadic Type:
Optional<T>
- Operations:
- Unit:
Optional.ofNullable(x): converts x to Optional
- Bind:
flatMap(T -> Optional<U>) returns another Optional
If flatMap monadic, Is map also monadic?