public void testListArithmetic() { List<List> numLists = new ArrayList<List>(); numLists.add(Arrays.asList(1, 2, 3)); numLists.add(Arrays.asList(10, 20, 30)); assertEquals( Arrays.asList(6, 60), collect( numLists, new Closure<Integer>(null) { public Integer doCall(Integer a, Integer b, Integer c) { return a + b + c; } })); Closure<Integer> arithmeticClosure = new Closure<Integer>(null) { public Integer doCall(Integer a, Integer b, Integer c) { return a * b + c; } }; Closure<Integer> tensAndUnits = arithmeticClosure.curry(10); assertEquals(35, (int) tensAndUnits.call(3, 5)); tensAndUnits = arithmeticClosure.ncurry(0, 10); assertEquals(35, (int) tensAndUnits.call(3, 5)); tensAndUnits = arithmeticClosure.ncurry(1, 10); assertEquals(35, (int) tensAndUnits.call(3, 5)); Closure<Integer> timesPlus5 = arithmeticClosure.rcurry(5); assertEquals(35, (int) timesPlus5.call(15, 2)); timesPlus5 = arithmeticClosure.ncurry(2, 5); assertEquals(35, (int) timesPlus5.call(15, 2)); }
public void testFindAllAndCurry() { Map<String, Integer> expected = new HashMap<String, Integer>(zoo); expected.remove("Lions"); Closure<Boolean> keyBiggerThan = new Closure<Boolean>(null) { public Boolean doCall(Map.Entry<String, Integer> e, Integer size) { return e.getKey().length() > size; } }; Closure<Boolean> keyBiggerThan6 = keyBiggerThan.rcurry(6); assertEquals(expected, findAll(zoo, keyBiggerThan6)); }