take method
Returns a new collection containing only the first n elements.
Implementation
@override
IList<A> take(int n) {
final nilA = Nil<A>();
if (isEmpty || n <= 0) return nilA;
final h = Cons(head, nilA);
var t = h;
var rest = tail;
var i = 1;
if (rest.isEmpty) return this;
while (i < n) {
if (rest.isEmpty) return this;
i += 1;
final nx = Cons(rest.head, nilA);
t.next = nx;
t = nx;
rest = rest.tail;
}
return h;
}