# rootulp's solution

## to Accumulate in the Java Track

Published at Jul 13 2018 · 2 comments
Instructions
Test suite
Solution

Implement the `accumulate` operation, which, given a collection and an operation to perform on each element of the collection, returns a new collection containing the result of applying that operation to each element of the input collection.

Given the collection of numbers:

• 1, 2, 3, 4, 5

And the operation:

• square a number (`x => x * x`)

Your code should be able to produce the collection of squares:

• 1, 4, 9, 16, 25

Check out the test suite to see the expected function signature.

## Restrictions

Keep your hands off that collect/map/fmap/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.

# Running the tests

You can run all the tests for an exercise by entering

``````\$ gradle test
``````

## Source

Conversation with James Edward Gray II https://twitter.com/jeg2

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### AccumulateTest.java

``````import org.junit.Test;
import org.junit.Ignore;

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

import static org.junit.Assert.assertEquals;

public class AccumulateTest {

@Test
public void emptyAccumulateProducesEmptyAccumulation() {
assertEquals(expectedOutput, Accumulate.accumulate(input, x -> x * x));
}

@Ignore("Remove to run test")
@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));
}

@Ignore("Remove to run test")
@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()));
}

@Ignore("Remove to run test")
@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();
}

@Ignore("Remove to run test")
@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))
));
}
}``````
``````import java.util.List;
import java.util.ArrayList;
import java.util.function.Function;

public class Accumulate {
public static <T> List<T> accumulate(List<T> list, Function<T, T> f) {
List<T> results = new ArrayList<>();

for (T item : list) {
}

return results;
}
}``````