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 629 forks source link

Integration: Provide an interface to the Javaslang functionality #218

Closed danieldietrich closed 9 years ago

danieldietrich commented 9 years ago

like so:

package javaslang; 

import javaslang.collection.*; 
import javaslang.control.*; 

// This is the 'door' to Javaslang. 
// Because of name-clashes java.util.* should not be imported. 
// Instead directly convert method result using this methods or denote java.util types full qualified. 
public interface Javaslang { 

        // -- core 

        static <T> Function1<T, Boolean> toFunction1(Predicate<T> predicate) { 
                return t -> predicate.test(t); 
        } 

        static <T> CheckedFunction1<T, Boolean> toCheckedFunction1(Predicate<T> predicate) { 
                return t -> predicate.test(t); 
        } 

        // TODO: to(Checked)Function1(java.util.function.Function 
        // TODO: to(Checked)Function2(BiFunction) 
        // TODO: to(Checked)Function(Supplier) 
        // TODO: to(Checked)Function(Consumer) 
        // TODO: ... 

    // -- collection 

    static <T> List<T> toList(Iterable<T> iterable) { 
            return List.ofAll(iterable); 
    } 

    static <T> Stream<T> toStream(Iterable<T> iterable) { 
            return Stream.ofAll(iterable); 
    } 

    // ??? 
    static <T> BinaryTree<T> toBinaryTree(Iterable<? extends Iterable<T>> iterable) { 
            ... 
    } 

        // ??? 
    static <T> RoseTree<T> toRoseTree(Iterable<? extends Iterable<T> iterable) { 
            ... 
    } 

        // -- control 

    static <T> Option<T> toOption(java.util.Optional<T> optional) { 
            return Option.of(Objects.requireNonNull(optional, "optional is null").orElse(null)); 
    } 

    // ??? 
    static <T> Either<T> toEither(...) { 
            ... 
    } 

    // ??? 
    static <T> Try<T> toTry(...) { 
            ... 
    } 
} 
danieldietrich commented 9 years ago

Just name it To

danieldietrich commented 9 years ago

Traversable (implemented by many Javaslang classes, not only collections) has toJavaXxx() methods. These convert Javaslang objects to Java objects.

This does not scale well. Instead we could provide a to(Class) or to(Supplier<Collection>) function. S.th. similar was suggested in jOOQ/jOOL#95.

TraversableOnce has toXxx() methods to convert between Javaslang types.

Javaslang types have static of(...), ofAll(...) factory methods to convert from the Java std lib world to Javaslang.

There shouldn't be a separate To class. The toXxx, toJavaXxx methods should belong to a common interface. Currently TraversableOnce fits best.

danieldietrich commented 9 years ago

This is obsolete. We now have Value which is implemented by most of the Javaslang types and Value has many conversion methods.