2017-06-05 67 views
5

我創建了一些tuple類,並將它們放入Java集合中。但是我不想直接在迭代collection時使用tuple作爲函數參數。所以我實現了tuple解包,如下面的代碼。如何在Java中實現函數參數的元組解包?

基本上它的工作原理,但問題是,類型轉換,需要:

map((Func2<Long, Long, Long>) (a, b) -> a + b)

是否有什麼辦法可以去除這裏的類型轉換?


編輯:

也許是我沒有說清楚,不僅tuple2,也tuple3tuple4 ...應予支持。 @飛揚的回答作品大爲Tuple2,但tuple2tuple3tuple4在此期間不工作

package test; 


import com.google.common.collect.Iterables; 

import java.util.Arrays; 
import java.util.function.Function; 

import static test.TupleIterable.Tuple.tuple; 

public interface TupleIterable<T> { 

    Iterable<T> apply(); 

    static <E> TupleIterable<E> from(Iterable<E> iterable) { 
     return() -> iterable; 
    } 

    default <E> TupleIterable<E> map(Function<? super T, ? extends E> op) { 
     return() -> Iterables.transform(TupleIterable.this.apply(), op::apply); 
    } 

    interface Func2<T1, T2, R> extends Function<Tuple.Tuple2<T1, T2>, R> { 

     R apply(T1 t1, T2 t2); 

     @Override 
     default R apply(Tuple.Tuple2<T1, T2> t) { 
      return apply(t.t1, t.t2); 
     } 
    } 

    interface Func3<T1, T2, T3, R> extends Function<Tuple.Tuple3<T1, T2, T3>, R> { 

     R apply(T1 t1, T2 t2, T3 t3); 

     @Override 
     default R apply(Tuple.Tuple3<T1, T2, T3> t) { 
      return apply(t.t1, t.t2, t.t3); 
     } 
    } 

    interface Tuple { 

     static <T1, T2> Tuple2<T1, T2> tuple(T1 t1, T2 t2) { 
      return new Tuple2<>(t1, t2); 
     } 

     static <T1, T2, T3> Tuple3<T1, T2, T3> tuple(T1 t1, T2 t2, T3 t3) { 
      return new Tuple3<>(t1, t2, t3); 
     } 

     class Tuple2<T1, T2> implements Tuple { 

      public T1 t1; 

      public T2 t2; 

      public Tuple2(T1 t1, T2 t2) { 
       this.t1 = t1; 
       this.t2 = t2; 
      } 
     } 

     class Tuple3<T1, T2, T3> implements Tuple { 

      public T1 t1; 

      public T2 t2; 

      public T3 t3; 

      public Tuple3(T1 t1, T2 t2, T3 t3) { 
       this.t1 = t1; 
       this.t2 = t2; 
       this.t3 = t3; 
      } 
     } 
    } 

    public static void main(String[] args) { 
     TupleIterable.from(Arrays.asList(1L, 2L)) 
       .map(x -> tuple(x, x)) // map long to tuple2 
       .map((Func2<Long, Long, Tuple.Tuple3<Long, Long, Long>>) (a, b) -> tuple(a, b, a + b)) // map tuple2 to tuple3 
       .map((Func3<Long, Long, Long, Long>) (a, b, c) -> a + b + c) // map tuple3 to Long 
       .apply() 
       .forEach(System.out::println); 
    } 
} 

回答

3

你可以做同樣的伎倆爲JDK開發者沒有爲基本類型的Stream專業。在不同的界面中引入mapToTupleunwrap

我重寫了一些您的代碼並使用了一些現有的FunctionalInterfaces

import java.util.Arrays; 
import java.util.Iterator; 
import java.util.function.BiFunction; 
import java.util.function.Function; 
import java.util.function.Supplier; 

public class Test { 

    public static void main(String... args) { 
    MyIterable.from(Arrays.asList(1L, 2L)).mapToTuple(l -> Tuple2.tuple(l, l + 1L)).unwrap((a, b) -> a + b).get() 
     .forEach(System.out::println); 
    } 
} 

final class Tuple2<T1, T2> { 

    public static <T1, T2> Tuple2<T1, T2> tuple(T1 t1, T2 t2) { 
    return new Tuple2<>(t1, t2); 
    } 

    public final T1 t1; 
    public final T2 t2; 

    private Tuple2(T1 t1, T2 t2) { 
    this.t1 = t1; 
    this.t2 = t2; 
    } 
} 

@FunctionalInterface 
interface TupleIterable<T1, T2> extends Supplier<Iterable<Tuple2<T1, T2>>> { 

    default <E> MyIterable<E> unwrap(BiFunction<T1, T2, E> func) { 
    return() -> Iterables.transform(get(), t -> func.apply(t.t1, t.t2)); 
    } 
} 

