Package cloud.opencode.base.functional.optics


package cloud.opencode.base.functional.optics
Optics - Lens and other optical types for immutable data 光学类型 - 用于不可变数据的 Lens 和其他光学类型

Provides Lens and related optical types for accessing and updating deeply nested immutable data structures in a composable way.

提供 Lens 和相关光学类型,以可组合的方式访问和更新深层嵌套的不可变数据结构。

Features | 主要功能:

Usage Examples | 使用示例:

record Address(String city, String street) {}
record Person(String name, Address address) {}

// Create lenses
Lens<Person, Address> addressLens = Lens.of(
    Person::address,
    (p, a) -> new Person(p.name(), a));

Lens<Address, String> cityLens = Lens.of(
    Address::city,
    (a, c) -> new Address(c, a.street()));

// Compose lenses
Lens<Person, String> personCityLens = addressLens.compose(cityLens);

// Update deeply nested value
Person person = new Person("Alice", new Address("NYC", "5th Ave"));
Person updated = personCityLens.modify(person, String::toUpperCase);
// Result: Person("Alice", Address("NYC", "5th Ave"))

Benefits | 优势:

  • Type-safe nested updates - 类型安全的嵌套更新
  • Composable accessors - 可组合的访问器
  • Immutable data manipulation - 不可变数据操作
Since:
JDK 25, opencode-base-functional V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Classes
    Class
    Description
    Lens<S,A>
    Lens - Functional lens for immutable data access and modification Lens - 用于不可变数据访问和修改的函数式透镜
    OptionalLens - Functional lens for optional data access OptionalLens - 用于可选数据访问的函数式透镜