瀏覽代碼

Merge branch 'accumulate'

Piet van Dongen 11 年之前
父節點
當前提交
25b87927af

+ 11
- 0
accumulate/build.gradle 查看文件

@@ -0,0 +1,11 @@
1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+  mavenCentral()
7
+}
8
+
9
+dependencies {
10
+  testCompile "junit:junit:4.10"
11
+}

+ 17
- 0
accumulate/example.java 查看文件

@@ -0,0 +1,17 @@
1
+import java.util.ArrayList;
2
+import java.util.Collection;
3
+import java.util.List;
4
+import java.util.function.Function;
5
+
6
+public class Accumulate {
7
+
8
+	public static <T> Collection<T> accumulate(Collection<T> collection, Function<T, T> function) {
9
+		List<T> newCollection = new ArrayList<>();
10
+
11
+		for (T item : collection) {
12
+			newCollection.add(function.apply(item));
13
+		}
14
+
15
+		return newCollection;
16
+	}
17
+}

+ 0
- 0
accumulate/src/main/java/.keep 查看文件


+ 2
- 0
accumulate/src/main/java/Accumulate.java 查看文件

@@ -0,0 +1,2 @@
1
+public class Accumulate {
2
+}

+ 53
- 0
accumulate/src/test/java/AccumulateTest.java 查看文件

@@ -0,0 +1,53 @@
1
+import org.junit.Test;
2
+
3
+import java.util.Arrays;
4
+import java.util.LinkedList;
5
+import java.util.List;
6
+
7
+import static org.junit.Assert.assertEquals;
8
+
9
+public class AccumulateTest {
10
+
11
+    @Test
12
+    public void emptyAccumulateProducesEmptyAccumulation() {
13
+        List<Integer> input = new LinkedList<>();
14
+        List<Integer> expectedOutput = new LinkedList<>();
15
+        assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x * x));
16
+    }
17
+
18
+    @Test
19
+    public void accumulateSquares() {
20
+        List<Integer> input = Arrays.asList(1, 2, 3);
21
+        List<Integer> expectedOutput = Arrays.asList(1, 4, 9);
22
+        assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x * x));
23
+    }
24
+
25
+    @Test
26
+    public void accumulateUpperCases() {
27
+        List<String> input = Arrays.asList("hello", "world");
28
+        List<String> expectedOutput = Arrays.asList("HELLO", "WORLD");
29
+        assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x.toUpperCase()));
30
+    }
31
+
32
+    @Test
33
+    public void accumulateReversedStrings() {
34
+        List<String> input = Arrays.asList("the quick brown fox etc".split(" "));
35
+        List<String> expectedOutput = Arrays.asList("eht kciuq nworb xof cte".split(" "));
36
+        assertEquals(expectedOutput, Accumulate.accumulate(input, this::reverse));
37
+    }
38
+
39
+    private String reverse(String input) {
40
+        return new StringBuilder(input).reverse().toString();
41
+    }
42
+
43
+    @Test
44
+    public void accumulateWithinAccumulate() {
45
+        List<String> input1 = Arrays.asList("a", "b", "c");
46
+        List<String> input2 = Arrays.asList("1", "2", "3");
47
+        List<String> expectedOutput = Arrays.asList("a1 a2 a3", "b1 b2 b3", "c1 c2 c3");
48
+        assertEquals(expectedOutput, Accumulate.accumulate(
49
+                input1, c ->
50
+                        String.join(" ", Accumulate.accumulate(input2, d -> c + d))
51
+        ));
52
+    }
53
+}

+ 2
- 1
config.json 查看文件

@@ -24,7 +24,8 @@
24 24
     "raindrops",
25 25
     "allergies",
26 26
     "strain",
27
-    "atbash-cipher"
27
+    "atbash-cipher",
28
+    "accumulate"
28 29
   ],
29 30
   "deprecated": [
30 31
   ],