Getting Started
dmx-fun is a functional programming library for Java that provides essential tools and utilities for writing functional code in a type-safe and composable way.
Installation
Maven
Add the following dependency to your pom.xml:
<dependency> <groupId>codes.domix</groupId> <artifactId>fun</artifactId> <version>0.0.7</version></dependency>Gradle
Add the following to your build.gradle:
implementation 'codes.domix:fun:0.0.7'Requirements
- Java 24 or higher
- No additional dependencies required
Core Concepts
Option
The Option type represents an optional value. It's a type-safe alternative to null.
import codes.domix.fun.Option;
class OptionExample { void main() { // Create an Option Option<String> some = Option.some("Hello"); Option<String> none = Option.none();
// Map over Option Option<Integer> length = some.map(String::length); // Some(5)
// Provide default value String value = none.getOrElse("Default"); // "Default"
// Chain operations Option<String> result = some .filter(s -> s.length() > 3) .map(String::toUpperCase); // Some("HELLO") }}Try
The Try type represents a computation that may either result in an exception or return a value.
import codes.domix.fun.Try;
class TryExample { void main() { // Wrap risky operations Try<Integer> result = Try.of(() -> Integer.parseInt("123"));
// Handle success and failure String message = result .map(i -> "Success: " + i) .recover(throwable -> "Failure: " + throwable.getMessage()) .get();
// Chain operations Try<Integer> computed = Try.of(() -> "42") .map(Integer::parseInt) .map(i -> i * 2); // Success(84) }}Result
The Result type represents a value of one of two possible types (a disjoint union).
import codes.domix.fun.Result;
class ResultExample { void main() { Result<Integer, String> result = Result.ok(123); result.map(i -> i + 1); // Result.ok(124) result.isOk(); // true result.get(); // 124
Result<Integer, String> failure = Result.err("error"); failure.map(i -> i + 1); // Result.err("error") failure.isError(); // true failure.getError(); // "error" }}