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

Added exercise accumulate #37

Merged
merged 1 commit into from
Apr 4, 2015
Merged
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
11 changes: 11 additions & 0 deletions accumulate/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apply plugin: "java"
apply plugin: "eclipse"
apply plugin: "idea"

repositories {
mavenCentral()
}

dependencies {
testCompile "junit:junit:4.10"
}
17 changes: 17 additions & 0 deletions accumulate/example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Function;

public class Accumulate {

public static <T> Collection<T> accumulate(Collection<T> collection, Function<T, T> function) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use a Collection? The tests don't demand a collection... actually, they assume that the order of elements will be preserved.

(Also, not probably necessarily the right place for this discussion @kytrinyx, but this function seems more like map than accumulate. See for example SICP §2.3.3.)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point about order specifically, I've created issue #38 for this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, in the Ruby standard library this behavior is called both map and collect (one is an alias of the other). I'm not sure why I chose accumulate as the name for this :/

List<T> newCollection = new ArrayList<>();

for (T item : collection) {
newCollection.add(function.apply(item));
}

return newCollection;
}
}
Empty file added accumulate/src/main/java/.keep
Empty file.
2 changes: 2 additions & 0 deletions accumulate/src/main/java/Accumulate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class Accumulate {
}
53 changes: 53 additions & 0 deletions accumulate/src/test/java/AccumulateTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import org.junit.Test;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class AccumulateTest {

@Test
public void emptyAccumulateProducesEmptyAccumulation() {
List<Integer> input = new LinkedList<>();
List<Integer> expectedOutput = new LinkedList<>();
assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x * x));
}

@Test
public void accumulateSquares() {
List<Integer> input = Arrays.asList(1, 2, 3);
List<Integer> expectedOutput = Arrays.asList(1, 4, 9);
assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x * x));
}

@Test
public void accumulateUpperCases() {
List<String> input = Arrays.asList("hello", "world");
List<String> expectedOutput = Arrays.asList("HELLO", "WORLD");
assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x.toUpperCase()));
}

@Test
public void accumulateReversedStrings() {
List<String> input = Arrays.asList("the quick brown fox etc".split(" "));
List<String> expectedOutput = Arrays.asList("eht kciuq nworb xof cte".split(" "));
assertEquals(expectedOutput, Accumulate.accumulate(input, this::reverse));
}

private String reverse(String input) {
return new StringBuilder(input).reverse().toString();
}

@Test
public void accumulateWithinAccumulate() {
List<String> input1 = Arrays.asList("a", "b", "c");
List<String> input2 = Arrays.asList("1", "2", "3");
List<String> expectedOutput = Arrays.asList("a1 a2 a3", "b1 b2 b3", "c1 c2 c3");
assertEquals(expectedOutput, Accumulate.accumulate(
input1, c ->
String.join(" ", Accumulate.accumulate(input2, d -> c + d))
));
}
}
3 changes: 2 additions & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"raindrops",
"allergies",
"strain",
"atbash-cipher"
"atbash-cipher",
"accumulate"
],
"deprecated": [
],
Expand Down