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;
 }
예제 #4
0
 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;
 }
예제 #6
0
 @Override
 public <U> Result<U> map(Function<T, U> f) {
   return success(f.apply(value));
 }
예제 #7
0
 @Override
 public List<A> filter(Function<A, Boolean> f) {
   return foldRight(list(), h -> t -> f.apply(h) ? new Cons<>(h, t) : t);
 }
예제 #8
0
 @Override
 public <B> List<B> map(Function<A, B> f) {
   return foldRight(list(), h -> t -> new Cons<>(f.apply(h), t));
 }
예제 #9
0
 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);
 }
예제 #10
0
파일: List.java 프로젝트: bcalmac/fpinjava
 @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);
 }
예제 #11
0
 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)));
 }
예제 #14
0
 public static <A, B> Rand<B> map(Rand<A> s, Function<A, B> f) {
   return flatMap(s, a -> unit(f.apply(a)));
 }
예제 #15
0
 public Result<T> filter(Function<T, Boolean> p) {
   return flatMap(x -> p.apply(x) ? this : failure("Condition not matched"));
 }
예제 #16
0
 public Result<T> filter(Function<T, Boolean> p, String message) {
   return flatMap(x -> p.apply(x) ? this : failure(message));
 }
예제 #17
0
 @Override
 public List<A> filterViaFlatmap(Function<A, Boolean> f) {
   return flatMap(a -> f.apply(a) ? List.list(a) : List.list());
 }