/** * Returns a constant function that always uses this value. * * @return A constant function that always uses this value. */ public final <B> F<B, A> constant() { return Function.constant(_1()); }
/** * Performs a bind across each stream element, but ignores the element value each time. * * @param bs The stream to apply in the final join. * @return A new stream after the final join. */ public final <B> Stream<B> sequence(final Stream<B> bs) { final F<A, Stream<B>> c = constant(bs); return bind(c); }
/** * Returns the size of this set. * * @return The number of elements in this set. */ public final int size() { final F<A, Integer> one = constant(1); return foldMap(one, Monoid.intAdditionMonoid); }
/** set polymorphically the target of a {@link PPrism} with a value */ public final F<S, T> set(final B b) { return modify(Function.constant(b)); }
/** * Anonymous bind through this projection. * * @param e The value to bind with. * @return An either after binding through this projection. */ public <X> Either<X, B> sequence(final Either<X, B> e) { return bind(Function.constant(e)); }
/** * Anonymous bind through this projection. * * @param e The value to bind with. * @return An either after binding through this projection. */ public <X> Either<A, X> sequence(final Either<A, X> e) { return bind(Function.constant(e)); }
/** * Performs a bind across the optional value, but ignores the element value in the function. * * @param o The optional value to apply in the final join. * @return A new optional value after the final join. */ public final <B> Option<B> sequence(final Option<B> o) { final F<A, Option<B>> c = constant(o); return bind(c); }
/** * Performs a bind across each list element, but ignores the element value each time. * * @param bs The list to apply in the final join. * @return A new list after the final join. */ public final <B> List<B> sequence(final List<B> bs) { final F<A, List<B>> c = constant(bs); return bind(c); }
/** * Performs a bind across each array element, but ignores the element value each time. * * @param bs The array to apply in the final join. * @return A new array after the final join. */ public <B> Array<B> sequence(final Array<B> bs) { final F<A, Array<B>> c = constant(bs); return bind(c); }
/** * Anonymous bind through this validation. * * @param v The value to bind with. * @return A validation after binding. */ public final <A> Validation<E, A> sequence(final Validation<E, A> v) { return bind(Function.constant(v)); }
/** * Replaces the first element of this product with the given value. * * @param c The value with which to replace the first element of this product. * @return A new product with the first element replaced with the given value. */ public final <C> P2<C, B> inject(final C c) { final F<P2<A, B>, C> co = constant(c); return cobind(co); }
/** * Returns the length of this zipper. * * @return the length of this zipper. */ public int length() { return foldRight(Function.constant(Integers.add.f(1)), 0); }
/** * modify polymorphically the target of a {@link POptional} with a function. return empty if the {@link POptional} is not * matching */ public final F<S, Option<T>> modifyOption(final F<A, B> f) { return s -> getOption(s).map(Function.constant(modify(f).f(s))); }
/** * Turn a list of functions into a function returning a list. * * @param fs The list of functions to sequence into a single function that returns a list. * @return A function that, when given an argument, applies all the functions in the given list to it * and returns a list of the results. */ public static <A, B> F<B, List<A>> sequence_(final List<F<B, A>> fs) { return fs.foldRight(Function.lift(List.cons()), Function .constant(List.nil())); }
/** modify polymorphically the target of a {@link PPrism} with an Applicative function */ public final <C> F<S, F<C, T>> modifyFunctionF(final F<A, F<C, B>> f) { return s -> getOrModify(s).either( Function.constant(), a -> Function.compose(this::reverseGet, f.f(a)) ); }
public final <C, B> F<C, List<B>> traverseF(F<A, F<C, B>> f) { return this.foldRight( (a, acc) -> Function.bind(acc, (bs) -> Function.compose(bs::cons, f.f(a))), constant(List.nil()) ); }
/** * Nil prism */ public static <A> Prism<List<A>, Unit> nil() { return prism((List<A> l) -> l.isEmpty() ? some(unit()) : none(), constant(List.nil())); }
@Override public <C> F<S, F<C, T>> modifyFunctionF(final F<A, F<C, B>> f) { return s -> getOrModify.f(s).either( Function.constant(), a -> Function.compose(b -> set.f(b).f(s), f.f(a)) ); }
/** * Turn a stream of functions into a function returning a stream. * * @param fs The stream of functions to sequence into a single function that returns a stream. * @return A function that, when given an argument, applies all the functions in the given stream to it * and returns a stream of the results. */ public static <A, B> F<B, Stream<A>> sequence_(final Stream<F<B, A>> fs) { return fs.foldRight((baf, p1) -> Function.bind(baf, p1._1(), curry((a, stream) -> cons(a, p(stream)))), Function .constant(Stream.nil())); }
@Override public FingerTree<V, A> append(final FingerTree<V, A> t) { final Measured<V, A> m = measured(); return t.match( constant(this), single -> snoc(single.value()), deep -> new Deep<>(m, m.sum(measure(), deep.measure()), prefix, addDigits0(m, middle, suffix, deep.prefix, deep.middle), deep.suffix)); }