Class FunctionUtil
Provides advanced functional programming operations including composition, currying, partial application, and memoization.
提供高级函数式编程操作,包括组合、柯里化、部分应用和记忆化。
Features | 主要功能:
- compose() - Function composition - 函数组合
- curry() - Function currying - 函数柯里化
- uncurry() - Reverse currying - 反柯里化
- partial() - Partial application - 部分应用
- flip() - Flip arguments - 翻转参数
- memoize() - Result caching - 结果缓存
- identity() - Identity function - 恒等函数
- constant() - Constant function - 常量函数
Usage Examples | 使用示例:
// Composition: g(f(x))
var addOne = FunctionUtil.compose(x -> x + 1, x -> x * 2);
addOne.apply(5); // (5 + 1) * 2 = 12
// Currying
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b;
Function<Integer, Integer> add5 = FunctionUtil.curry(add).apply(5);
add5.apply(3); // 8
// Memoization
Function<Integer, Integer> fib = FunctionUtil.memoize(n ->
n <= 1 ? n : fib.apply(n - 1) + fib.apply(n - 2));
// Partial application
BiFunction<String, String, String> concat = (a, b) -> a + b;
Function<String, String> hello = FunctionUtil.partial(concat, "Hello, ");
hello.apply("World"); // "Hello, World"
Performance | 性能特性:
- compose(): O(1) - 常量时间
- curry(): O(1) - 常量时间
- memoize(): O(1) average for cache hits - 缓存命中平均 O(1)
Security | 安全性:
- Thread-safe: Yes (all methods are stateless) - 线程安全: 是
- memoize() uses ConcurrentHashMap - memoize() 使用 ConcurrentHashMap
- Since:
- JDK 25, opencode-base-functional V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <A,B, C> Function <A, C> Compose two functions: g(f(x)) 组合两个函数:g(f(x))static <A,B, C, D> Function <A, D> Compose three functions: h(g(f(x))) 组合三个函数:h(g(f(x)))static <T,R> Function <T, R> constant(R value) Constant function - always returns the same value 常量函数 - 始终返回相同的值curry(TriFunction<T1, T2, T3, R> f) Curry a TriFunction to a curried function 将 TriFunction 柯里化curry(BiFunction<A, B, R> f) Curry a BiFunction to a curried function 将 BiFunction 柯里化static <A,B, R> BiFunction <B, A, R> flip(BiFunction<A, B, R> f) Flip the arguments of a BiFunction 翻转 BiFunction 的参数static <T> Function<T, T> identity()Identity function - returns its input unchanged 恒等函数 - 原样返回输入lift(CheckedFunction<T, R> f) Lift a checked function to return Option instead of throwing 将受检函数提升为返回 Option 而非抛出异常static <T,U, R> BiFunction <T, U, Option<R>> liftBi(CheckedBiFunction<T, U, R> f) Lift a checked bi-function to return Option instead of throwing 将受检双参函数提升为返回 Option 而非抛出异常static <T,U, R> BiFunction <T, U, Try<R>> liftBiTry(CheckedBiFunction<T, U, R> f) Lift a checked bi-function to return Try instead of throwing 将受检双参函数提升为返回 Try 而非抛出异常liftTry(CheckedFunction<T, R> f) Lift a checked function to return Try instead of throwing 将受检函数提升为返回 Try 而非抛出异常static <T,R> Function <T, R> Memoize a function - cache results for repeated calls 记忆化函数 - 缓存重复调用的结果static <T,R> Function <T, R> Memoize a function with specified maximum cache size 使用指定最大缓存大小记忆化函数static <T> Supplier<T> Memoize a Supplier - cache the result of first call 记忆化 Supplier - 缓存首次调用的结果static <T> Predicate<T> Predicate negation 谓词取反static <A,B, R> Function <B, R> partial(BiFunction<A, B, R> f, A a) Partial application - fix the first argument 部分应用 - 固定第一个参数static <A,B, R> Function <A, R> partialRight(BiFunction<A, B, R> f, B b) Partial application - fix the second argument 部分应用 - 固定第二个参数static <T,U, R> BiFunction <T, U, R> unchecked(CheckedBiFunction<T, U, R> f) Convert CheckedBiFunction to standard BiFunction (wrapping exceptions) 将 CheckedBiFunction 转换为标准 BiFunction(包装异常)static <T,R> Function <T, R> unchecked(CheckedFunction<T, R> f) Convert CheckedFunction to standard Function (wrapping exceptions) 将 CheckedFunction 转换为标准 Function(包装异常)static <A,B, R> BiFunction <A, B, R> Uncurry a curried function back to BiFunction 将柯里化函数还原为 BiFunction
-
Method Details
-
compose
Compose two functions: g(f(x)) 组合两个函数:g(f(x))Example | 示例:
compose(x -> x + 1, x -> x * 2).apply(5) = 12 // (5+1)*2
- Type Parameters:
A- input type - 输入类型B- intermediate type - 中间类型C- output type - 输出类型- Parameters:
f- first function (applied first) - 第一个函数(先应用)g- second function (applied to result of f) - 第二个函数(应用于 f 的结果)- Returns:
- composed function - 组合后的函数
-
compose
Compose three functions: h(g(f(x))) 组合三个函数:h(g(f(x)))- Type Parameters:
A- input type - 输入类型B- first intermediate type - 第一个中间类型C- second intermediate type - 第二个中间类型D- output type - 输出类型- Parameters:
f- first function - 第一个函数g- second function - 第二个函数h- third function - 第三个函数- Returns:
- composed function - 组合后的函数
-
curry
Curry a BiFunction to a curried function 将 BiFunction 柯里化Example | 示例:
BiFunction<Integer, Integer, Integer> add = (a, b) -> a + b; Function<Integer, Function<Integer, Integer>> curried = curry(add); curried.apply(5).apply(3) = 8
- Type Parameters:
A- first input type - 第一个输入类型B- second input type - 第二个输入类型R- result type - 结果类型- Parameters:
f- function to curry - 要柯里化的函数- Returns:
- curried function - 柯里化后的函数
-
curry
public static <T1,T2, Function<T1, Function<T2, Function<T3,T3, R> R>>> curry(TriFunction<T1, T2, T3, R> f) Curry a TriFunction to a curried function 将 TriFunction 柯里化- Type Parameters:
T1- first input type - 第一个输入类型T2- second input type - 第二个输入类型T3- third input type - 第三个输入类型R- result type - 结果类型- Parameters:
f- function to curry - 要柯里化的函数- Returns:
- curried function - 柯里化后的函数
-
uncurry
Uncurry a curried function back to BiFunction 将柯里化函数还原为 BiFunction- Type Parameters:
A- first input type - 第一个输入类型B- second input type - 第二个输入类型R- result type - 结果类型- Parameters:
f- curried function - 柯里化函数- Returns:
- uncurried BiFunction - 反柯里化后的 BiFunction
-
partial
Partial application - fix the first argument 部分应用 - 固定第一个参数Example | 示例:
BiFunction<String, String, String> concat = (a, b) -> a + b; Function<String, String> hello = partial(concat, "Hello, "); hello.apply("World") = "Hello, World"- Type Parameters:
A- first input type - 第一个输入类型B- second input type - 第二个输入类型R- result type - 结果类型- Parameters:
f- function - 函数a- first argument (fixed) - 第一个参数(固定)- Returns:
- partially applied function - 部分应用后的函数
-
partialRight
Partial application - fix the second argument 部分应用 - 固定第二个参数- Type Parameters:
A- first input type - 第一个输入类型B- second input type - 第二个输入类型R- result type - 结果类型- Parameters:
f- function - 函数b- second argument (fixed) - 第二个参数(固定)- Returns:
- partially applied function - 部分应用后的函数
-
flip
Flip the arguments of a BiFunction 翻转 BiFunction 的参数Example | 示例:
BiFunction<String, Integer, String> repeat = (s, n) -> s.repeat(n); BiFunction<Integer, String, String> flipped = flip(repeat); flipped.apply(3, "ab") = "ababab"
- Type Parameters:
A- first input type - 第一个输入类型B- second input type - 第二个输入类型R- result type - 结果类型- Parameters:
f- function - 函数- Returns:
- function with flipped arguments - 参数翻转后的函数
-
memoize
Memoize a function - cache results for repeated calls 记忆化函数 - 缓存重复调用的结果Uses ConcurrentHashMap for thread-safe caching with a default maximum size of 1000 entries (LRU eviction).
使用 ConcurrentHashMap 实现线程安全缓存,默认最大 1000 条记录(LRU 淘汰)。
Example | 示例:
Function<Integer, Integer> fib = memoize(n -> n <= 1 ? n : fib.apply(n-1) + fib.apply(n-2)); fib.apply(10); // Computed fib.apply(10); // Cached result- Type Parameters:
T- input type - 输入类型R- result type - 结果类型- Parameters:
f- function to memoize - 要记忆化的函数- Returns:
- memoized function - 记忆化后的函数
-
memoize
Memoize a function with specified maximum cache size 使用指定最大缓存大小记忆化函数Uses LRU (Least Recently Used) eviction when cache is full. Thread-safe: Computation runs outside the lock to avoid blocking other keys. Note: For the same uncached key, concurrent callers may compute simultaneously; only the first result is cached (subsequent results are discarded).
缓存满时使用 LRU(最近最少使用)淘汰策略。 线程安全:计算在锁外执行以避免阻塞其他 key。 注意:对于同一个未缓存的 key,并发调用者可能同时计算; 仅第一个结果被缓存(后续结果被丢弃)。
Example | 示例:
// Cache only last 100 results Function<String, Data> fetch = memoize(api::fetchData, 100);
- Type Parameters:
T- input type - 输入类型R- result type - 结果类型- Parameters:
f- function to memoize - 要记忆化的函数maxSize- maximum cache size (must be positive) - 最大缓存大小(必须为正数)- Returns:
- memoized function - 记忆化后的函数
- Throws:
IllegalArgumentException- if maxSize is not positive
-
memoize
-
unchecked
Convert CheckedFunction to standard Function (wrapping exceptions) 将 CheckedFunction 转换为标准 Function(包装异常)- Type Parameters:
T- input type - 输入类型R- result type - 结果类型- Parameters:
f- checked function - 受检函数- Returns:
- unchecked function - 非受检函数
-
unchecked
Convert CheckedBiFunction to standard BiFunction (wrapping exceptions) 将 CheckedBiFunction 转换为标准 BiFunction(包装异常)- Type Parameters:
T- first input type - 第一个输入类型U- second input type - 第二个输入类型R- result type - 结果类型- Parameters:
f- checked function - 受检函数- Returns:
- unchecked function - 非受检函数
-
identity
Identity function - returns its input unchanged 恒等函数 - 原样返回输入- Type Parameters:
T- type - 类型- Returns:
- identity function - 恒等函数
-
constant
Constant function - always returns the same value 常量函数 - 始终返回相同的值Example | 示例:
Function<Object, String> always = constant("default"); always.apply(anything) = "default"- Type Parameters:
T- input type (ignored) - 输入类型(被忽略)R- result type - 结果类型- Parameters:
value- constant value to return - 要返回的常量值- Returns:
- constant function - 常量函数
-
not
-
lift
Lift a checked function to return Option instead of throwing 将受检函数提升为返回 Option 而非抛出异常On success, returns Option.some(result). On exception, returns Option.none().
成功时返回 Option.some(result)。异常时返回 Option.none()。
Example | 示例:
Function<String, Option<Integer>> safeParse = FunctionUtil.lift(Integer::parseInt); safeParse.apply("123"); // Option.some(123) safeParse.apply("abc"); // Option.none()- Type Parameters:
T- input type - 输入类型R- result type - 结果类型- Parameters:
f- checked function to lift - 要提升的受检函数- Returns:
- function returning Option - 返回 Option 的函数
- Since:
- JDK 25, opencode-base-functional V1.0.3
-
liftTry
Lift a checked function to return Try instead of throwing 将受检函数提升为返回 Try 而非抛出异常On success, returns Try.success(result). On exception, returns Try.failure(exception).
成功时返回 Try.success(result)。异常时返回 Try.failure(exception)。
Example | 示例:
Function<String, Try<Integer>> safeParse = FunctionUtil.liftTry(Integer::parseInt); safeParse.apply("123"); // Try.success(123) safeParse.apply("abc"); // Try.failure(NumberFormatException)- Type Parameters:
T- input type - 输入类型R- result type - 结果类型- Parameters:
f- checked function to lift - 要提升的受检函数- Returns:
- function returning Try - 返回 Try 的函数
- Since:
- JDK 25, opencode-base-functional V1.0.3
-
liftBi
Lift a checked bi-function to return Option instead of throwing 将受检双参函数提升为返回 Option 而非抛出异常On success, returns Option.some(result). On exception, returns Option.none().
成功时返回 Option.some(result)。异常时返回 Option.none()。
Example | 示例:
BiFunction<String, Integer, Option<String>> safeSub = FunctionUtil.liftBi((s, len) -> s.substring(0, len)); safeSub.apply("hello", 3); // Option.some("hel") safeSub.apply("hello", 100); // Option.none()- Type Parameters:
T- first input type - 第一个输入类型U- second input type - 第二个输入类型R- result type - 结果类型- Parameters:
f- checked bi-function to lift - 要提升的受检双参函数- Returns:
- bi-function returning Option - 返回 Option 的双参函数
- Since:
- JDK 25, opencode-base-functional V1.0.3
-
liftBiTry
Lift a checked bi-function to return Try instead of throwing 将受检双参函数提升为返回 Try 而非抛出异常On success, returns Try.success(result). On exception, returns Try.failure(exception).
成功时返回 Try.success(result)。异常时返回 Try.failure(exception)。
Example | 示例:
BiFunction<String, Integer, Try<String>> safeSub = FunctionUtil.liftBiTry((s, len) -> s.substring(0, len)); safeSub.apply("hello", 3); // Try.success("hel") safeSub.apply("hello", 100); // Try.failure(StringIndexOutOfBoundsException)- Type Parameters:
T- first input type - 第一个输入类型U- second input type - 第二个输入类型R- result type - 结果类型- Parameters:
f- checked bi-function to lift - 要提升的受检双参函数- Returns:
- bi-function returning Try - 返回 Try 的双参函数
- Since:
- JDK 25, opencode-base-functional V1.0.3
-