Last active
December 3, 2018 14:58
-
-
Save newmen/9cab0e82b23a3a13679babe9cef6a794 to your computer and use it in GitHub Desktop.
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
package ...; | |
import reactor.core.publisher.Flux; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.function.BiFunction; | |
import java.util.function.Function; | |
import java.util.function.Predicate; | |
import java.util.stream.Collectors; | |
import java.util.stream.IntStream; | |
import java.util.stream.Stream; | |
public class ListSeq { | |
public static <T> List<T> cons(T item, List<T> list) { | |
return concat(Stream.of(item), list.stream()); | |
} | |
public static <T> List<T> conj(List<T> list, T... items) { | |
return concat(list.stream(), Stream.of(items)); | |
} | |
public static <T> List<T> concat(List<T> list1, List<T> list2) { | |
return concat(list1.stream(), list2.stream()); | |
} | |
private static <T> List<T> concat(Stream<T> stream1, Stream<T> stream2) { | |
return Stream.concat(stream1, stream2).collect(Collectors.toList()); | |
} | |
public static <T> List<T> filter(List<T> xs, Predicate<T> predicate) { | |
return xs.stream() | |
.filter(predicate) | |
.collect(Collectors.toList()); | |
} | |
public static <T, R> List<R> map(List<T> xs, Function<T, R> function) { | |
return xs.stream() | |
.map(function) | |
.collect(Collectors.toList()); | |
} | |
public static <T, R> List<R> reduce(List<T> xs, BiFunction<List<R>, T, List<R>> function) { | |
return Flux.fromIterable(xs) | |
.reduce(new ArrayList<>(), function) | |
.block(); | |
} | |
public static <T> Stream<T> reversedStream(List<T> xs) { | |
if (xs.isEmpty()) { | |
return Stream.empty(); | |
} else { | |
int lastIndex = xs.size() - 1; | |
return IntStream.rangeClosed(0, lastIndex) | |
.mapToObj(i -> xs.get(lastIndex - i)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment