Skip to content

2bllw8/unf

Repository files navigation

unF

GitHub CI Maven Central

A functional programming library for modern Java (21+).

Key features

  • Optics: a first-class, composable notion of substructure.
    • Annotation Processor: automatically generate optics for record classes
      • For each record component, generate a Lens
      • For each record component of type List, also generate a Traversal
  • Either monad: represent values with two possibilities.
  • Maybe monad: encapsulate an optional value.
  • Result monad: encapsulate potentially failing computations.
  • Function interfaces with partial application.

Usage

dependencies {
  // Core library
  implementation "cc.chokoka:unf.lib:2.1.0"
  // Optional annotation processor for automatic lens generation for record
  // classes
  annotationProcessor "cc.chokoka:unf.processor:2.1.0"
}

Demo: Record Optics

import unf.maybe.Just;
import unf.maybe.Maybe;
import unf.optics.RecordOptics;

@RecordOptics
record Rec1(int i, Maybe<String> s) {
}

@RecordOptics
record Rec0(Rec1 r, boolean b) {
}

public final class Demo {

  public static void main(String[] args) {
    final Rec0 r = new Rec0(new Rec1(1, new Just<>("one")), true);
    final Rec0 newR = Rec0Optics.r.i.over(i -> i + 2, r);

    System.out.println(newR);
    // Rec0[r=Rec1[i=3, s="one"], b=true]
  }
}

Changelog

See the CHANGELOG.md file.

Development

  • Build: ./gradlew assemble
  • Test: ./gradlew check

Project Structure

  • lib: main module, the library itself.
  • processor: annotation processor that enables automatic generation of optics instances for record components.

About

A functional programming library for modern Java (21+)

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages