高级农民
- 积分
- 2721
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2017-6-18
- 最后登录
- 1970-1-1
|
本帖最后由 magicsets 于 2018-3-19 10:54 编辑
我写了一份简单代码供参考~
不过如果要优化性能的话,实现上会复杂很多:
(1) 需要特化(specialize)基础类型(例如int, double)的实现,参照Java的IntStream、DoubleStream
(2) 需要考虑lazy evaluation,以减少物化(materialize)数据造成的对内存带宽的冲击和对cache locality的破坏
(3) 如果要做lazy evaluation,实际上就是要生成中间的query plan,那么可以进行进一步的query optimization,比如filter pushdown
(4) 需要考虑对多核并行执行的支持
(5) 再加上对分布式的支持,你就得到了类似Spark的东西..- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.Iterator;
- public class Main {
- public static void main(String[] args) {
- int[] first = new int[] { 0, 1, 2 };
- int[] second = new int[] { 4, 5, 6 };
- MyStream.concat(MyStream.of(first).map(x -> (int)x + 1),
- MyStream.of(second)) // 1, 2, 3, 4, 5, 6
- .map(x -> (int)x * (int)x) // 1, 4, 9, 16, 25, 36
- .filter(x -> (int)x > 5 && (int)x < 30) // 9, 16, 25
- .reduce(0, (x, y) -> (int)x + (int)y) // 50
- .each(x -> System.out.println("sum = " + x)); // sum = 50
- }
- }
- // map()方法的参数类型:一元函数
- interface UnaryOperation {
- public Object apply(Object operand);
- }
- // reduce()方法的参数类型:二元函数
- interface BinaryOperation {
- public Object apply(Object lhs, Object rhs);
- }
- // filter()方法的参数类型:一元谓词
- interface UnaryPredicate {
- public boolean apply(Object operand);
- }
- // each()方法的参数类型
- interface Consumer {
- public void apply(Object operand);
- }
- // 不考虑性能的简单实现
- @SuppressWarnings("rawtypes")
- abstract class MyStream implements Iterable {
- public MyStream filter(UnaryPredicate predicate) {
- ArrayList<Object> results = new ArrayList<Object>();
- for (Object o : this) {
- if (predicate.apply(o)) {
- results.add(o);
- }
- }
- return of(results);
- }
- public MyStream map(UnaryOperation operation) {
- ArrayList<Object> results = new ArrayList<Object>();
- for (Object o : this) {
- results.add(operation.apply(o));
- }
- return of(results);
- }
- public MyStream reduce(Object init, BinaryOperation operation) {
- Object accumulated = init;
- for (Object o : this) {
- if (accumulated == null) {
- accumulated = o;
- } else {
- accumulated = operation.apply(accumulated, o);
- }
- }
- ArrayList<Object> results = new ArrayList<Object>();
- if (accumulated != null) {
- results.add(accumulated);
- }
- return of(results);
- }
- public MyStream reduce(BinaryOperation operation) {
- return reduce(null, operation);
- }
- public void each(Consumer consumer) {
- for (Object o : this) {
- consumer.apply(o);
- }
- }
- public static MyStream concat(MyStream a, MyStream b) {
- ArrayList<Object> results = new ArrayList<Object>();
- for (Object o : a) {
- results.add(o);
- }
- for (Object o : b) {
- results.add(o);
- }
- return of(results);
- }
- public static MyStream of(int[] data) {
- ArrayList<Integer> results = new ArrayList<Integer>();
- for (int i = 0; i < data.length; ++i) {
- results.add(data[i]);
- }
- return of(results);
- }
- public static MyStream of(Object[] data) {
- return new ArrayStream(data);
- }
- public static MyStream of(Collection data) {
- return new CollectionStream(data);
- }
- }
- class ArrayStream extends MyStream {
- private Object[] data;
- public ArrayStream(Object[] data) {
- this.data = data;
- }
- @Override
- public Iterator<Object> iterator() {
- return Arrays.asList(data).iterator();
- }
- }
- @SuppressWarnings("rawtypes")
- class CollectionStream extends MyStream {
- private Collection data;
- CollectionStream(Collection data) {
- this.data = data;
- }
- @Override
- public Iterator iterator() {
- return data.iterator();
- }
- }
复制代码 |
|