Skip to content

Add Java implementations of Result, Maybe, Either #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions Java/Either/Either.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.util.function.Function;

public class Either<L, R> {
private final L left;
private final R right;

private Either(L left, R right) {
this.left = left;
this.right = right;
}

public static <L, R> Either<L, R> left(L value) {
return new Either<>(value, null);
}

public static <L, R> Either<L, R> right(R value) {
return new Either<>(null, value);
}

public L getLeft() {
return left;
}

public R getRight() {
return right;
}

public boolean isLeft() {
return left != null;
}

public boolean isRight() {
return right != null;
}

public <U> Either<L, U> map(Function<? super R, ? extends U> fn) {
if (right == null) return Either.left(left);
return Either.right(fn.apply(right));
}

public <T> T match(Function<? super L, ? extends T> leftFn, Function<? super R, ? extends T> rightFn) {
return isRight() ? rightFn.apply(right) : leftFn.apply(left);
}
}
15 changes: 15 additions & 0 deletions Java/Either/EitherUsage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public class EitherUsage {
public static void main(String[] args) {
Either<Integer, Integer> success = Either.right(42);
Either<Integer, Integer> failure = Either.left(500);

Either<Integer, Integer> doubled = success.map(x -> x * 2);
System.out.println("doubled: " + doubled.getRight());

String result = failure.match(
error -> "Failure: " + error,
value -> "Success: " + value
);
System.out.println("result: " + result);
}
}
26 changes: 26 additions & 0 deletions Java/Maybe/Maybe.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import java.util.function.Function;
import java.util.function.Supplier;

public class Maybe<T> {
private final T value;

public Maybe() {
this.value = null;
}

public Maybe(T value) {
this.value = value;
}

public T getValue() {
return value;
}

public boolean isEmpty() {
return value == null;
}

public <R> R match(Function<? super T, ? extends R> someFn, Supplier<? extends R> noneFn) {
return isEmpty() ? noneFn.get() : someFn.apply(value);
}
}
27 changes: 27 additions & 0 deletions Java/Maybe/MaybeUsage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class MaybeUsage {
public static void main(String[] args) {
Maybe<Integer> some = new Maybe<>(42);
Maybe<Integer> none = new Maybe<>();

System.out.println(some);
System.out.println(none);

System.out.println(some.isEmpty());
System.out.println(none.isEmpty());

System.out.println(some.getValue());
System.out.println(none.getValue());

String res1 = some.match(
v -> "Got value " + v,
() -> "No value"
);
System.out.println(res1);

String res2 = none.match(
v -> "Got value " + v,
() -> "No value"
);
System.out.println(res2);
}
}
39 changes: 39 additions & 0 deletions Java/Result/Result.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
public class Result<T, E extends Exception> {
private final T value;
private final E error;

public Result(T value, E error) {
this.value = value;
this.error = error;
}

public static <T, E extends Exception> Result<T, E> create(Object input) {
return (input instanceof Exception)
? new Result<>(null, (E) input)
: new Result<>((T) input, null);
}

public static <T> Result<T, Exception> fromValue(T value) {
return new Result<>(value, null);
}

public static <T, E extends Exception> Result<T, E> fromError(E error) {
return new Result<>(null, error);
}

public boolean isSuccess() {
return error == null;
}

public boolean isError() {
return error != null;
}

public T getValue() {
return value;
}

public E getError() {
return error;
}
}
41 changes: 41 additions & 0 deletions Java/Result/ResultUsage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
public class ResultUsage {
public static void main(String[] args) {
// Basic usage
Result<String, Exception> success = Result.create("Successfully received data");
Result<String, Exception> failure = Result.create(new Exception("Network error"));

if (success.isSuccess()) {
System.out.println("Success: " + success.getValue());
}

if (failure.isError()) {
System.out.println("Failure: " + failure.getError().getMessage());
}

// Additional usage example
// Handling a method that may throw and wrapping the result
Result<Integer, Exception> result1 = parseIntSafe("123");
Result<Integer, Exception> result2 = parseIntSafe("abc");

if (result1.isSuccess()) {
System.out.println("Parsed value: " + result1.getValue());
} else {
System.out.println("Parse error: " + result1.getError().getMessage());
}

if (result2.isSuccess()) {
System.out.println("Parsed value: " + result2.getValue());
} else {
System.out.println("Parse error: " + result2.getError().getMessage());
}
}

public static Result<Integer, Exception> parseIntSafe(String input) {
try {
int value = Integer.parseInt(input);
return Result.fromValue(value);
} catch (Exception e) {
return Result.fromError(e);
}
}
}