Monadics { public static <A> ListMonadic<A> mlist(final Collection<A> as) { return mlist(new ArrayList<A>(as)); } private  Monadics(); static ListMonadic<A> mlist(final Collection<A> as); static ListMonadic<A> mlist(final List<A> as); static ListMonadic<A> mlist(final A... as); static ListMonadic<A> mlist(final Iterator<A> as); static IteratorMonadic<A> mlazy(final Iterator<A> as); static IteratorMonadic<A> mlazy(final List<A> as);  }
@Test public void testMap() { List<Integer> list = asList(1, 2, 3); List<String> mapped = mlist(list).map(new Function<Integer, Integer>() { @Override public Integer apply(Integer a) { return a * a; } }).map(new Function<Integer, String>() { @Override public String apply(Integer a) { return a + " " + a; } }).value(); assertEquals("1 1", mapped.get(0)); assertEquals("4 4", mapped.get(1)); assertEquals("9 9", mapped.get(2)); }
@Test public void testFlatMap() { List<Integer> mapped = mlist(new Integer[]{1, 2, 3}).flatMap(new Function<Integer, Collection<Integer>>() { @Override public Collection<Integer> apply(Integer a) { return asList(a, a); } }).value(); assertEquals(6, mapped.size()); assertEquals(new Integer(1), mapped.get(0)); assertEquals(new Integer(2), mapped.get(2)); assertEquals(new Integer(3), mapped.get(4)); }
@Test public void testFlatMap2() { final List<Object> l = list((Object) 1); final List<String> r1 = mlist(l).bind(new Function<Object, Option<String>>() { @Override public Option<String> apply(Object o) { return some("x"); } }).value(); assertEquals(1, r1.size()); assertEquals("x", r1.get(0)); final List<String> r2 = mlist(l).bind(new Function<Object, Option<String>>() { @Override public Option<String> apply(Object o) { return none(); } }).value(); assertEquals(0, r2.size()); }
@Test public void testFoldl() { String fold = mlist(new Integer[]{1, 2, 3}).foldl("", new Function2<String, Integer, String>() { @Override public String apply(String s, Integer a) { return s + a + a; } }); assertEquals("112233", fold); }
@Test public void testReducel() { String fold = mlist("a", "b", "c").reducel(new Function2<String, String, String>() { @Override public String apply(String a, String b) { return a + "," + b; } }); assertEquals("a,b,c", fold); }
@Test(expected = RuntimeException.class) public void testReducelError() { mlist(new String[]{}).reducel(new Function2<String, String, String>() { @Override public String apply(String a, String b) { return a + "," + b; } }); }
@Test public void testFlatten() { List<Integer> mapped = mlist(list(list(1, 2), list(3, 4))).flatMap(Functions.<List<Integer>>identity()).value(); assertEquals(4, mapped.size()); List<Integer> mapped2 = mlist(some(1), Option.<Integer>option(null), some(3), Option.<Integer>none()).flatMap(Functions.<Option<Integer>>identity()).value(); assertEquals(2, mapped2.size()); }
@Test public void testTakeArray() { assertTrue(mlist(array(1, 2, 3, 4, 5)).take(0).value().isEmpty()); assertEquals(3, mlist(array(1, 2, 3, 4, 5)).take(3).value().size()); assertEquals(5, mlist(array(1, 2, 3, 4, 5)).take(5).value().size()); assertEquals(5, mlist(array(1, 2, 3, 4, 5)).take(10).value().size()); }
@Test public void testTakeList() { assertTrue(mlist(asList(1, 2, 3, 4, 5)).take(0).value().isEmpty()); assertEquals(3, mlist(asList(1, 2, 3, 4, 5)).take(3).value().size()); assertEquals(5, mlist(asList(1, 2, 3, 4, 5)).take(5).value().size()); assertEquals(5, mlist(asList(1, 2, 3, 4, 5)).take(10).value().size()); }
@Test public void testTakeIterator() { assertTrue(mlist(asList(1, 2, 3, 4, 5).iterator()).take(0).value().isEmpty()); assertEquals(3, mlist(asList(1, 2, 3, 4, 5).iterator()).take(3).value().size()); assertEquals(5, mlist(asList(1, 2, 3, 4, 5).iterator()).take(5).value().size()); assertEquals(5, mlist(asList(1, 2, 3, 4, 5).iterator()).take(10).value().size()); }
@Test public void testDropArray() { assertTrue(mlist(array(1, 2, 3, 4, 5)).drop(10).value().isEmpty()); assertEquals(3, mlist(array(1, 2, 3, 4, 5)).drop(2).value().size()); assertEquals(1, mlist(array(1, 2, 3, 4, 5)).drop(4).value().size()); assertEquals(5, mlist(array(1, 2, 3, 4, 5)).drop(0).value().size()); }
@Test public void testZip() { { List<Tuple<String, Integer>> r = mlist(new String[]{"a", "b", "c"}).zip(list(1, 2, 3, 4, 5)).value(); assertEquals(3, r.size()); assertEquals(tuple("b", 2), r.get(1)); } { List<Tuple<String, Integer>> r = mlist(new String[]{"a", "b", "c"}).zip(Collections.<Integer>nil()).value(); assertEquals(0, r.size()); } { List<Tuple<String, Integer>> r = mlist(list("a", "b", "c")).zip(list(1, 2)).value(); assertEquals(2, r.size()); assertEquals(tuple("b", 2), r.get(1)); } { List<Tuple<String, Integer>> r = mlist(list("a", "b", "c").iterator()).zip(list(1, 2)).value(); assertEquals(2, r.size()); assertEquals(tuple("b", 2), r.get(1)); } { List<Tuple<String, Integer>> r = mlist(Collections.<String>nil().iterator()).zip(list(1, 2)).value(); assertEquals(0, r.size()); } }
@Test public void testConcat() { final List<Integer> a = mlist(1, 2, 3).concat(list(4, 5)).value(); assertEquals(5, a.size()); assertEquals(4, (Object) a.get(3)); final List<Object> b = mlist((Object) 1).concat(Collections.<Object>list("x")).value(); assertEquals(2, b.size()); assertEquals(1, b.get(0)); assertEquals("x", b.get(1)); }