GithubHelp home page GithubHelp logo

ashr123 / option Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 77 KB

DOP version of Optional

License: GNU General Public License v3.0

Java 100.00%
maybe nothing option some maybe-monad monad option-monad java java-21

option's Introduction

Maven Central

Option

Small package that brings DOP of Optional to Java.

Gives you the possibility to write code as

import io.github.ashr123.option.None;
import io.github.ashr123.option.Option;
import io.github.ashr123.option.Some;

public record Pair<L, R>(L left, R right) {
}

public static Integer intABSOption(Integer integer) {
	return switch (Option.of(integer)) {
		case Some(Integer i) when i < 0 -> -i;
		case Some(Integer i) -> i;
		case None<Integer> ignored -> null;
		case None() -> null; // effectively same as the case above
	};
}

public static Integer intMax(Pair<Integer, Integer> pair) {
	return switch (Option.of(pair)) {
		case Some(Pair(Integer l, Integer r)) when l != null && r == null -> l;
		case Some(Pair(Integer l, Integer r)) when l == null && r != null -> r;
		case Some(Pair(Integer l, Integer r)) when l != null && r != null && l > r -> l;
		case Some(Pair(Integer l, Integer r)) -> r;
		case None() -> null;
	};
}

Comparison against existing Optional<T>

Optional methods Option<T> opt equivalent
empty() new None<>()
of(T value) new Some<>(T value)
ofNullable(T value)
  • of(T value)
  • of(Optional<T> value)
get()
  • switch-case
  • if (opt instanceof Some(T value)) ...
isPresent() if (opt instanceof Some<?>) ...
isEmpty() if (opt instanceof None<?>) ...
ifPresent(Consumer<? super T> action)
  • switch-case
  • if (opt instanceof Some(T value)) value ...
ifPresentOrElse(Consumer<? super T> action, Runnable emptyAction) switch-case
filter(Predicate<? super T> predicate) if (opt instanceof Some(T value) && value ...)
map(Function<? super T, ? extends U> mapper) if (opt instanceof Some(T value)) value ...
flatMap(Function<? super T, ? extends Optional<? extends U>> mapper) flatMap(Function<? super T, ? extends Option<? extends U>> mapper)
or(Supplier<? extends Optional<? extends T>> supplier) switch-case
stream() stream()
orElse(T other) switch-case
orElseGet(Supplier<? extends T> supplier) switch-case
orElseThrow() switch-case
orElseThrow(Supplier<? extends X> exceptionSupplier) switch-case

Requires JRE 21 or above.

option's People

Contributors

ashr123 avatar renovate[bot] avatar

Watchers

 avatar

option's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

This repository currently has no open or pending branches.

Detected dependencies

maven
pom.xml
  • org.apache.maven.plugins:maven-compiler-plugin 3.13.0
  • org.apache.maven.plugins:maven-source-plugin 3.3.1
  • org.apache.maven.plugins:maven-javadoc-plugin 3.7.0
  • org.apache.maven.plugins:maven-gpg-plugin 3.2.4
  • org.sonatype.central:central-publishing-maven-plugin 0.4.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.