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

Add Koans for Map and Filter #7

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String, Boolean> nonEmptyString = s -> !s.isEmpty();

Iterable<String> strings = emptyList();
Iterable<String> filteredStrings = filter(nonEmptyString, strings);

assertThat(filteredStrings, iterates(Koans.<String>__("Replace with whatever strings you expect to be in filteredStrings")));
}


@Test
public void filteredElementsStayIn() {
Fn1<String, Boolean> nonEmptyString = s -> !s.isEmpty();

Iterable<String> strings = asList("", "Hello", "", "World", "!");
Iterable<String> filteredStrings = filter(nonEmptyString, strings);

assertThat(filteredStrings, iterates(Koans.<String>__("Replace with whatever strings you expect to be in filteredStrings")));
}

@Test
public void removesLongStrings() {
Fn1<String, Boolean> isShortString = s -> s.length() <= 5;

Iterable<String> strings = asList("Hello", "World!", "Functional", "Code");
Iterable<String> filteredStrings = __();

assertThat(filteredStrings, iterates("Hello", "Code"));
}

@Test
public void filtersWithAlwaysTrue() {
Fn1<Integer, Boolean> alwaysTrue = s -> true; // Same as constantly(true)

Iterable<Integer> ints = asList(1, 2, 3, 4, 5);
Iterable<Integer> filteredInts = filter(alwaysTrue, ints);

assertThat(filteredInts, iterates(Koans.<Integer>__("Replace with whatever strings you expect to be in filteredInts")));
}

@Test
public void filtersWithAlwaysFalse() {
Fn1<Integer, Boolean> alwaysFalse = s -> false; // Same as constantly(false)

Iterable<Integer> ints = asList(1, 2, 3, 4, 5);
Iterable<Integer> filteredInts = filter(alwaysFalse, ints);

assertThat(filteredInts, iterates(__("Replace with whatever strings you expect to be in filteredInts")));
}

@Test
public void removeOdds() {
Fn1<Integer, Boolean> fn = __();

Iterable<Integer> ints = asList(1, 2, 3, 4, 5, 6);
Iterable<Integer> filteredInts = filter(fn, ints);

assertThat(filteredInts, iterates(2, 4, 6));
}

@Test
public void keepEvenStringsThatArentEmpty() {
Fn1<String, Boolean> isEven = __("Keep Even Strings");
Fn1<String, Boolean> isNotEmpty = __("Remove Empty Strings");

List<String> strings = asList("", "a", "ab", "abc", "abcd", "abcde", "abcdef");
Iterable<String> filteredInts = filter(isNotEmpty, filter(isEven, strings));

assertThat(filteredInts, iterates("ab", "abcd", "abcdef"));
}

// Complete AboutMap first
@Test
public void combineMapAndFilter() {
Fn1<Integer, Integer> halve = i -> i / 2;
Fn1<Integer, Boolean> isEven = i -> i % 2 == 0;

Iterable<Integer> ints = asList(1, 6, 9, 14, 11, 32);
Iterable<Integer> halvedEvens = __("Use Filter and Map with the above functions to return the even numbers divided by 2");

assertThat(halvedEvens, iterates(3, 7, 16));
}

}
Original file line number Diff line number Diff line change
@@ -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<Integer, Integer> increment = __();

Iterable<Integer> list = asList(1, 2, 3, 4, 5);
Iterable<Integer> incremented = map(increment, list);

assertThat(incremented, iterates(2, 3, 4, 5, 6));
}

@Test
public void mapsToADifferentType() {
Fn1<Integer, Double> halve = __();

Iterable<Integer> list = asList(1, 2, 3, 4, 5);
Iterable<Double> halved = map(halve, list);

assertThat(halved, iterates(0.5, 1.0, 1.5, 2.0, 2.5));
}

@Test
public void useMapToConvertTypes() {
Fn1<Boolean, String> serialize = b -> b ? "True" : "False";

Iterable<Boolean> list = asList(true, false, false, true);
Iterable<String> strings = __();

assertThat(strings, iterates("True", "False", "False", "True"));
}

@Test
public void mappingsCompose() {
Fn1<Integer, Boolean> isEven = i -> i % 2 == 0;
Fn1<Boolean, String> serialize = b -> b ? "Even" : "Odd";

List<Integer> list = asList(1, 2, 3, 4, 5);
Iterable<String> strings = __(); // Call map twice to map over both functions

assertThat(strings, iterates("Odd", "Even", "Odd", "Even", "Odd"));
}

@Test
public void predictMappings() {
Fn1<String, Integer> stringLength = String::length;
Fn1<Integer, Boolean> isEven = i -> i % 2 == 0;
Fn1<Boolean, String> serialize = b -> b ? "Even" : "Odd";

Iterable<String> strings = asList("first", "second", "third", "fourth", "fifth");
Iterable<String> newStrings = map(serialize, map(isEven, map(stringLength, strings)));

assertThat(newStrings, iterates(__(), __(), __(), __(), __()));
}

}