diff --git a/src/test/java/EasyTest.java b/src/test/java/EasyTest.java index 31fec83..1994dfa 100644 --- a/src/test/java/EasyTest.java +++ b/src/test/java/EasyTest.java @@ -4,6 +4,7 @@ import org.organicdesign.fp.ephemeral.View; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; @RunWith(JUnit4.class) public class EasyTest { @@ -11,6 +12,7 @@ public class EasyTest { @Test public void helloWorld() { assertArrayEquals(new String[] { "Hello World" }, View.ofArray("Hello World").toArray()); + assertEquals("Hello World", "Hello World"); } // Construct an array by passing individual arguments to View @Test public void fromAndToArray() { @@ -60,11 +62,18 @@ public class EasyTest { // View.ofArray(1, 2, 3, 4).map(???).toArray()); } - // What higher-order function is used to accept numbers that match a certain test? + // Write a function to accept numbers that match a certain test @Test public void filterEven() { // assertArrayEquals(new Integer[]{2, 4, 6, 8}, // View.ofArray(1,2,3,4,5,6,7,8,9) -// .???(i -> i % 2 == 0).toArray()); +// .filter(i -> ???).toArray()); + } + + // Concatenate some strings - try using the new :: method pointer syntax + @Test public void concat() { +// StringBuilder sB = new StringBuilder(); +// View.ofArray("Little ", "strokes ", "fell ", "great ", "oaks.").forEach(???); +// assertEquals("Little strokes fell great oaks.", sB.toString()); } // Chain together two higher order functions diff --git a/src/test/java/MediumTest.java b/src/test/java/MediumTest.java index f7f409d..8f8054d 100644 --- a/src/test/java/MediumTest.java +++ b/src/test/java/MediumTest.java @@ -39,10 +39,9 @@ public void foldLeftTimes() { // .foldLeft(???, (accum, item) -> accum * item)); } - - // You can use foldLeft to do the same work as map() + // You can use foldLeft to do the same work as map() (but use map() in practice) @Test - public void foldLeftMap() { + public void foldLeftMapLike() { // assertArrayEquals(new Integer[] { 2, 3, 4, 5 }, // View.ofArray(1, 2, 3, 4) // .foldLeft(new ArrayList<>(), (ls, i) -> { @@ -51,4 +50,33 @@ public void foldLeftMap() { // }).toArray()); } + // foldLeft can do the work of flatMap() too (but use flatMap() in practice) + @Test + public void foldLeftFlatMapLike() { +// assertArrayEquals(new Integer[] { 1,2,3, 10,20,30, 100,200,300 }, +// View.ofArray(1, 10, 100) +// .foldLeft(new ArrayList<>(), (ls, i) -> { +// ???; +// ???; +// ???; +// return ls; +// }).toArray()); + } + + // foldLeft can find a maximum number + @Test + public void findMax() { +// assertEquals((Integer) 73, +// View.ofArray(-201, 53, 73, 26, -59, 72) +// .foldLeft(???, (max, i) -> (i > max) ? i : max)); + } + + // or a minimum + @Test + public void findMin() { +// assertEquals(Integer.valueOf(-201), +// View.ofArray(-201, 53, 73, 26, -59, 72) +// .foldLeft(Integer.MAX_VALUE, ???)); + } + }