-
-
Notifications
You must be signed in to change notification settings - Fork 666
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
List<T> newCollection = new ArrayList<>(); | ||
|
||
for (T item : collection) { | ||
newCollection.add(function.apply(item)); | ||
} | ||
|
||
return newCollection; | ||
} | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
public class Accumulate { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
thanaccumulate
. See for example SICP §2.3.3.)There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
andcollect
(one is an alias of the other). I'm not sure why I choseaccumulate
as the name for this :/