Skip to content

Instantly share code, notes, and snippets.

@clinuxrulz
Last active August 29, 2015 14:24
Show Gist options
  • Save clinuxrulz/1af9c06b30fa45d3ef59 to your computer and use it in GitHub Desktop.
Save clinuxrulz/1af9c06b30fa45d3ef59 to your computer and use it in GitHub Desktop.
Incremental Arrow Transformer
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clinuxrulz.gmail.rxtest;
import clinuxrulz.gmail.rxtest.inc_arrow.IncArrowArrow;
import clinuxrulz.gmail.rxtest.inc_arrow.IncArrowStatefulArrowTransformer;
import clinuxrulz.gmail.rxtest.inc_arrow.IncArrowCategory;
import clinuxrulz.gmail.rxtest.inc_arrow.IncArrowSemigroupoid;
import clinuxrulz.gmail.rxtest.inc_arrow.IncArrowStatelessArrowTransformer;
import org.highj._;
import org.highj.__;
import org.highj.___;
import org.highj.data.tuple.T2;
import org.highj.typeclass2.arrow.Arrow;
import org.highj.typeclass2.arrow.ArrowChoice;
/**
*
* @author clintonselke
*/
public class IncArrow<A,B,C> implements ___<IncArrow.µ,A,B,C> {
public static class µ {}
private final __<A,T2<B,Boolean>,T2<T2<C,Boolean>,IncArrow<A,B,C>>> _run;
private IncArrow(__<A,T2<B,Boolean>,T2<T2<C,Boolean>,IncArrow<A,B,C>>> run) {
this._run = run;
}
public static <A,B,C> IncArrow<A,B,C> incArrow(__<A,T2<B,Boolean>,T2<T2<C,Boolean>,IncArrow<A,B,C>>> run) {
return new IncArrow<>(run);
}
public static <A,B,C> IncArrow<A,B,C> narrow(___<IncArrow.µ,A,B,C> a) {
return (IncArrow<A,B,C>)a;
}
public static <A,B,C> IncArrow<A,B,C> narrow(__<___.µ<IncArrow.µ,A>,B,C> a) {
return (IncArrow<A,B,C>)a;
}
public static <A,B,C> IncArrow<A,B,C> narrow(_<__.µ<___.µ<IncArrow.µ,A>,B>,C> a) {
return (IncArrow<A,B,C>)a;
}
public __<A,T2<B,Boolean>,T2<T2<C,Boolean>,IncArrow<A,B,C>>> run() {
return _run;
}
public static <A> IncArrowSemigroupoid<A> semigroupoid(Arrow<A> a) {
return () -> a;
}
public static <A> IncArrowCategory<A> category(Arrow<A> a) {
return () -> a;
}
public static <A> IncArrowArrow<A> arrow(Arrow<A> a) {
return () -> a;
}
public static <A> IncArrowStatelessArrowTransformer<A> statelessArrowTransformer(ArrowChoice<A> a) {
return () -> a;
}
public static <A> IncArrowStatefulArrowTransformer<A> statefulArrowTransformer(Arrow<A> a) {
return () -> a;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clinuxrulz.gmail.rxtest.inc_arrow;
import clinuxrulz.gmail.rxtest.IncArrow;
import java.util.function.Function;
import org.highj.__;
import org.highj.___;
import org.highj.data.tuple.T2;
import org.highj.typeclass2.arrow.Arrow;
/**
*
* @author clintonselke
*/
public interface IncArrowArrow<A> extends IncArrowCategory<A>, Arrow<___.µ<IncArrow.µ,A>> {
@Override
public default <B, C> IncArrow<A, B, C> arr(Function<B, C> fn) {
class Util {
IncArrow<A,B,C> arr2(C last) {
return IncArrow.incArrow(a().arr((T2<B,Boolean> x) -> {
if (x._2()) {
C x2 = fn.apply(x._1());
return T2.of(T2.of(x2, true), arr2(x2));
} else {
return T2.of(T2.of(last, false), arr2(last));
}
}));
}
}
final Util util = new Util();
return IncArrow.incArrow(a().arr((T2<B,Boolean> x) -> {
C x2 = fn.apply(x._1());
return T2.of(T2.of(x2, true), util.arr2(x2));
}));
}
@Override
public default <B, C, D> IncArrow<A, T2<B, D>, T2<C, D>> first(__<___.µ<IncArrow.µ, A>, B, C> arrow) {
return IncArrow.incArrow(a().dot(
a().arr((T2<T2<T2<C,Boolean>,IncArrow<A,B,C>>,T2<D,Boolean>> x) -> T2.of(T2.of(T2.of(x._1()._1()._1(), x._2()._1()), x._1()._1()._2() || x._2()._2()), first(x._1()._2()))),
a().dot(
a().first(IncArrow.narrow(arrow).run()),
a().fanout(
a().arr((T2<T2<B,D>,Boolean> x) -> T2.of(x._1()._1(), x._2())),
a().arr((T2<T2<B,D>,Boolean> x) -> T2.of(x._1()._2(), x._2()))
)
)
));
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clinuxrulz.gmail.rxtest.inc_arrow;
import clinuxrulz.gmail.rxtest.IncArrow;
import org.highj.___;
import org.highj.data.tuple.T2;
import org.highj.typeclass2.arrow.Category;
/**
*
* @author clintonselke
*/
public interface IncArrowCategory<A> extends IncArrowSemigroupoid<A>, Category<___.µ<IncArrow.µ,A>> {
@Override
public default <B> IncArrow<A, B, B> identity() {
return IncArrow.incArrow(a().arr((T2<B,Boolean> x) -> T2.of(x, identity())));
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clinuxrulz.gmail.rxtest.inc_arrow;
import clinuxrulz.gmail.rxtest.IncArrow;
import org.highj.__;
import org.highj.___;
import org.highj.data.tuple.T2;
import org.highj.typeclass2.arrow.Arrow;
import org.highj.typeclass2.arrow.Semigroupoid;
/**
*
* @author clintonselke
*/
public interface IncArrowSemigroupoid<A> extends Semigroupoid<___.µ<IncArrow.µ,A>> {
public Arrow<A> a();
@Override
public default <B, C, D> IncArrow<A, B, D> dot(__<___.µ<IncArrow.µ, A>, C, D> cd, __<___.µ<IncArrow.µ, A>, B, C> bc) {
return IncArrow.incArrow(a().dot(
a().arr((T2<T2<T2<D,Boolean>,IncArrow<A,C,D>>,IncArrow<A,B,C>> x) -> T2.of(
T2.of(x._1()._1()._1(), x._1()._1()._2()),
dot(x._1()._2(), x._2())
)),
a().dot(
a().first(IncArrow.narrow(cd).run()),
IncArrow.narrow(bc).run()
)
));
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clinuxrulz.gmail.rxtest.inc_arrow;
import clinuxrulz.gmail.rxtest.IncArrow;
import org.highj.__;
import org.highj.data.tuple.T2;
import org.highj.typeclass2.arrow.ArrowTransformer;
/**
*
* @author clintonselke
*/
public interface IncArrowStatefulArrowTransformer<A> extends IncArrowArrow<A>, ArrowTransformer<IncArrow.µ,A> {
@Override
public default <B, C> IncArrow<A, B, C> lift(__<A, B, C> arrow) {
return IncArrow.incArrow(a().dot(
a().arr((C x) -> T2.of(T2.of(x, true), lift(arrow))),
a().dot(
arrow,
a().arr(T2::_1)
)
));
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clinuxrulz.gmail.rxtest.inc_arrow;
import clinuxrulz.gmail.rxtest.IncArrow;
import org.highj.__;
import org.highj.data.collection.Either;
import org.highj.data.tuple.T2;
import org.highj.typeclass2.arrow.ArrowChoice;
import org.highj.typeclass2.arrow.ArrowTransformer;
/**
*
* @author clintonselke
*/
public interface IncArrowStatelessArrowTransformer<A> extends IncArrowArrow<A>, ArrowTransformer<IncArrow.µ,A> {
@Override
public ArrowChoice<A> a();
@Override
public default <B, C> IncArrow<A, B, C> lift(__<A, B, C> arrow) {
class Util {
IncArrow<A,B,C> lift2(C last) {
return IncArrow.incArrow(a().dot(
a().fanin(
a().arr((B x) -> T2.of(T2.of(last, false), lift2(last))),
a().dot(
a().arr((C x) -> T2.of(T2.of(x, true), lift2(x))),
arrow
)
),
a().arr((T2<B,Boolean> x) -> {
if (x._2()) {
return Either.newRight(x._1());
} else {
return Either.newLeft(x._1());
}
})
));
}
}
final Util util = new Util();
return IncArrow.incArrow(a().dot(
a().arr((C x) -> T2.of(T2.of(x, true), util.lift2(x))),
a().dot(
arrow,
a().arr((T2<B,Boolean> x) -> x._1())
)
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment