Commits

Stepan Koltsov committed f2ca118

Cf.List.concat(ListF<ListF<A>>)

Comments (0)

Files changed (2)

src/main/java/ru/yandex/bolts/type/collection/AnyListType.java

         };
     }
 
+    public <A> ListF<A> concat(ListF<ListF<A>> lists) {
+        Function2<Integer, Collection<?>, Integer> c = Cf.Integer.plusF().compose2(Cf.List.sizeF());
+        int totalLength = lists.foldLeft(0, c);
+        ListF<A> r = Cf.arrayList(totalLength);
+        for (ListF<A> l : lists) {
+            r.addAll(l);
+        }
+        return r.makeReadOnly();
+    }
+
     public <A> Function2<List<A>, Function1B<? super A>, ListF<A>> filterF() {
         return new Function2<List<A>, Function1B<? super A>, ListF<A>>() {
             public ListF<A> apply(List<A> set, Function1B<? super A> f) {

src/main/java/ru/yandex/bolts/type/collection/AnyListTypeTest.java

+package ru.yandex.bolts.type.collection;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import ru.yandex.bolts.collection.Cf;
+import ru.yandex.bolts.collection.ListF;
+
+/**
+ * @author Stepan Koltsov
+ */
+public class AnyListTypeTest {
+
+    @SuppressWarnings("unchecked")
+    @Test
+    public void concat() {
+        Assert.assertEquals(Cf.list(), Cf.List.concat(Cf.<ListF<Object>>list()));
+        Assert.assertEquals(Cf.list(), Cf.List.concat(Cf.list(Cf.list(), Cf.list())));
+        Assert.assertEquals(Cf.list(1, 2, 3, 4, 5, 6), Cf.List.concat(Cf.list(Cf.list(1), Cf.list(2, 3), Cf.list(4, 5, 6))));
+    }
+
+} //~