@FunctionalInterface 
interface MyIterable<T> extends Supplier<Iterable<T>> { 

    static <E> MyIterable<E> from(Iterable<E> iterable) { 
    return() -> iterable; 
    } 

    default <E> MyIterable<E> map(Function<? super T, ? extends E> mapper) { 
    return() -> Iterables.transform(get(), mapper::apply); 
    } 

    default <T1, T2> TupleIterable<T1, T2> mapToTuple(Function<? super T, ? extends Tuple2<T1, T2>> tupleMapper) { 
    return() -> Iterables.transform(get(), tupleMapper::apply); 
    } 
} 

final class Iterables { 
    public static <T, E> Iterable<E> transform(Iterable<T> iterable, Function<? super T, ? extends E> mapper) { 
    return() -> new Iterator<E>() { 

     private final Iterator<T> iter = iterable.iterator(); 

     @Override 
     public boolean hasNext() { 
     return iter.hasNext(); 
     } 

     @Override 
     public E next() { 
     return mapper.apply(iter.next()); 
     } 
    }; 
    } 
} 
+0

感謝您的好評!我還有一個問題,這是否支持其他元組,例如'Tuple3','Tuple4'等等......看來我需要創建很多方法,比如'mapToTuple3','mapToTuple4' ... – moshangcheng

+0

'Tuple3Iterable ','Tuple4Iterable'不兼容。也就是說,參數不能在'Tuple3Iterable :: mapToTuple4'中解壓縮。 – moshangcheng

+2

@moshangcheng如果你想成爲通用的,那麼你無法避免施放。 – Flown

0

我真的不能告訴,如果這是過於「骯髒」或沒有,但爲什麼不爲T1T2提供內部Tuple2 getter和做:

.map(x -> tuple(x, x + 1)) // map long to tuple2 
.map(a -> a.getT1() + a.getT2()) // map tuple2 to long 
1

而不是做類型轉換,我創建了一個unpack函數來解開元組。它的工作原理與類似,但仍不夠直觀。

package test; 


import com.google.common.collect.Iterables; 

import java.util.Arrays; 
import java.util.function.Function; 

import static test.TupleIterable.Func.unpack; 
import static test.TupleIterable.Tuple.tuple; 

public interface TupleIterable<T> { 

    Iterable<T> apply(); 

    static <E> TupleIterable<E> from(Iterable<E> iterable) { 
     return() -> iterable; 
    } 

    default <E> TupleIterable<E> map(Function<? super T, E> op) { 
     return() -> Iterables.transform(TupleIterable.this.apply(), op::apply); 
    } 

    interface Func { 

     static <T1, T2, R> Function<Tuple.Tuple2<T1, T2>, R> unpack(Func2<T1, T2, R> f) { 
      return t -> f.apply(t.t1, t.t2); 
     } 

     static <T1, T2, T3, R> Function<Tuple.Tuple3<T1, T2, T3>, R> unpack(Func3<T1, T2, T3, R> f) { 
      return t -> f.apply(t.t1, t.t2, t.t3); 
     } 

     @FunctionalInterface 
     interface Func2<T1, T2, R> { 

      R apply(T1 t1, T2 t2); 
     } 

     @FunctionalInterface 
     interface Func3<T1, T2, T3, R> { 

      R apply(T1 t1, T2 t2, T3 t3); 
     } 
    } 

    interface Tuple { 

     static <T1, T2> Tuple2<T1, T2> tuple(T1 t1, T2 t2) { 
      return new Tuple2<>(t1, t2); 
     } 

     static <T1, T2, T3> Tuple3<T1, T2, T3> tuple(T1 t1, T2 t2, T3 t3) { 
      return new Tuple3<>(t1, t2, t3); 
     } 

     class Tuple2<T1, T2> implements Tuple { 

      public T1 t1; 

      public T2 t2; 

      public Tuple2(T1 t1, T2 t2) { 
       this.t1 = t1; 
       this.t2 = t2; 
      } 
     } 

     class Tuple3<T1, T2, T3> implements Tuple { 

      public T1 t1; 

      public T2 t2; 

      public T3 t3; 

      public Tuple3(T1 t1, T2 t2, T3 t3) { 
       this.t1 = t1; 
       this.t2 = t2; 
       this.t3 = t3; 
      } 
     } 
    } 

    public static void main(String[] args) { 
     TupleIterable.from(Arrays.asList(1L, 2L)) 
       .map(x -> tuple(x, x)) // map long to tuple2 
       .map(unpack((a, b) -> tuple(a, b, a + b))) // map tuple2 to tuple3 
       .map(unpack((a, b, c) -> a + b + c)) // map tuple3 to Long 
       .apply() 
       .forEach(System.out::println); 
    } 
}