vavr-io / vavr

vʌvr (formerly called Javaslang) is a non-commercial, non-profit object-functional library that runs with Java 8+. It aims to reduce the lines of code and increase code quality.
https://vavr.io
Other
5.67k stars 628 forks source link

Simulate ADT #239

Closed sirinath closed 8 years ago

jbgi commented 9 years ago

You may be interested in https://github.com/sviperll/adt4j and a recent design discussion of the project: https://github.com/sviperll/adt4j/issues/20

danieldietrich commented 9 years ago

This is great!

We should take this into account when designing the collections. List and Stream are there, Map (#170) and Set (#214) are in the pipeline. I'm currently investigating which form of binary search tree to take in order to create efficient Map and Set implementations. Not only time complexity is important but also space complexity, particularly for HashMaps. There are important balanced tree impls, AVL and RedBlack. Scala uses binary encoded Tries in order to fail fast if a key/hash is not present. I'm into it - a very exiting topic!

danieldietrich commented 9 years ago

@sirinath Could you please provide some details, what to implement?

I think if it is a Java equivalent of Scala case classes, like adt4j project does, @jbgi mentioned above, it is out of scope for Javaslang. There already exists a great project that exactly does that!

talios commented 9 years ago

What would be nice is having some way that the two projects could intersect in user-land, which is in part the discussion @jbgi linked to - awhile ago I was thinking "it would be nice if these ADT4j classes were monads..."

danieldietrich commented 9 years ago

I'm against baking this annotation trickery into Javaslang. With JEP 169 there is already an official project to add Value Objects to Java. Beside that I don't like annotations very well.

danieldietrich commented 9 years ago

@talios I think the Monad operations on one ADT can be expressed in multiple ways. It is likewise with Monoids (String concatenation, String multiplication, etc). It is better to express this explicitly.

talios commented 9 years ago

@danieldietrich True, altho going FooMonad.map(foo, foo2 -> .... ); is kinda arse.

danieldietrich commented 9 years ago

@talios Yes, right...

sirinath commented 8 years ago

Also https://github.com/talios/javagadt/

jbgi commented 8 years ago

And now also https://github.com/derive4j/derive4j (no special support for javaslang... yet!)

danieldietrich commented 8 years ago

Nice!

jbgi commented 8 years ago

In order to add special support for javaslang into derive4J, what I would need is a none and a some static factory method in the Option interface. the methods should return an Option (not a Some or a None). Same for Either, with left and right constructors.

danieldietrich commented 8 years ago

Can't this be declared as some sort of mapping file within derive4J? From the API perspective this does not scale very well. I mean, why isn't it possible to declare within derive4J which class is Option and how to create a None and a Some?

jbgi commented 8 years ago

The main problem is type inference. Derive4J use invariant generics: using explicit subtyping (new Some() / None.instance()) will probably breaks generated code.

jbgi commented 8 years ago

I did some tests, it complicate things a little bit but should be doable. From a user point a view I still thinks that having some/none constructors in Option that return Option is a good thing. Sub-typing complicate usage of parametric polymorphism a lot and I find the later far more useful for FP than the former.

danieldietrich commented 8 years ago

Great, this sound like the right direction!

(If I understand it right, you provide views for ADTs like Option. Because you provide a general abstraction over a set of specific implementations, you cannot rely on a specific inheritance hierarchy, e.g. good old Java has just Optional, Javaslang has Option, Some and None. For FP features, like pattern matching, types are more useful compared to a flat view, but we can't achieve this when providing a general abstraction backed by various different impls, right?)

danieldietrich commented 8 years ago

Too many ADT projects in Java land. We will wait for official Java support by oracle...