/** * Returns the first element of this vector. * * @return the first element of this vector. */ public A _1() { return head._1(); }
/** * Promote any function to a transformation between P1s. * * @param f A function to promote to a transformation between P1s. * @return A function promoted to operate on P1s. */ public static <A, B> F<P1<A>, P1<B>> map_(final F<A, B> f) { return a -> a.map(f); }
/** * Performs function application within a P1 (applicative functor pattern). * * @param cf The P1 function to apply. * @return A new P1 after applying the given P1 function to the first argument. */ public final <B> P1<B> apply(final P1<F<A, B>> cf) { P1<A> self = this; return cf.bind(f -> map_(f).f(self)); }
/** * Binds the given function to the values in the given P1s with a final join. * * @param cb A given P1 to bind the given function with. * @param f The function to apply to the values in the given P1s. * @return A new P1 after performing the map, then final join. */ public final <B, C> P1<C> bind(final P1<B> cb, final F<A, F<B, C>> f) { return cb.apply(map_(f).f(this)); }
public String toString() { F<String, String> leftPad10 = curry( UnixFsObject.leftPad, 10 ); F<String, String> rightPad10 = curry( UnixFsObject.rightPad, 10 ); // I wonder how long this will hold. Perhaps it should only be possible to call toString() on valid // objects - trygve FileAttributes attributes = this.attributes.some(); return prefixChar + attributes.mode.map( UnixFileMode.showLong ).orSome( "<unknown>" ) + " " + attributes.user.map( leftPad10 ).orSome( " <unknown>" ) + " " + attributes.group.map( leftPad10 ).orSome( " <unknown>" ) + " " + p( String.valueOf( size ) ).map( rightPad10 )._1() + " " + p( lastModified ).map( formatter )._1() + " " + path.string; }
@BeforeClass public static void setupSuite() { DB.transact(new EffectOp("CREATE TABLE BAR (BAZ VARCHAR(100))")).f(); }
/** * An order instance for a product-1. * * @param oa Order across the produced type. * @return An order instance for a product-1. */ public static <A> Ord<P1<A>> p1Ord(final Ord<A> oa) { return oa.contramap(P1.__1()); }
/** * Performs function application within a vector (applicative functor pattern). * * @param vf The vector of functions to apply. * @return A new vector after zipping the given vector of functions over this vector. */ public <B> V4<B> apply(final V4<F<A, B>> vf) { return new V4<>(head.apply(vf.head()), tail.apply(vf.tail())); }
/** * Joins a P1 of a P1 with a bind operation. * * @param a The P1 of a P1 to join. * @return A new P1 that is the join of the given P1. */ public static <A> P1<A> join(final P1<P1<A>> a) { return a.bind(Function.identity()); }
@Override public F<Input<E>, IterV<E, Integer>> f(final Integer acc) { final F0<IterV<E, Integer>> empty = () -> cont(step.f(acc)); final F0<F<E, IterV<E, Integer>>> el = () -> P.p(cont(step.f(acc + 1))).constant(); final F0<IterV<E, Integer>> eof = () -> done(acc, Input.<E>eof()); return s -> s.apply(empty, el, eof); } };
/** * Combines two trampolines so they run cooperatively. The results are combined with the given function. * * @param b Another trampoline to combine with this trampoline. * @param f A function to combine the results of the two trampolines. * @return A new trampoline that runs this trampoline and the given trampoline simultaneously. */ @SuppressWarnings("LoopStatementThatDoesntLoop") public final <B, C> Trampoline<C> zipWith(final Trampoline<B> b, final F2<A, B, C> f) { final Either<P1<Trampoline<A>>, A> ea = resume(); final Either<P1<Trampoline<B>>, B> eb = b.resume(); for (final P1<Trampoline<A>> x : ea.left()) { for (final P1<Trampoline<B>> y : eb.left()) { return suspend(x.bind(y, F2Functions.curry((ta, tb) -> suspend(() -> ta.zipWith(tb, f))))); } for (final B y : eb.right()) { return suspend(x.map(ta -> ta.map(F2Functions.f(F2Functions.flip(f), y)))); } } for (final A x : ea.right()) { for (final B y : eb.right()) { return suspend(() -> pure(f.f(x, y))); } for (final P1<Trampoline<B>> y : eb.left()) { return suspend(y.map(liftM2(F2Functions.curry(f)).f(pure(x)))); } } throw Bottom.error("Match error: Trampoline is neither done nor suspended."); } }
@Test public void testIt() { assertThat(DB.submit(insert("x")).f(), is(success(1))); assertThat(DB.submit(selectAll()).f(), is(success(single("x")))); SQLException ex = new SQLException("failed i have"); assertThat( DB.transact(new DB<Unit>() { @Override public Unit run(Connection c) throws SQLException { insert("a").run(c); insert("b").run(c); throw ex; } }).f(), is(fail(ex)) ); assertThat(DB.submit(selectAll()).f(), is(success(single("x")))); SQLException noConn = new SQLException("no connection"); ValidationDbInterpreter noConnection = new ValidationDbInterpreter(() -> { throw noConn; }); assertThat(noConnection.transact(insert("alabala")).f(), is(fail(noConn))); assertThat(DB.submit(selectAll()).f(), is(success(single("x")))); assertThat( DB.transact(insert("y").bind(ignore -> insert("z")).map(ignore2 -> Unit.unit())).f(), is(success(Unit.unit())) ); assertThat(DB.submit(selectAll()).f(), is(success(arrayList("x", "y", "z")))); }
/** * A hash instance for a product-1. * * @param ha A hash for the first element of the product. * @return A hash instance for a product-1. */ public static <A> Hash<P1<A>> p1Hash(final Hash<A> ha) { return ha.contramap(P1.__1()); }
/** * Performs function application within a vector (applicative functor pattern). * * @param vf The vector of functions to apply. * @return A new vector after zipping the given vector of functions over this vector. */ public <B> V6<B> apply(final V6<F<A, B>> vf) { return new V6<>(head.apply(vf.head()), tail.apply(vf.tail())); }
/** * Promotes a function of arity-2 to a function on P1s. * * @param f The function to promote. * @return A function of arity-2 promoted to map over P1s. */ public static <A, B, C> F<P1<A>, F<P1<B>, P1<C>>> liftM2(final F<A, F<B, C>> f) { return Function.curry((pa, pb) -> pa.bind(pb, f)); }
/** An iteratee that skips the first n elements of the input */ public static <E> IterV<E, Unit> drop(final int n) { final F<Input<E>, IterV<E, Unit>> step = new F<Input<E>, IterV<E, Unit>>() { final F<Input<E>, IterV<E, Unit>> step = this; final F0<IterV<E, Unit>> empty = () -> cont(step); final F0<F<E, IterV<E, Unit>>> el = () -> P.p(IterV.<E>drop(n - 1)).constant(); final F0<IterV<E, Unit>> eof = () -> done(Unit.unit(), Input.<E>eof()); @Override public IterV<E, Unit> f(final Input<E> s) { return s.apply(empty, el, eof); } }; return n == 0 ? done(Unit.unit(), Input.empty()) : cont(step); }