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 monad
  • Either - Either/Or type (Left/Right)
  • Option - Optional value (Some/None)
  • Validation - Accumulating validation
  • Lazy - 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:
  • Class
    Description
    Either<L,R>
    Either Monad - Represents one of two possible values (Left or Right) Either Monad - 表示两种可能值之一(Left 或 Right)
    Left - Represents the left case (typically error) Left - 表示左情况(通常为错误)
    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-comprehension builder for one Iterable.
    For-comprehension builder for two Iterables.
    For-comprehension builder for three Iterables.
    For-comprehension builder for one Option.
    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-comprehension builder for one Sequence.
    For-comprehension builder for two Sequences.
    For-comprehension builder for three Sequences.
    For-comprehension builder for one Try.
    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 Monad - Enhanced Optional with more functional operations Option Monad - 增强的 Optional,提供更多函数式操作
    None - Represents absence of value None - 表示值缺失
    Some - Contains a value Some - 包含值
    Sequence - Lazy evaluated sequence Sequence - 惰性求值序列
    Value with its index.
    Trampoline - Stack-safe recursion using trampolining Trampoline - 使用蹦床模式的栈安全递归
    Completed Trampoline with a result.
    FlatMapped Trampoline for chaining computations.
    Suspended Trampoline with a continuation.
    Try<T>
    Try Monad - Encapsulates computations that may fail Try Monad - 封装可能失败的计算
    Failure - Represents a failed computation Failure - 表示失败的计算
    Success - Represents a successful computation Success - 表示成功的计算
    Unit - Represents a valueless result (void equivalent as a type) Unit - 表示无值结果(void 的类型等价物)
    Validation Monad - Accumulating error validation Validation Monad - 累积错误的验证
    Invalid - Represents a failed validation with accumulated errors Invalid - 表示失败的验证,包含累积的错误
    Valid - Represents a successful validation Valid - 表示成功的验证