Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toMap、partition #557

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<List<Object>> result = new ArrayList<>();
int index = 0;
for (Object element : RuntimeUtils.seq(coll, env)) {
if (index % pageSize == 0) {
List<Object> list = new ArrayList<>();
result.add(list);
}
result.get(index / pageSize).add(element);
index++;
}

return AviatorRuntimeJavaType.valueOf(result);
}

@Override
public String getName() {
return "seq.partition";
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Object> 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<Object, Object> 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<String, Object> 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<Object, Object> 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);
}
}
10 changes: 10 additions & 0 deletions src/test/java/com/googlecode/aviator/scripts/TestScripts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
24 changes: 24 additions & 0 deletions src/test/resources/scripts/partition.av
Original file line number Diff line number Diff line change
@@ -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));
6 changes: 6 additions & 0 deletions src/test/resources/scripts/toMap.av
Original file line number Diff line number Diff line change
@@ -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);