public static <T> List<T> iterate(T seed, Function<T, T> f, Function<T, Boolean> p) { List<T> result = new ArrayList<>(); T temp = seed; while (p.apply(temp)) { result.add(temp); temp = f.apply(temp); } return result; }
public static <T> List<T> unfold(T seed, Function<T, T> f, Function<T, Boolean> p) { List<T> list = new ArrayList<>(); T temp = seed; while (p.apply(temp)) { list = append(list, temp); temp = f.apply(temp); } return list; }
public static <T, U> List<U> map(List<T> l, Function<T, U> f) { List<U> newList = new ArrayList<>(); for (T value : l) { newList.add(f.apply(value)); } return newList; }
public static <T> Result<T> of(Function<T, Boolean> predicate, T value) { try { return predicate.apply(value) ? success(value) : empty(); } catch (Exception e) { String errMessage = String.format("Exception while evaluating predicate: %s", value); return Result.failure(new IllegalStateException(errMessage, e)); } }
public static <T> List<T> iterate(T seed, Function<T, T> f, int n) { List<T> result = new ArrayList<>(); T temp = seed; for (int i = 0; i < n; i++) { result.add(temp); temp = f.apply(temp); } return result; }
@Override public <U> Result<U> map(Function<T, U> f) { return success(f.apply(value)); }
@Override public List<A> filter(Function<A, Boolean> f) { return foldRight(list(), h -> t -> f.apply(h) ? new Cons<>(h, t) : t); }
@Override public <B> List<B> map(Function<A, B> f) { return foldRight(list(), h -> t -> new Cons<>(f.apply(h), t)); }
private TailCall<List<A>> dropWhile_(List<A> list, Function<A, Boolean> f) { return !list.isEmpty() && f.apply(list.head()) ? sus(() -> dropWhile_(list.tail(), f)) : ret(list); }
@Override public <B> B foldRight(B identity, Function<A, Function<B, B>> f) { return foldLeft(Function.<B>identity(), g -> a -> b -> g.apply(f.apply(a).apply(b))) .apply(identity); }
public static <A, B> Rand<B> map_(Rand<A> s, Function<A, B> f) { return rng -> { Tuple<A, RNG> t = s.apply(rng); return new Tuple<>(f.apply(t._1), t._2); }; }
public static <T, U> List<U> mapViaFoldRight(List<T> list, Function<T, U> f) { return foldRight(list, list(), x -> y -> prepend(f.apply(x), y)); }
public static <T, U> List<U> mapViaFoldLeft(List<T> list, Function<T, U> f) { return foldLeft(list, list(), x -> y -> append(x, f.apply(y))); }
public static <A, B> Rand<B> map(Rand<A> s, Function<A, B> f) { return flatMap(s, a -> unit(f.apply(a))); }
public Result<T> filter(Function<T, Boolean> p) { return flatMap(x -> p.apply(x) ? this : failure("Condition not matched")); }
public Result<T> filter(Function<T, Boolean> p, String message) { return flatMap(x -> p.apply(x) ? this : failure(message)); }
@Override public List<A> filterViaFlatmap(Function<A, Boolean> f) { return flatMap(a -> f.apply(a) ? List.list(a) : List.list()); }