Skip to content

Commit 8be6e33

Browse files
committed
2 parents b46c4d1 + aec491c commit 8be6e33

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

core/src/main/java/fj/data/List.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,15 @@ public final A index(final int i) {
795795
* @return A new list with a length the same, or less than, this list.
796796
*/
797797
public final List<A> take(final int i) {
798-
return i <= 0 || isEmpty() ? List.<A>nil() : cons(head(), tail().take(i - 1));
798+
Buffer<A> result = Buffer.empty();
799+
List<A> list = this;
800+
int index = i;
801+
while (index > 0 && list.isNotEmpty()) {
802+
result.snoc(list.head());
803+
list = list.tail();
804+
index--;
805+
}
806+
return result.toList();
799807
}
800808

801809
/**
@@ -823,16 +831,19 @@ public final List<A> drop(final int i) {
823831
* @return A pair of lists split at the given index of this list.
824832
*/
825833
public final P2<List<A>, List<A>> splitAt(final int i) {
826-
P2<List<A>, List<A>> s = p(List.<A>nil(), List.<A>nil());
827-
828834
int c = 0;
835+
List<A> first = List.<A>nil();
836+
List<A> second = List.nil();
829837
for (List<A> xs = this; xs.isNotEmpty(); xs = xs.tail()) {
830838
final A h = xs.head();
831-
s = c < i ? s.map1(as -> as.snoc(h)) : s.map2(as1 -> as1.snoc(h));
839+
if (c < i) {
840+
first = first.cons(h);
841+
} else {
842+
second = second.cons(h);
843+
}
832844
c++;
833845
}
834-
835-
return s;
846+
return P.p(first.reverse(), second.reverse());
836847
}
837848

838849
/**

core/src/main/java/fj/data/Stream.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818
import fj.Ordering;
1919
import fj.function.Effect1;
2020

21-
import java.util.AbstractCollection;
22-
import java.util.Collection;
23-
import java.util.Iterator;
24-
import java.util.NoSuchElementException;
21+
import java.util.*;
2522

2623
import static fj.Bottom.error;
2724
import static fj.Function.compose;
@@ -232,7 +229,7 @@ public Stream<A> _1() {
232229

233230
public Stream<A> prefix(final A x, final Stream<A> xs) {
234231
return xs.isEmpty() ? xs : cons(x, p(cons(xs.head(), () -> prefix(a, xs.tail()._1()))));
235-
}
232+
}
236233
});
237234
}
238235

@@ -553,6 +550,14 @@ public final Stream<A> interleave(final Stream<A> as) {
553550
return isEmpty() ? as : as.isEmpty() ? this : cons(head(), () -> as.interleave(tail()._1()));
554551
}
555552

553+
public static <A> Stream<A> enumerationStream(Enumeration<A> e) {
554+
if (e.hasMoreElements()) {
555+
return Stream.cons(e.nextElement(), () -> enumerationStream(e));
556+
} else {
557+
return Stream.nil();
558+
}
559+
}
560+
556561
/**
557562
* Sort this stream according to the given ordering.
558563
*

0 commit comments

Comments
 (0)