From c8b1be3b172f6d1d7a98d8abbbb4619bc631de9e Mon Sep 17 00:00:00 2001 From: zengnianmei027148 Date: Fri, 7 Jul 2023 10:45:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?toMap=E3=80=81partition?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../aviator/AviatorEvaluatorInstance.java | 4 ++ .../function/seq/SeqPartitionFunction.java | 45 ++++++++++++ .../function/seq/SeqToMapFunction.java | 68 +++++++++++++++++++ .../aviator/scripts/TestScripts.java | 10 +++ src/test/resources/scripts/partition.av | 24 +++++++ src/test/resources/scripts/toMap.av | 6 ++ 6 files changed, 157 insertions(+) create mode 100644 src/main/java/com/googlecode/aviator/runtime/function/seq/SeqPartitionFunction.java create mode 100644 src/main/java/com/googlecode/aviator/runtime/function/seq/SeqToMapFunction.java create mode 100644 src/test/resources/scripts/partition.av create mode 100644 src/test/resources/scripts/toMap.av diff --git a/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java b/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java index 1e0d04de..c7780021 100644 --- a/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java +++ b/src/main/java/com/googlecode/aviator/AviatorEvaluatorInstance.java @@ -110,12 +110,14 @@ import com.googlecode.aviator.runtime.function.seq.SeqNewMapFunction; import com.googlecode.aviator.runtime.function.seq.SeqNewSetFunction; import com.googlecode.aviator.runtime.function.seq.SeqNotAnyFunction; +import com.googlecode.aviator.runtime.function.seq.SeqPartitionFunction; import com.googlecode.aviator.runtime.function.seq.SeqPutFunction; import com.googlecode.aviator.runtime.function.seq.SeqReduceFunction; import com.googlecode.aviator.runtime.function.seq.SeqRemoveFunction; import com.googlecode.aviator.runtime.function.seq.SeqReverseFunction; import com.googlecode.aviator.runtime.function.seq.SeqSomeFunction; import com.googlecode.aviator.runtime.function.seq.SeqSortFunction; +import com.googlecode.aviator.runtime.function.seq.SeqToMapFunction; import com.googlecode.aviator.runtime.function.seq.SeqValsFunction; import com.googlecode.aviator.runtime.function.seq.SeqZipmapFunction; import com.googlecode.aviator.runtime.function.string.StringContainsFunction; @@ -975,6 +977,8 @@ private void loadSeqFunctions() { new SeqMakePredicateFunFunction("seq.false", OperatorType.EQ, AviatorBoolean.FALSE)); addFunction(new SeqMakePredicateFunFunction("seq.nil", OperatorType.EQ, AviatorNil.NIL)); addFunction(new SeqMakePredicateFunFunction("seq.exists", OperatorType.NEQ, AviatorNil.NIL)); + addFunction(new SeqToMapFunction()); + addFunction(new SeqPartitionFunction()); } private void loadMathFunctions() { diff --git a/src/main/java/com/googlecode/aviator/runtime/function/seq/SeqPartitionFunction.java b/src/main/java/com/googlecode/aviator/runtime/function/seq/SeqPartitionFunction.java new file mode 100644 index 00000000..4672600c --- /dev/null +++ b/src/main/java/com/googlecode/aviator/runtime/function/seq/SeqPartitionFunction.java @@ -0,0 +1,45 @@ +package com.googlecode.aviator.runtime.function.seq; + +import com.googlecode.aviator.runtime.RuntimeUtils; +import com.googlecode.aviator.runtime.function.AbstractFunction; +import com.googlecode.aviator.runtime.type.AviatorNil; +import com.googlecode.aviator.runtime.type.AviatorObject; +import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** + * seq.partition(from_seq, page_size) 将seq分页,保持原有顺序 + * + * @author zengnianmei + */ +public class SeqPartitionFunction extends AbstractFunction { + @Override + public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2) { + Object coll = arg1.getValue(env); + if (coll == null) { + return AviatorNil.NIL; + } + Number arg2Value = (Number) arg2.getValue(env); + int pageSize = arg2Value.intValue(); + List> result = new ArrayList<>(); + int index = 0; + for (Object element : RuntimeUtils.seq(coll, env)) { + if (index % pageSize == 0) { + List list = new ArrayList<>(); + result.add(list); + } + result.get(index / pageSize).add(element); + index++; + } + + return AviatorRuntimeJavaType.valueOf(result); + } + + @Override + public String getName() { + return "seq.partition"; + } +} diff --git a/src/main/java/com/googlecode/aviator/runtime/function/seq/SeqToMapFunction.java b/src/main/java/com/googlecode/aviator/runtime/function/seq/SeqToMapFunction.java new file mode 100644 index 00000000..61ccf16f --- /dev/null +++ b/src/main/java/com/googlecode/aviator/runtime/function/seq/SeqToMapFunction.java @@ -0,0 +1,68 @@ +package com.googlecode.aviator.runtime.function.seq; + +import com.googlecode.aviator.runtime.RuntimeUtils; +import com.googlecode.aviator.runtime.function.AbstractFunction; +import com.googlecode.aviator.runtime.function.LambdaFunction; +import com.googlecode.aviator.runtime.type.AviatorNil; +import com.googlecode.aviator.runtime.type.AviatorObject; +import com.googlecode.aviator.runtime.type.AviatorRuntimeJavaType; + +import java.util.HashMap; +import java.util.Map; + +/** + * seq.toMap(from_seq, key_mapper_lambda) Or seq.toMap(from_seq, key_mapper_lambda, value_mapper_lambda) + * + * @author zengnianmei + */ +public class SeqToMapFunction extends AbstractFunction { + + @Override + public String getName() { + return "seq.toMap"; + } + + @Override + public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2) { + Object fromSeq = arg1.getValue(env); + if (fromSeq == null) { + return AviatorNil.NIL; + } + LambdaFunction keyMapper = (LambdaFunction) arg2.getValue(env); + if (keyMapper == null) { + throw new IllegalArgumentException("null keyMapper"); + } + Map result = new HashMap<>(); + for (Object element : RuntimeUtils.seq(fromSeq, env)) { + AviatorObject elementObject = AviatorRuntimeJavaType.valueOf(element); + AviatorObject key = keyMapper.call(env, elementObject); + result.put(key.getValue(env), element); + } + return AviatorRuntimeJavaType.valueOf(result); + } + + @Override + public AviatorObject call(Map env, AviatorObject arg1, AviatorObject arg2, + AviatorObject arg3) { + Object fromSeq = arg1.getValue(env); + if (fromSeq == null) { + return AviatorNil.NIL; + } + LambdaFunction keyMapper = (LambdaFunction) arg2.getValue(env); + if (keyMapper == null) { + throw new IllegalArgumentException("null keyMapper"); + } + LambdaFunction valueMapper = (LambdaFunction) arg3.getValue(env); + if (valueMapper == null) { + throw new IllegalArgumentException("null valueMapper"); + } + Map result = new HashMap<>(); + for (Object element : RuntimeUtils.seq(fromSeq, env)) { + AviatorObject elementObject = AviatorRuntimeJavaType.valueOf(element); + AviatorObject key = keyMapper.call(env, elementObject); + AviatorObject value = valueMapper.call(env, elementObject); + result.put(key.getValue(env), value.getValue(env)); + } + return AviatorRuntimeJavaType.valueOf(result); + } +} diff --git a/src/test/java/com/googlecode/aviator/scripts/TestScripts.java b/src/test/java/com/googlecode/aviator/scripts/TestScripts.java index 69823216..702cf309 100644 --- a/src/test/java/com/googlecode/aviator/scripts/TestScripts.java +++ b/src/test/java/com/googlecode/aviator/scripts/TestScripts.java @@ -382,4 +382,14 @@ public void testTryCatch() { assertEquals(1, testScript("try_catch6.av")); assertEquals(2, testScript("try_catch7.av")); } + + @Test + public void testToMap() { + testScript("toMap.av"); + } + + @Test + public void testPartition() { + testScript("partition.av"); + } } diff --git a/src/test/resources/scripts/partition.av b/src/test/resources/scripts/partition.av new file mode 100644 index 00000000..e0647077 --- /dev/null +++ b/src/test/resources/scripts/partition.av @@ -0,0 +1,24 @@ +# 简单列表 +let r1 = seq.partition(range(1,10), 5); +println(r1); + +# 对象列表 +let list = seq.list(seq.map('id', 1, 'name', '1'), seq.map('id', 2, 'name', '22'), seq.map('id', 3, 'name', '333'), + seq.map('id', 4, 'name', '4'), seq.map('id', 5, 'name', '5')); +let r2 = seq.partition(list, 2); +println(r2); +println(seq.get(seq.get(seq.get(r2, 1), 1), 'id')); + +for i in r2 { + println(i); +} + +# 针对map +let map = seq.map(); +for i in range(1,10) { + seq.add(map, i, i * 2); +} +let r3 = seq.partition(map, 5); +println(r3); +let node = seq.get(seq.get(r3, 1), 1); +println("key=" + getKey(node) + ", value=" + getValue(node)); \ No newline at end of file diff --git a/src/test/resources/scripts/toMap.av b/src/test/resources/scripts/toMap.av new file mode 100644 index 00000000..cec41785 --- /dev/null +++ b/src/test/resources/scripts/toMap.av @@ -0,0 +1,6 @@ +let list = seq.list(seq.map('id', 1, 'name', '1'), seq.map('id', 2, 'name', '22'), seq.map('id', 3, 'name', '333'), + seq.map('id', 1, 'name', '1111')); +let map1 = seq.toMap(list, lambda(x) -> x.id end); +println(map1); +let map2 = seq.toMap(seq.list(1,2,3), lambda(x) -> x end, lambda(x) -> x * 2 end); +println(map2); \ No newline at end of file From bca21aa321c4394868317dd4e30f26cc39d3364b Mon Sep 17 00:00:00 2001 From: zengnianmei027148 Date: Fri, 14 Jul 2023 13:03:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?toMap=E3=80=81partition?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/test/resources/scripts/partition.av | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/resources/scripts/partition.av b/src/test/resources/scripts/partition.av index e0647077..bfd45448 100644 --- a/src/test/resources/scripts/partition.av +++ b/src/test/resources/scripts/partition.av @@ -1,8 +1,8 @@ -# 简单列表 +## 简单列表 let r1 = seq.partition(range(1,10), 5); println(r1); -# 对象列表 +## 对象列表 let list = seq.list(seq.map('id', 1, 'name', '1'), seq.map('id', 2, 'name', '22'), seq.map('id', 3, 'name', '333'), seq.map('id', 4, 'name', '4'), seq.map('id', 5, 'name', '5')); let r2 = seq.partition(list, 2); @@ -13,7 +13,7 @@ for i in r2 { println(i); } -# 针对map +## 针对map let map = seq.map(); for i in range(1,10) { seq.add(map, i, i * 2);