Package cloud.opencode.base.functional.monad
package cloud.opencode.base.functional.monad
Monad Types - Functional containers for computation
Monad 类型 - 计算的函数式容器
Provides standard Monad types for functional programming including Try, Either, Option, Validation, and Lazy evaluation.
提供函数式编程的标准 Monad 类型,包括 Try、Either、Option、Validation 和惰性求值。
Monad Types | Monad 类型:
Try- Exception handling monadEither- Either/Or type (Left/Right)Option- Optional value (Some/None)Validation- Accumulating validationLazy- Lazy evaluation container
Usage Examples | 使用示例:
// Try - Handle exceptions functionally
Try<Integer> result = Try.of(() -> Integer.parseInt(input))
.map(n -> n * 2)
.recover(e -> 0);
// Either - Express two possible outcomes
Either<Error, User> user = findUser(id)
.map(User::activate);
// Validation - Accumulate multiple errors
Validation<String, User> validated = Validation.combine(
validateName(name),
validateAge(age),
validateEmail(email),
User::new
);
Thread Safety | 线程安全:
All Monad types are immutable and thread-safe.
所有 Monad 类型都是不可变且线程安全的。
- Since:
- JDK 25, opencode-base-functional V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
ClassDescriptionEither<L,
R> Either Monad - Represents one of two possible values (Left or Right) Either Monad - 表示两种可能值之一(Left 或 Right)Either.Left<L,R> Left - Represents the left case (typically error) Left - 表示左情况(通常为错误)Either.Right<L,R> Right - Represents the right case (typically success) Right - 表示右情况(通常为成功)For - For-comprehension for simplified flatMap operations For - 简化 flatMap 操作的 For 表达式For.Function3<T1,T2, T3, R> Function with 3 parameters. 3 参数函数。For.Function4<T1,T2, T3, T4, R> Function with 4 parameters. 4 参数函数。For.Function5<T1,T2, T3, T4, T5, R> Function with 5 parameters. 5 参数函数。For.IterableFor1<T1>For-comprehension builder for one Iterable.For.IterableFor2<T1,T2> For-comprehension builder for two Iterables.For.IterableFor3<T1,T2, T3> For-comprehension builder for three Iterables.For.OptionFor1<T1>For-comprehension builder for one Option.For.OptionFor2<T1,T2> For-comprehension builder for two Options.For.OptionFor3<T1,T2, T3> For-comprehension builder for three Options.For.OptionFor4<T1,T2, T3, T4> For-comprehension builder for four Options.For.SequenceFor1<T1>For-comprehension builder for one Sequence.For.SequenceFor2<T1,T2> For-comprehension builder for two Sequences.For.SequenceFor3<T1,T2, T3> For-comprehension builder for three Sequences.For.TryFor1<T1>For-comprehension builder for one Try.For.TryFor2<T1,T2> For-comprehension builder for two Tries.For.TryFor3<T1,T2, T3> For-comprehension builder for three Tries.For.TryFor4<T1,T2, T3, T4> For-comprehension builder for four Tries.Lazy<T>Lazy - Lazy evaluation container Lazy - 惰性求值容器Option<T>Option Monad - Enhanced Optional with more functional operations Option Monad - 增强的 Optional,提供更多函数式操作Option.None<T>None - Represents absence of value None - 表示值缺失Option.Some<T>Some - Contains a value Some - 包含值Sequence<T>Sequence - Lazy evaluated sequence Sequence - 惰性求值序列Value with its index.Trampoline<T>Trampoline - Stack-safe recursion using trampolining Trampoline - 使用蹦床模式的栈安全递归Completed Trampoline with a result.Trampoline.FlatMap<A,B> FlatMapped Trampoline for chaining computations.Suspended Trampoline with a continuation.Try<T>Try Monad - Encapsulates computations that may fail Try Monad - 封装可能失败的计算Try.Failure<T>Failure - Represents a failed computation Failure - 表示失败的计算Try.Success<T>Success - Represents a successful computation Success - 表示成功的计算Unit - Represents a valueless result (void equivalent as a type) Unit - 表示无值结果(void 的类型等价物)Validation<E,T> Validation Monad - Accumulating error validation Validation Monad - 累积错误的验证Validation.Invalid<E,T> Invalid - Represents a failed validation with accumulated errors Invalid - 表示失败的验证,包含累积的错误Validation.Valid<E,T> Valid - Represents a successful validation Valid - 表示成功的验证