default ImmutableList<T> replaceFirst(T currentElement, T newElement){ ImmutableList<T> preceding = emptyUnit(); ImmutableList<T> tail = this; while(!tail.isEmpty()){ ImmutableList<T> ref= preceding; ImmutableList<T> tailRef = tail; Tuple3<ImmutableList<T>, ImmutableList<T>, Boolean> t3 = tail.fold(c -> { if (Objects.equals(c.head(), currentElement)) return Tuple.tuple(ref, tailRef, true); return Tuple.tuple(ref.prepend(c.head()), c.tail(), false); }, nil -> Tuple.tuple(ref, tailRef, true)); preceding = t3._1(); tail = t3._2(); if(t3._3()) break; } ImmutableList<T> start = preceding; return tail.fold(cons->cons.tail().prepend(newElement).prependAll(start), nil->this); } default ImmutableList<T> removeFirst(Predicate<? super T> pred){
@Override public NonEmptyList<ReactiveSeq<T>> combinations(int size) { return (NonEmptyList<ReactiveSeq<T>>)ImmutableList.Some.super.combinations(size); }
@Override public NonEmptyList<T> appendAll(T... values) { return (NonEmptyList<T>) ImmutableList.Some.super.appendAll(values); }
@Override public NonEmptyList<Vector<T>> grouped(int groupSize) { return (NonEmptyList<Vector<T>>) ImmutableList.Some.super.grouped(groupSize); }
default Option<Zipper<T>> focusAt(int pos){ Tuple2<ImmutableList<T>, ImmutableList<T>> t2 = splitAt(pos); Option<T> value = t2._2().fold(c -> Option.some(c.head()), n -> Option.none()); return value.map(l-> { ImmutableList<T> right = t2._2().fold(c -> c.tail(), n -> null); return Zipper.of(t2._1(), l, right); }); }
@Override public NonEmptyList<T> replaceFirst(T currentElement, T newElement){ return ImmutableList.Some.super.replaceFirst(currentElement,newElement) .fold(cons->cons(cons.head(),cons.tail()),nil->this); }
@Override public <C extends PersistentCollection<? super T>> NonEmptyList<C> grouped(int size, Supplier<C> supplier) { return (NonEmptyList<C>) ImmutableList.Some.super.grouped(size,supplier); }
@Override public NonEmptyList<T> distinct() { return (NonEmptyList<T>) ImmutableList.Some.super.distinct(); }
@Override public <C extends PersistentCollection<? super T>> NonEmptyList<C> groupedUntil(Predicate<? super T> predicate, Supplier<C> factory) { return (NonEmptyList<C>) ImmutableList.Some.super.groupedUntil(predicate,factory); }
@Override public NonEmptyList<Vector<T>> groupedUntil(Predicate<? super T> predicate) { return (NonEmptyList<Vector<T>>) ImmutableList.Some.super.groupedUntil(predicate); }
@Override public NonEmptyList<Vector<T>> groupedWhile(Predicate<? super T> predicate) { return (NonEmptyList<Vector<T>>) ImmutableList.Some.super.groupedWhile(predicate); }