当我在Clojure中学习传感器时,它突然让我想起了他们提醒我的:Java 8流!
Clojure的:
(def xf
(comp
(filter odd?)
(map inc)
(take 5)))
(println
(transduce xf + (range 100))) ; => 30
(println
(into [] xf (range 100))) ; => [2 4 6 8 10]
爪哇:
// Purposely using Function and boxed primitive streams (instead of
// UnaryOperator<LongStream>) in order to keep it general.
Function<Stream<Long>, Stream<Long>> xf =
s -> s.filter(n -> n % 2L == 1L)
.map(n -> n + 1L)
.limit(5L);
System.out.println(
xf.apply(LongStream.range(0L, 100L).boxed())
.reduce(0L, Math::addExact)); // => 30
System.out.println(
xf.apply(LongStream.range(0L, 100L).boxed())
.collect(Collectors.toList())); // => [2, 4, 6, 8, 10]
除了静态/动态类型的不同之外,这些在目的和使用方面看起来与我非常相似。
Java流转换的类比是否是考虑传感器的合理方式?如果没有,它是如何有缺陷的,或者两者在概念上有何不同(不是说实施)?
答案 0 :(得分:10)
主要区别在于动词(操作)的集合在某种程度上对于流而言是关闭的,而它们对于传感器是开放的:例如尝试在流上实现partition
,感觉有点第二类:
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.stream.Stream.Builder;
public class StreamUtils {
static <T> Stream<T> delay(final Supplier<Stream<T>> thunk) {
return Stream.of((Object) null).flatMap(x -> thunk.get());
}
static class Partitioner<T> implements Function<T, Stream<Stream<T>>> {
final Function<T, ?> f;
Object prev;
Builder<T> sb;
public Partitioner(Function<T, ?> f) {
this.f = f;
}
public Stream<Stream<T>> apply(T t) {
Object tag = f.apply(t);
if (sb != null && prev.equals(tag)) {
sb.accept(t);
return Stream.empty();
}
Stream<Stream<T>> partition = sb == null ? Stream.empty() : Stream.of(sb.build());
sb = Stream.builder();
sb.accept(t);
prev = tag;
return partition;
}
Stream<Stream<T>> flush() {
return sb == null ? Stream.empty() : Stream.of(sb.build());
}
}
static <T> Stream<Stream<T>> partitionBy(Stream<T> in, Function<T, ?> f) {
Partitioner<T> partitioner = new Partitioner<>(f);
return Stream.concat(in.flatMap(partitioner), delay(() -> partitioner.flush()));
}
}
也像序列和缩减者一样,当你改变时,你不会创造一个更大的&#34;计算,你创造一个更大的&#34;源。
为了能够通过计算,您已经从流到流引入了xf
函数来将操作从方法提升到第一类实体(以便从源中解开它们)。通过这样做,您已经创建了一个换能器,尽管接口太大了。
以下代码的更通用版本将任何(clojure)传感器应用于Stream:
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;
import java.util.stream.Stream.Builder;
import clojure.lang.AFn;
import clojure.lang.IFn;
import clojure.lang.Reduced;
public class StreamUtils {
static <T> Stream<T> delay(final Supplier<Stream<T>> thunk) {
return Stream.of((Object) null).flatMap(x -> thunk.get());
}
static class Transducer implements Function {
IFn rf;
public Transducer(IFn xf) {
rf = (IFn) xf.invoke(new AFn() {
public Object invoke(Object acc) {
return acc;
}
public Object invoke(Object acc, Object item) {
((Builder<Object>) acc).accept(item);
return acc;
}
});
}
public Stream<?> apply(Object t) {
if (rf == null) return Stream.empty();
Object ret = rf.invoke(Stream.builder(), t);
if (ret instanceof Reduced) {
Reduced red = (Reduced) ret;
Builder<?> sb = (Builder<?>) red.deref();
return Stream.concat(sb.build(), flush());
}
return ((Builder<?>) ret).build();
}
Stream<?> flush() {
if (rf == null) return Stream.empty();
Builder<?> sb = (Builder<?>) rf.invoke(Stream.builder());
rf = null;
return sb.build();
}
}
static <T> Stream<?> withTransducer(Stream<T> in, IFn xf) {
Transducer transducer = new Transducer(xf);
return Stream.concat(in.flatMap(transducer), delay(() -> transducer.flush()));
}
}
答案 1 :(得分:1)
我看到的另一个重要区别是Clojure换能器是可组合的。我经常遇到这样的情况,我的流管道比您的示例更长,在该示例中,我可以在其他地方重复使用一些中间步骤,例如:
someStream
.map(...)
.filter(...)
.map(...) // <- gee, there are at least two other
.filter(...) // <- pipelines where I could use the functionality
.map(...) // <- of just these three steps!
.filter(...)
.collect(...)
我还没有找到实现该目标的理智方法。我希望我拥有的是这样的东西:
Transducer<Integer,String> smallTransducer = s -> s.map(...); // usable in a stream Integer -> String
Transducer<String,MyClass> otherTransducer = s -> s.filter(...).map(...); // stream String -> MyClass
Transducer<Integer,MyClass> combinedTransducer = smallTransducer.then(otherTransducer); // compose transducers, to get an Integer -> MyClass transducer
然后像这样使用它:
someStream
.map(...)
.filter(...)
.transduce(smallTransducer)
.transduce(otherTransducer)
.filter(...)
.collect(...)
// or
someStream
.map(...)
.filter(...)
.transduce(combinedTransducer)
.filter(...)
.collect(...)