/** * Returns a parser that produces an element from the stream that satisfies the given predicate, * or fails. * * @param missing The error if no element is available. * @param sat The error if the element does not satisfy the predicate. * @param f The predicate that the element should satisfy. * @return A parser that produces an element from the stream that satisfies the given predicate, * or fails. */ public static <I, E> Parser<Stream<I>, I, E> satisfy( final F0<E> missing, final F<I, E> sat, final F<I, Boolean> f) { return StreamParser.<I, E>element(missing) .bind( x -> f.f(x) ? Parser.<Stream<I>, I, E>value(x) : Parser.<Stream<I>, I, E>fail(sat.f(x))); }
public <A, B> HashMap<A, B> map( F<K, A> keyFunction, F<V, B> valueFunction, Equal<A> equal, Hash<A> hash) { final HashMap<A, B> hashMap = new HashMap<>(equal, hash); for (K key : keys()) { final A newKey = keyFunction.f(key); final B newValue = valueFunction.f(get(key).some()); hashMap.set(newKey, newValue); } return hashMap; }
static <T extends Node> IfContext iF(Object n, Class<T> clazz, F<T> cb) { if (clazz.isInstance(n)) { cb.f(clazz.cast(n)); return IsTrue; } return IsFalse; }
void THROWS(Class<? extends Throwable> k, F... fs) { for (F f : fs) try { f.f(); fail("Expected " + k.getName() + " not thrown"); } catch (Throwable t) { if (k.isAssignableFrom(t.getClass())) pass(); else unexpected(t); } }
/** * Returns a parser that fails with the given error if the result value does not meet the given * predicate. * * @param f The predicate to filter on. * @param e The error to in the event that the predicate is not met. * @return A parser that fails with the given error if the result value does not meet the given * predicate. */ public Parser<I, A, E> filter(final F<A, Boolean> f, final E e) { return parser( i -> parse(i) .bind( r -> { final A v = r.value(); return f.f(v) ? Validation.<E, Result<I, A>>success(result(r.rest(), v)) : Validation.<E, Result<I, A>>fail(e); })); }
public Object t(Object obj) throws Exception { Object[] o = (Object[]) obj; if (o.length != 2) throw new Exception("Wrong data number: " + o.length); Object[] input = (Object[]) o[0]; F filter = (F) o[1]; int nb = input.length; int count = 0; for (int i = 0; i < nb; i++) { Object element = input[i]; if (filter.f(element)) count++; } return new Integer(count); }
public <T> T match(F<? super Empty, T> a, F<? super Leaf, T> b, F<? super Node, T> c) { return a.f(this); }
/** * Folds this tree using the given monoid. * * @param f A transformation from this tree's elements, to the monoid. * @param m The monoid to fold this tree with. * @return The result of folding the tree with the given monoid. */ public <B> B foldMap(final F<A, B> f, final Monoid<B> m) { return m.sum(f.f(root()), m.sumRight(subForest()._1().map(foldMap_(f, m)).toList())); }
/** * Maps the given function over this tree. * * @param f The function to map over this tree. * @return The new Tree after the function has been applied to each element in this Tree. */ public <B> Tree<B> fmap(final F<A, B> f) { return node( f.f(root()), subForest().map(Stream.<Tree<A>, Tree<B>>map_().f(Tree.<A, B>fmap_().f(f)))); }
/** * Maps the parse input type through an invariant functor. * * @param f The function to covariant map. * @param g The function to contra-variant map. * @return A parser with the new input type. */ public <Z> Parser<Z, A, E> xmap(final F<I, Z> f, final F<Z, I> g) { return parser(z -> parse(g.f(z)).map(r -> r.mapRest(f))); }
/** * Maps the given function across this list. * * @param f The function to map across this list. * @return A new list after the given function has been applied to each element. */ public <B> NonEmptyList<B> map(final F<A, B> f) { return nel(f.f(head), tail.map(f)); }
public LazyString bind(F<Character, LazyString> f) { return fromStream(s.bind(c -> f.f(c).toStream())); }