spencerwi / either.java Goto Github PK
View Code? Open in Web Editor NEWA right-biased implementation of "Either a b" for Java, using Java 8 for mapping/folding and type inference.
License: MIT License
A right-biased implementation of "Either a b" for Java, using Java 8 for mapping/folding and type inference.
License: MIT License
public abstract class Try<A> extends Either<Throwable, A>
but then I don't know how to implement Success
and Failure
.
In the changelog my username is given as hamishwilson instead of the correct hamishlawson
Hello,
I was very happy you made the effort to create this library.
Also I like how you added the Result-approach.
I am missing a run method for the Result class.
There is one in the Either so I will change my current usage of the library to the Either-Approach.
map and FlatMap have basically the same Type Signature apart from Exception Handling, which is wrong.
public abstract <T> Result<T> map(Function<R,T> transformValue);
public abstract <T> Result<T> flatMap(ExceptionThrowingFunction<R,T> transformValue);
Compared to flatMap on Scalas Try (or pretty much any other Class with flatMap in any functional Language):
flatMap[U](f: (T) โ Try[U]): Try[U]
I'd like to have a "real" flatMap Function. Catching Exceptions and translating to Result should be done by both, map and flatMap.
What's the reason for using 2 value pointers per element instead of one:
public abstract class Either<L, R> {
protected L leftValue;
protected R rightValue;
It seems like every indirection via Either will double the required space for the pointers. Shy of a union type, you could have used java.lang.Object and cast it dynamically to L or R.
Next scenario from the readme fails, however it's expected that it'll wrap exceptions from chained methods
@Test
public void map() {
Result<?> result = Result.attempt(() -> someOperationThatMightFailOrReturnA())
.map(a -> someOtherOperationThatMightFailOReturnB(a))
.map(b -> someThirdOperationThatMightFailOrReturnC(b));
Assert.assertTrue(result.isErr());
}
private Object someThirdOperationThatMightFailOrReturnC(Object b) {
return Objects.requireNonNull(b);
}
private Object someOtherOperationThatMightFailOReturnB(Object a) {
return Objects.requireNonNull(a);
}
private Object someOperationThatMightFailOrReturnA() {
return null;
}
java.lang.NullPointerException
at java.util.Objects.requireNonNull(Objects.java:203)
at com.spencerwi.either.ResultMapTest.someOtherOperationThatMightFailOReturnB(ResultMapTest.java:27)
at com.spencerwi.either.ResultMapTest.lambda$map$1(ResultMapTest.java:16)
at com.spencerwi.either.Result$Ok.map(Result.java:108)
Hi @spencerwi,
I was wondering if you can think about and then add a license for this library (which is very nice and simple that's why I like it!).
Thank you very much!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.