|
| 1 | +package com.annimon.ownlang.lib.modules.functions; |
| 2 | + |
| 3 | +import com.annimon.ownlang.exceptions.TypeException; |
| 4 | +import com.annimon.ownlang.lib.*; |
| 5 | + |
| 6 | +public final class functional_stream implements Function { |
| 7 | + |
| 8 | + @Override |
| 9 | + public Value execute(Value... args) { |
| 10 | + Arguments.checkAtLeast(1, args.length); |
| 11 | + |
| 12 | + if (args.length > 1) { |
| 13 | + return new StreamValue(new ArrayValue(args)); |
| 14 | + } |
| 15 | + |
| 16 | + final Value value = args[0]; |
| 17 | + switch (value.type()) { |
| 18 | + case Types.MAP: |
| 19 | + return new StreamValue(((MapValue) value).toPairs()); |
| 20 | + case Types.ARRAY: |
| 21 | + return new StreamValue((ArrayValue) value); |
| 22 | + default: |
| 23 | + throw new TypeException("Invalid argument. Array or map expected"); |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + private static class StreamValue extends MapValue { |
| 28 | + |
| 29 | + private final ArrayValue container; |
| 30 | + |
| 31 | + public StreamValue(ArrayValue container) { |
| 32 | + super(12); |
| 33 | + this.container = container; |
| 34 | + init(); |
| 35 | + } |
| 36 | + |
| 37 | + private void init() { |
| 38 | + set("filter", wrapIntermediate(new functional_filter(false))); |
| 39 | + set("map", wrapIntermediate(new functional_map())); |
| 40 | + set("flatMap", wrapIntermediate(new functional_flatmap())); |
| 41 | + set("sortBy", wrapIntermediate(new functional_sortby())); |
| 42 | + set("takeWhile", wrapIntermediate(new functional_filter(true))); |
| 43 | + set("dropWhile", wrapIntermediate(new functional_dropwhile())); |
| 44 | + set("skip", this::skip); |
| 45 | + set("limit", this::limit); |
| 46 | + |
| 47 | + set("reduce", wrapTerminal(new functional_reduce())); |
| 48 | + set("forEach", wrapTerminal(new functional_foreach())); |
| 49 | + set("toArray", args -> container); |
| 50 | + set("count", args -> NumberValue.of(container.size())); |
| 51 | + } |
| 52 | + |
| 53 | + private Value skip(Value... args) { |
| 54 | + Arguments.check(1, args.length); |
| 55 | + |
| 56 | + final int skipCount = args[0].asInt(); |
| 57 | + final int size = container.size(); |
| 58 | + |
| 59 | + if (skipCount <= 0) return this; |
| 60 | + if (skipCount >= size) { |
| 61 | + return new StreamValue(new ArrayValue(0)); |
| 62 | + } |
| 63 | + |
| 64 | + final Value[] result = new Value[size - skipCount]; |
| 65 | + System.arraycopy(container.getCopyElements(), skipCount, result, 0, result.length); |
| 66 | + return new StreamValue(new ArrayValue(result)); |
| 67 | + } |
| 68 | + |
| 69 | + private Value limit(Value... args) { |
| 70 | + Arguments.check(1, args.length); |
| 71 | + |
| 72 | + final int limitCount = args[0].asInt(); |
| 73 | + final int size = container.size(); |
| 74 | + |
| 75 | + if (limitCount >= size) return this; |
| 76 | + if (limitCount <= 0) { |
| 77 | + return new StreamValue(new ArrayValue(0)); |
| 78 | + } |
| 79 | + |
| 80 | + final Value[] result = new Value[limitCount]; |
| 81 | + System.arraycopy(container.getCopyElements(), 0, result, 0, limitCount); |
| 82 | + return new StreamValue(new ArrayValue(result)); |
| 83 | + } |
| 84 | + |
| 85 | + private FunctionValue wrapIntermediate(Function f) { |
| 86 | + return wrap(f, true); |
| 87 | + } |
| 88 | + |
| 89 | + private FunctionValue wrapTerminal(Function f) { |
| 90 | + return wrap(f, false); |
| 91 | + } |
| 92 | + |
| 93 | + private FunctionValue wrap(Function f, boolean intermediate) { |
| 94 | + return new FunctionValue(args -> { |
| 95 | + final Value[] newArgs = new Value[args.length + 1]; |
| 96 | + System.arraycopy(args, 0, newArgs, 1, args.length); |
| 97 | + newArgs[0] = container; |
| 98 | + final Value result = f.execute(newArgs); |
| 99 | + if (intermediate && result.type() == Types.ARRAY) { |
| 100 | + return new StreamValue((ArrayValue) result); |
| 101 | + } |
| 102 | + return result; |
| 103 | + }); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments