diff --git a/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java new file mode 100644 index 0000000..54c9895 --- /dev/null +++ b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutFilter.java @@ -0,0 +1,102 @@ +package com.jnape.palatable.lambdakoans.builtin.fn2; + +import com.jnape.palatable.lambda.functions.Fn1; +import com.jnape.palatable.lambdakoans.Koans; +import org.hamcrest.collection.IsIterableWithSize; +import org.junit.Test; + +import java.util.List; + +import static com.jnape.palatable.lambda.functions.builtin.fn2.Filter.filter; +import static com.jnape.palatable.lambdakoans.Koans.__; +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import static org.hamcrest.MatcherAssert.assertThat; +import static testsupport.matchers.IterableMatcher.iterates; + +public class AboutFilter { + @Test + public void filterDoesWhatForEmptyList() { + Fn1 nonEmptyString = s -> !s.isEmpty(); + + Iterable strings = emptyList(); + Iterable filteredStrings = filter(nonEmptyString, strings); + + assertThat(filteredStrings, iterates(Koans.__("Replace with whatever strings you expect to be in filteredStrings"))); + } + + + @Test + public void filteredElementsStayIn() { + Fn1 nonEmptyString = s -> !s.isEmpty(); + + Iterable strings = asList("", "Hello", "", "World", "!"); + Iterable filteredStrings = filter(nonEmptyString, strings); + + assertThat(filteredStrings, iterates(Koans.__("Replace with whatever strings you expect to be in filteredStrings"))); + } + + @Test + public void removesLongStrings() { + Fn1 isShortString = s -> s.length() <= 5; + + Iterable strings = asList("Hello", "World!", "Functional", "Code"); + Iterable filteredStrings = __(); + + assertThat(filteredStrings, iterates("Hello", "Code")); + } + + @Test + public void filtersWithAlwaysTrue() { + Fn1 alwaysTrue = s -> true; // Same as constantly(true) + + Iterable ints = asList(1, 2, 3, 4, 5); + Iterable filteredInts = filter(alwaysTrue, ints); + + assertThat(filteredInts, iterates(Koans.__("Replace with whatever strings you expect to be in filteredInts"))); + } + + @Test + public void filtersWithAlwaysFalse() { + Fn1 alwaysFalse = s -> false; // Same as constantly(false) + + Iterable ints = asList(1, 2, 3, 4, 5); + Iterable filteredInts = filter(alwaysFalse, ints); + + assertThat(filteredInts, iterates(__("Replace with whatever strings you expect to be in filteredInts"))); + } + + @Test + public void removeOdds() { + Fn1 fn = __(); + + Iterable ints = asList(1, 2, 3, 4, 5, 6); + Iterable filteredInts = filter(fn, ints); + + assertThat(filteredInts, iterates(2, 4, 6)); + } + + @Test + public void keepEvenStringsThatArentEmpty() { + Fn1 isEven = __("Keep Even Strings"); + Fn1 isNotEmpty = __("Remove Empty Strings"); + + List strings = asList("", "a", "ab", "abc", "abcd", "abcde", "abcdef"); + Iterable filteredInts = filter(isNotEmpty, filter(isEven, strings)); + + assertThat(filteredInts, iterates("ab", "abcd", "abcdef")); + } + + // Complete AboutMap first + @Test + public void combineMapAndFilter() { + Fn1 halve = i -> i / 2; + Fn1 isEven = i -> i % 2 == 0; + + Iterable ints = asList(1, 6, 9, 14, 11, 32); + Iterable halvedEvens = __("Use Filter and Map with the above functions to return the even numbers divided by 2"); + + assertThat(halvedEvens, iterates(3, 7, 16)); + } + +} diff --git a/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutMap.java b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutMap.java new file mode 100644 index 0000000..a5dc3fb --- /dev/null +++ b/src/test/java/com/jnape/palatable/lambdakoans/builtin/fn2/AboutMap.java @@ -0,0 +1,68 @@ +package com.jnape.palatable.lambdakoans.builtin.fn2; + +import com.jnape.palatable.lambda.functions.Fn1; +import org.junit.Test; + +import java.util.List; + +import static com.jnape.palatable.lambda.functions.builtin.fn2.Map.map; +import static com.jnape.palatable.lambdakoans.Koans.__; +import static java.util.Arrays.asList; +import static org.hamcrest.MatcherAssert.assertThat; +import static testsupport.matchers.IterableMatcher.iterates; + +public class AboutMap { + @Test + public void mapsValuesWithinAType() { + Fn1 increment = __(); + + Iterable list = asList(1, 2, 3, 4, 5); + Iterable incremented = map(increment, list); + + assertThat(incremented, iterates(2, 3, 4, 5, 6)); + } + + @Test + public void mapsToADifferentType() { + Fn1 halve = __(); + + Iterable list = asList(1, 2, 3, 4, 5); + Iterable halved = map(halve, list); + + assertThat(halved, iterates(0.5, 1.0, 1.5, 2.0, 2.5)); + } + + @Test + public void useMapToConvertTypes() { + Fn1 serialize = b -> b ? "True" : "False"; + + Iterable list = asList(true, false, false, true); + Iterable strings = __(); + + assertThat(strings, iterates("True", "False", "False", "True")); + } + + @Test + public void mappingsCompose() { + Fn1 isEven = i -> i % 2 == 0; + Fn1 serialize = b -> b ? "Even" : "Odd"; + + List list = asList(1, 2, 3, 4, 5); + Iterable strings = __(); // Call map twice to map over both functions + + assertThat(strings, iterates("Odd", "Even", "Odd", "Even", "Odd")); + } + + @Test + public void predictMappings() { + Fn1 stringLength = String::length; + Fn1 isEven = i -> i % 2 == 0; + Fn1 serialize = b -> b ? "Even" : "Odd"; + + Iterable strings = asList("first", "second", "third", "fourth", "fifth"); + Iterable newStrings = map(serialize, map(isEven, map(stringLength, strings))); + + assertThat(newStrings, iterates(__(), __(), __(), __(), __())); + } + +}