Last active
April 9, 2019 23:11
-
-
Save nicolopignatelli/ab6c9520f450190596d79f211116a64f to your computer and use it in GitHub Desktop.
Result micro-library. Emoji powered.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class Aborted extends Failure { | |
public Result<T> ✅(Consumer<T> f) { | |
return this; | |
} | |
public Result<T> ❌(Consumer<ExceptionStack> f) { | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class ExceptionStack { | |
private Exception[] exceptions; | |
public ExceptionStack(Exception... exceptions) { | |
this.exceptions = exceptions; | |
} | |
public merge(ExceptionStack exceptionStack) { | |
return Stream.of(this.exceptionStack, exceptionStack).flatMap(Stream::of).toArray(ExceptionStack[]::new); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Failure<T> implements Result<T> { | |
private ExceptionStack exceptionStack; | |
public Failure(ExceptionStack exceptionStack) { | |
this.exceptionStack = exceptionStack; | |
} | |
public Result<T> ✅(Consumer<T> f) { | |
return this; | |
} | |
public Result<T> ❌(Consumer<ExceptionStack> f) { | |
op = Result<ExceptionStack>::result(() -> { | |
f.accept(exceptionStack); | |
return exceptionStack; | |
}); | |
switch (true) { | |
case op instanceof Success<ExceptionStack>: | |
return new Aborted<T>(exStack); | |
case op instanceof Failure<ExceptionStack>: | |
return new Aborted<T>(exceptionStack.merge(deeperExStack)); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Result<T> { | |
default Result<T> get(Supplier<T> f) { | |
try { | |
value = f.get(); | |
return new Success(value); | |
} catch(Exception e) { | |
return new Failure(new ExceptionStack(e)); | |
} | |
} | |
Result<T> ✅(Consumer<T> consumer); | |
Result<T> ❌(Consumer<ExceptionStack> consumer); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class Success<T> implements Result<T> { | |
private T value; | |
public Success(T value) { | |
this.value = value; | |
} | |
public Result<T> ✅(Consumer<T> f) { | |
return Result<T>::execute(() -> { | |
f.accept(value); | |
return value; | |
); | |
} | |
public Result<T> ❌(Consumer<ExceptionStack> f) { | |
return this; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment