Created
July 21, 2016 23:24
-
-
Save clinuxrulz/b20a2281a02c356360c7da66debaa61e to your computer and use it in GitHub Desktop.
Reference implementation for typeable
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sm.fp.data.typeable; | |
import com.sm.fp.DataSinkEffects2; | |
import com.sm.fp.DataSourceEffects2; | |
import com.sm.fp.LoadSave3; | |
import com.sm.fp.data.eq.type.Leibniz; | |
import com.sm.fp.kinds.__; | |
import fj.*; | |
import fj.data.Either; | |
import fj.data.List; | |
import fj.data.Option; | |
import org.derive4j.Data; | |
import org.derive4j.Derive; | |
import org.derive4j.Flavour; | |
import org.derive4j.Visibility; | |
@Data(flavour = Flavour.FJ) | |
public abstract class CoproductTypeable<A> { | |
public interface Cases<R,A> { | |
R Singleton(String tag, Typeable<A> typeable); | |
R Append(AppendData<?,?,A> appendData); | |
R Invmap(InvmapData<?,A> invmapData); | |
} | |
public abstract <R> R match(Cases<R,A> cases); | |
public static class AppendData<A,B,C> { | |
private final CoproductTypeable<A> _coproductTypeable1; | |
private final CoproductTypeable<B> _coproductTypeable2; | |
private final Leibniz<Either<A,B>,C> _leibniz; | |
private AppendData(CoproductTypeable<A> coproductTypeable1, CoproductTypeable<B> coproductTypeable2, Leibniz<Either<A,B>,C> leibniz) { | |
this._coproductTypeable1 = coproductTypeable1; | |
this._coproductTypeable2 = coproductTypeable2; | |
this._leibniz = leibniz; | |
} | |
public static <A,B> AppendData<A,B,Either<A,B>> create(CoproductTypeable<A> coproductTypeable1, CoproductTypeable<B> coproductTypeable2) { | |
return new AppendData<>(coproductTypeable1, coproductTypeable2, Leibniz.refl()); | |
} | |
public CoproductTypeable<A> coproductTypeable1() { | |
return _coproductTypeable1; | |
} | |
public CoproductTypeable<B> coproductTypeable2() { | |
return _coproductTypeable2; | |
} | |
public Leibniz<Either<A, B>, C> leibniz() { | |
return _leibniz; | |
} | |
public <E,M> __<M,Option<C>> loadOp(DataSourceEffects2<E,M> eff) { | |
return eff.monad.bind( | |
coproductTypeable1().loadOp(eff), | |
(Option<A> aOp) -> | |
aOp.<__<M,Option<C>>>option( | |
() -> | |
eff.monad.bind( | |
coproductTypeable2().loadOp(eff), | |
(Option<B> bOp) -> | |
bOp.<__<M,Option<C>>>option( | |
() -> eff.monad.pure(Option.<C>none()), | |
(B b) -> eff.monad.pure(Option.some(leibniz().coerce(Either.<A,B>right(b)))) | |
) | |
), | |
(A a) -> eff.monad.pure(Option.some(leibniz().coerce(Either.<A,B>left(a)))) | |
) | |
); | |
} | |
public <E,M> __<M,Unit> save(DataSinkEffects2<E,M> eff, C c) { | |
return leibniz().symm().coerce(c).either( | |
(A a) -> coproductTypeable1().save(eff, a), | |
(B b) -> coproductTypeable2().save(eff, b) | |
); | |
} | |
public Equal<C> makeEqual() { | |
return Equal.eitherEqual( | |
coproductTypeable1().makeEqual(), | |
coproductTypeable2().makeEqual() | |
).contramap((C x) -> leibniz().symm().coerce(x)); | |
} | |
public Ord<C> makeOrd() { | |
return Ord.eitherOrd( | |
coproductTypeable1().makeOrd(), | |
coproductTypeable2().makeOrd() | |
).contramap((C x) -> leibniz().symm().coerce(x)); | |
} | |
} | |
public static class InvmapData<A,B> { | |
private final CoproductTypeable<A> _coproductTypeable; | |
private final F<A,B> _f; | |
private final F<B,A> _g; | |
private InvmapData(CoproductTypeable<A> coproductTypeable, F<A,B> f, F<B,A> g) { | |
this._coproductTypeable = coproductTypeable; | |
this._f = f; | |
this._g = g; | |
} | |
public static <A,B> InvmapData<A,B> create(CoproductTypeable<A> coproductTypeable, F<A,B> f, F<B,A> g) { | |
return new InvmapData<>(coproductTypeable, f, g); | |
} | |
public CoproductTypeable<A> coproductTypeable() { | |
return _coproductTypeable; | |
} | |
public F<A, B> f() { | |
return _f; | |
} | |
public F<B, A> g() { | |
return _g; | |
} | |
public LoadSave3<B> makeLoadSave() { | |
return new LoadSave3<B>() { | |
@Override | |
public <E, M> __<M, B> load(DataSourceEffects2<E, M> eff) { | |
return eff.functor.fmap(f(), coproductTypeable().makeLoadSave().load(eff)); | |
} | |
@Override | |
public <E, M> __<M, Unit> save(DataSinkEffects2<E, M> eff, B b) { | |
return coproductTypeable().makeLoadSave().save(eff, g().f(b)); | |
} | |
}; | |
} | |
public Equal<B> makeEqual() { | |
return coproductTypeable().makeEqual().contramap(g()); | |
} | |
public Ord<B> makeOrd() { | |
return coproductTypeable().makeOrd().contramap(g()); | |
} | |
} | |
public static <A> CoproductTypeable<A> singleton(String tag, Typeable<A> typeable) { | |
return CoproductTypeables.Singleton(tag, typeable); | |
} | |
public static <A,B> CoproductTypeable<Either<A,B>> append(CoproductTypeable<A> ta, CoproductTypeable<B> tb) { | |
return CoproductTypeables.Append(AppendData.create(ta, tb)); | |
} | |
public <B> CoproductTypeable<B> invmap(F<A,B> f, F<B,A> g) { | |
return CoproductTypeables.Invmap(InvmapData.create(this, f, g)); | |
} | |
public <B> CoproductTypeable<Either<A,B>> append(CoproductTypeable<B> tb) { | |
return CoproductTypeable.append(this, tb); | |
} | |
public static <A,B> CoproductTypeable<Either<A,B>> either( | |
CoproductTypeable<A> t1, | |
CoproductTypeable<B> t2 | |
) { | |
return t1.append(t2); | |
} | |
public LoadSave3<A> makeLoadSave() { | |
return new LoadSave3<A>() { | |
@Override | |
public <E, M> __<M, A> load(DataSourceEffects2<E, M> eff) { | |
return eff.monad.bind( | |
CoproductTypeable.this.loadOp(eff), | |
(Option<A> aOp) -> | |
aOp.<__<M,A>>option( | |
() -> eff.monadError.<A>throwError(eff.error.strMsg("Invalid type.")), | |
(A a) -> eff.monad.pure(a) | |
) | |
); | |
} | |
@Override | |
public <E, M> __<M, Unit> save(DataSinkEffects2<E, M> eff, A a) { | |
return CoproductTypeable.this.save(eff, a); | |
} | |
}; | |
} | |
private <E,M> __<M,Option<A>> loadOp(DataSourceEffects2<E,M> eff) { | |
return CoproductTypeables | |
.<A>cases() | |
.Singleton((String tag, Typeable<A> typeable) -> | |
eff.monad.bind( | |
eff.getStringFieldM("type"), | |
(String type) -> { | |
if (type.equals(tag)) { | |
return eff.functor.fmap(Option.<A>some_(), typeable.makeLoadSave().mapField("value").load(eff)); | |
} else { | |
return eff.monad.pure(Option.none()); | |
} | |
} | |
) | |
) | |
.Append((AppendData<?,?,A> appendData) -> appendData.loadOp(eff)) | |
.Invmap((InvmapData<?,A> invmapData) -> eff.functor.fmap(Option.<A>some_(), invmapData.makeLoadSave().load(eff))) | |
.f(this); | |
} | |
private <E,M> __<M,Unit> save(DataSinkEffects2<E,M> eff, A a) { | |
return CoproductTypeables | |
.<A>cases() | |
.Singleton((String tag, Typeable<A> typeable) -> | |
eff.applicative.seq_(List.list( | |
eff.setStringFieldM("type", tag), | |
eff.setFieldM("value", typeable.makeLoadSave().save(eff, a)) | |
)) | |
) | |
.Append((AppendData<?,?,A> appendData) -> appendData.save(eff, a)) | |
.Invmap((InvmapData<?,A> invmapData) -> invmapData.makeLoadSave().save(eff, a)) | |
.f(this); | |
} | |
public Equal<A> makeEqual() { | |
return CoproductTypeables | |
.<A>cases() | |
.Singleton((String tag, Typeable<A> typeable) -> typeable.makeEqual()) | |
.Append(AppendData::makeEqual) | |
.Invmap(InvmapData::makeEqual) | |
.f(this); | |
} | |
public Ord<A> makeOrd() { | |
return CoproductTypeables | |
.<A>cases() | |
.Singleton((String tag, Typeable<A> typeable) -> typeable.makeOrd()) | |
.Append(AppendData::makeOrd) | |
.Invmap(InvmapData::makeOrd) | |
.f(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sm.fp.data.typeable; | |
import com.sm.fp.DataSinkEffects2; | |
import com.sm.fp.DataSourceEffects2; | |
import com.sm.fp.LoadSave3; | |
import com.sm.fp.data.eq.type.Leibniz; | |
import com.sm.fp.kinds.__; | |
import fj.*; | |
import org.derive4j.Data; | |
import org.derive4j.Derive; | |
import org.derive4j.Flavour; | |
import org.derive4j.Visibility; | |
@Data(flavour = Flavour.FJ) | |
public abstract class ProductTypeable<A> { | |
public interface Cases<R,A> { | |
R Singleton(String tag, Typeable<A> typeable); | |
R Append(AppendData<?,?,A> appendData); | |
R Invmap(InvmapData<?,A> invmapData); | |
} | |
public abstract <R> R match(Cases<R,A> cases); | |
public static class AppendData<A,B,C> { | |
private final ProductTypeable<A> _productTypeable1; | |
private final ProductTypeable<B> _productTypeable2; | |
private final Leibniz<P2<A,B>,C> _leibniz; | |
private AppendData(ProductTypeable<A> productTypeable1, ProductTypeable<B> productTypeable2, Leibniz<P2<A,B>,C> leibniz) { | |
this._productTypeable1 = productTypeable1; | |
this._productTypeable2 = productTypeable2; | |
this._leibniz = leibniz; | |
} | |
public static <A,B> AppendData<A,B,P2<A,B>> create(ProductTypeable<A> productTypeable1, ProductTypeable<B> productTypeable2) { | |
return new AppendData<>(productTypeable1, productTypeable2, Leibniz.refl()); | |
} | |
public ProductTypeable<A> productTypeable1() { | |
return _productTypeable1; | |
} | |
public ProductTypeable<B> productTypeable2() { | |
return _productTypeable2; | |
} | |
public Leibniz<P2<A, B>, C> leibniz() { | |
return _leibniz; | |
} | |
public LoadSave3<C> makeLoadSave() { | |
return new LoadSave3<C>() { | |
@Override | |
public <E, M> __<M, C> load(DataSourceEffects2<E, M> eff) { | |
return eff.apply.apply2( | |
(A a, B b) -> leibniz().coerce(P.p(a, b)), | |
productTypeable1().makeLoadSave().load(eff), | |
productTypeable2().makeLoadSave().load(eff) | |
); | |
} | |
@Override | |
public <E, M> __<M, Unit> save(DataSinkEffects2<E, M> eff, C c) { | |
P2<A,B> x = leibniz().symm().coerce(c); | |
return eff.apply.seqRight( | |
productTypeable1().makeLoadSave().save(eff, x._1()), | |
productTypeable2().makeLoadSave().save(eff, x._2()) | |
); | |
} | |
}; | |
} | |
public Equal<C> makeEqual() { | |
return Equal.p2Equal( | |
productTypeable1().makeEqual(), | |
productTypeable2().makeEqual() | |
).contramap((C x) -> leibniz().symm().coerce(x)); | |
} | |
public Ord<C> makeOrd() { | |
return Ord.p2Ord( | |
productTypeable1().makeOrd(), | |
productTypeable2().makeOrd() | |
).contramap((C x) -> leibniz().symm().coerce(x)); | |
} | |
} | |
public static class InvmapData<A,B> { | |
private final ProductTypeable<A> _productTypeable; | |
private final F<A,B> _f; | |
private final F<B,A> _g; | |
private InvmapData(ProductTypeable<A> productTypeable, F<A,B> f, F<B,A> g) { | |
this._productTypeable = productTypeable; | |
this._f = f; | |
this._g = g; | |
} | |
public static <A,B> InvmapData<A,B> create(ProductTypeable<A> productTypeable, F<A,B> f, F<B,A> g) { | |
return new InvmapData<>(productTypeable, f, g); | |
} | |
public ProductTypeable<A> productTypeable() { | |
return _productTypeable; | |
} | |
public F<A, B> f() { | |
return _f; | |
} | |
public F<B, A> g() { | |
return _g; | |
} | |
public LoadSave3<B> makeLoadSave() { | |
return new LoadSave3<B>() { | |
@Override | |
public <E, M> __<M, B> load(DataSourceEffects2<E, M> eff) { | |
return eff.functor.fmap(f(), productTypeable().makeLoadSave().load(eff)); | |
} | |
@Override | |
public <E, M> __<M, Unit> save(DataSinkEffects2<E, M> eff, B b) { | |
return productTypeable().makeLoadSave().save(eff, g().f(b)); | |
} | |
}; | |
} | |
public Equal<B> makeEqual() { | |
return productTypeable().makeEqual().contramap(g()); | |
} | |
public Ord<B> makeOrd() { | |
return productTypeable().makeOrd().contramap(g()); | |
} | |
} | |
public static <A> ProductTypeable<A> singleton(String tag, Typeable<A> typeable) { | |
return ProductTypeables.Singleton(tag, typeable); | |
} | |
public static <A,B> ProductTypeable<P2<A,B>> append(ProductTypeable<A> ta, ProductTypeable<B> tb) { | |
return ProductTypeables.Append(AppendData.create(ta, tb)); | |
} | |
public <B> ProductTypeable<B> invmap(F<A,B> f, F<B,A> g) { | |
return ProductTypeables.Invmap(InvmapData.create(this, f, g)); | |
} | |
public <B> ProductTypeable<P2<A,B>> append(ProductTypeable<B> tb) { | |
return append(this, tb); | |
} | |
public static <A,B> ProductTypeable<P2<A,B>> p2( | |
ProductTypeable<A> t1, | |
ProductTypeable<B> t2 | |
) { | |
return t1.append(t2); | |
} | |
public static <A,B,C> ProductTypeable<P3<A,B,C>> p3( | |
ProductTypeable<A> t1, | |
ProductTypeable<B> t2, | |
ProductTypeable<C> t3 | |
) { | |
return t1.append(p2(t2,t3)).invmap( | |
(P2<A, P2<B, C>> x) -> P.p(x._1(), x._2()._1(), x._2()._2()), | |
(P3<A, B, C> x) -> P.p(x._1(), P.p(x._2(), x._3())) | |
); | |
} | |
public static <A,B,C,D> ProductTypeable<P4<A,B,C,D>> p4( | |
ProductTypeable<A> t1, | |
ProductTypeable<B> t2, | |
ProductTypeable<C> t3, | |
ProductTypeable<D> t4 | |
) { | |
return p2(t1,t2).append(p2(t3,t4)).invmap( | |
(P2<P2<A, B>, P2<C, D>> x) -> P.p(x._1()._1(), x._1()._2(), x._2()._1(), x._2()._2()), | |
(P4<A, B, C, D> x) -> P.p(P.p(x._1(), x._2()), P.p(x._3(), x._4())) | |
); | |
} | |
public static <A,B,C,D,E> ProductTypeable<P5<A,B,C,D,E>> p5( | |
ProductTypeable<A> t1, | |
ProductTypeable<B> t2, | |
ProductTypeable<C> t3, | |
ProductTypeable<D> t4, | |
ProductTypeable<E> t5 | |
) { | |
return p2(t1,t2).append(p3(t3,t4,t5)).invmap( | |
(P2<P2<A,B>,P3<C,D,E>> x) -> P.p(x._1()._1(), x._1()._2(), x._2()._1(), x._2()._2(), x._2()._3()), | |
(P5<A,B,C,D,E> x) -> P.p(P.p(x._1(), x._2()), P.p(x._3(), x._4(), x._5())) | |
); | |
} | |
public LoadSave3<A> makeLoadSave() { | |
return new LoadSave3<A>() { | |
@Override | |
public <E, M> __<M, A> load(DataSourceEffects2<E, M> eff) { | |
return ProductTypeables | |
.<A>cases() | |
.Singleton( | |
(String tag, Typeable<A> typeable) -> | |
eff.getFieldM(tag, typeable.makeLoadSave().load(eff)) | |
) | |
.Append((AppendData<?,?,A> appendData) -> appendData.makeLoadSave().load(eff)) | |
.Invmap((InvmapData<?,A> invmapData) -> invmapData.makeLoadSave().load(eff)) | |
.f(ProductTypeable.this); | |
} | |
@Override | |
public <E, M> __<M, Unit> save(DataSinkEffects2<E, M> eff, A a) { | |
return ProductTypeables | |
.<A>cases() | |
.Singleton( | |
(String tag, Typeable<A> typeable) -> | |
eff.setFieldM(tag, typeable.makeLoadSave().save(eff, a)) | |
) | |
.Append((AppendData<?,?,A> appendData) -> appendData.makeLoadSave().save(eff, a)) | |
.Invmap((InvmapData<?,A> invmapData) -> invmapData.makeLoadSave().save(eff, a)) | |
.f(ProductTypeable.this); | |
} | |
}; | |
} | |
public Equal<A> makeEqual() { | |
return ProductTypeables | |
.<A>cases() | |
.Singleton((String tag, Typeable<A> typeable) -> typeable.makeEqual()) | |
.Append(AppendData::makeEqual) | |
.Invmap(InvmapData::makeEqual) | |
.f(this); | |
} | |
public Ord<A> makeOrd() { | |
return ProductTypeables | |
.<A>cases() | |
.Singleton((String tag, Typeable<A> typeable) -> typeable.makeOrd()) | |
.Append(AppendData::makeOrd) | |
.Invmap(InvmapData::makeOrd) | |
.f(this); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sm.fp.data.typeable; | |
import com.sm.fp.DataSinkEffects2; | |
import com.sm.fp.DataSourceEffects2; | |
import com.sm.fp.LoadSave3; | |
import com.sm.fp.data.eq.type.Leibniz; | |
import com.sm.fp.kinds.__; | |
import fj.*; | |
import fj.data.List; | |
import org.derive4j.Data; | |
import org.derive4j.Flavour; | |
@Data(flavour = Flavour.FJ) | |
public abstract class Typeable<A> implements __<Typeable.Mu,A> { | |
public static class Mu {} | |
public interface Cases<R,A> { | |
R Unit(Leibniz<Unit,A> unitLeibniz); | |
R Boolean(Leibniz<Boolean,A> booleanLeibniz); | |
R Integer(Leibniz<Integer,A> integerLeibniz); | |
R Long(Leibniz<Long,A> longLeibniz); | |
R Double(Leibniz<Double,A> doubleLeibniz); | |
R String(Leibniz<String,A> stringLeibniz); | |
R Product(ProductTypeable<A> productTypeable); | |
R Coproduct(CoproductTypeable<A> coproductTypeable); | |
R List(ListData<?,A> listData); | |
R Invmap(InvmapData<?,A> invmapData); | |
} | |
public abstract <R> R match(Cases<R,A> cases); | |
public static class InvmapData<A,B> { | |
private final Typeable<A> _typeable; | |
private final F<A,B> _f; | |
private final F<B,A> _g; | |
private InvmapData(Typeable<A> typeable, F<A,B> f, F<B,A> g) { | |
this._typeable = typeable; | |
this._f = f; | |
this._g = g; | |
} | |
public static <A,B> InvmapData<A,B> create(Typeable<A> typeable, F<A,B> f, F<B,A> g) { | |
return new InvmapData<>(typeable, f, g); | |
} | |
public Typeable<A> typeable() { | |
return _typeable; | |
} | |
public F<A, B> f() { | |
return _f; | |
} | |
public F<B, A> g() { | |
return _g; | |
} | |
public LoadSave3<B> makeLoadSave() { | |
return new LoadSave3<B>() { | |
@Override | |
public <E, M> __<M, B> load(DataSourceEffects2<E, M> eff) { | |
return eff.functor.fmap(f(), typeable().makeLoadSave().load(eff)); | |
} | |
@Override | |
public <E, M> __<M, Unit> save(DataSinkEffects2<E, M> eff, B b) { | |
return typeable().makeLoadSave().save(eff, g().f(b)); | |
} | |
}; | |
} | |
public Equal<B> makeEqual() { | |
return typeable().makeEqual().contramap(g()); | |
} | |
public Ord<B> makeOrd() { | |
return typeable().makeOrd().contramap(g()); | |
} | |
} | |
public <B> Typeable<B> invmap(F<A,B> f, F<B,A> g) { | |
return Typeables.Invmap(InvmapData.create(this, f, g)); | |
} | |
public static Typeable<Unit> unit() { | |
return Typeables.Unit(Leibniz.refl()); | |
} | |
public static Typeable<Boolean> boolean_() { | |
return Typeables.Boolean(Leibniz.refl()); | |
} | |
public static Typeable<Integer> int_() { | |
return Typeables.Integer(Leibniz.refl()); | |
} | |
public static Typeable<Long> long_() { | |
return Typeables.Long(Leibniz.refl()); | |
} | |
public static Typeable<Double> double_() { | |
return Typeables.Double(Leibniz.refl()); | |
} | |
public static Typeable<String> string_() { | |
return Typeables.String(Leibniz.refl()); | |
} | |
public static <A> Typeable<A> product(ProductTypeable<A> productTypeable) { | |
return Typeables.Product(productTypeable); | |
} | |
public static <A> Typeable<A> coproduct(CoproductTypeable<A> coproductTypeable) { | |
return Typeables.Coproduct(coproductTypeable); | |
} | |
public static <A> Typeable<List<A>> list(Typeable<A> typeable) { | |
return Typeables.List(ListData.create(typeable)); | |
} | |
public static class ListData<A,B> { | |
private final Typeable<A> _typeable; | |
private final Leibniz<List<A>,B> _leibniz; | |
private ListData(Typeable<A> typeable, Leibniz<List<A>,B> leibniz) { | |
this._typeable = typeable; | |
this._leibniz = leibniz; | |
} | |
public static <A> ListData<A,List<A>> create(Typeable<A> typeable) { | |
return new ListData<>(typeable, Leibniz.refl()); | |
} | |
public Typeable<A> typeable() { | |
return _typeable; | |
} | |
public Leibniz<List<A>,B> leibniz() { | |
return _leibniz; | |
} | |
public LoadSave3<B> makeLoadSave() { | |
LoadSave3<A> elementLoadSave = typeable().makeLoadSave(); | |
return new LoadSave3<B>() { | |
@Override | |
public <E, M> __<M, B> load(DataSourceEffects2<E, M> eff) { | |
return leibniz().subst(eff.getListOfM(elementLoadSave.load(eff))); | |
} | |
@Override | |
public <E, M> __<M, Unit> save(DataSinkEffects2<E, M> eff, B a) { | |
return eff.setListOfM(leibniz().symm().coerce(a), (A x) -> elementLoadSave.save(eff, x)); | |
} | |
}; | |
} | |
public Equal<B> makeEqual() { | |
return Equal.listEqual(typeable().makeEqual()).contramap((B x) -> leibniz().symm().coerce(x)); | |
} | |
public Ord<B> makeOrd() { | |
return Ord.listOrd(typeable().makeOrd()).contramap((B x) -> leibniz().symm().coerce(x)); | |
} | |
} | |
public LoadSave3<A> makeLoadSave() { | |
return new LoadSave3<A>() { | |
@Override | |
public <E, M> __<M, A> load(DataSourceEffects2<E, M> eff) { | |
return Typeables | |
.<A>cases() | |
.Unit((Leibniz<Unit,A> leibniz) -> eff.monad.pure(leibniz.coerce(Unit.unit()))) | |
.Boolean((Leibniz<Boolean, A> leibniz) -> leibniz.subst(eff.getBooleanM())) | |
.Integer((Leibniz<Integer, A> leibniz) -> leibniz.subst(eff.getIntegerM())) | |
.Long((Leibniz<Long, A> leibniz) -> | |
leibniz.subst(eff.functor.fmap((Integer a) -> a.longValue(), eff.getIntegerM())) | |
) | |
.Double((Leibniz<Double, A> leibniz) -> leibniz.subst(eff.getDoubleM())) | |
.String((Leibniz<String, A> leibniz) -> leibniz.subst(eff.getStringM())) | |
.Product( | |
(ProductTypeable<A> productTypeable) -> | |
productTypeable.makeLoadSave().load(eff) | |
) | |
.Coproduct( | |
(CoproductTypeable<A> coproductTypeable) -> | |
coproductTypeable.makeLoadSave().load(eff) | |
) | |
.List((ListData<?, A> x) -> x.makeLoadSave().load(eff)) | |
.Invmap((InvmapData<?, A> x) -> x.makeLoadSave().load(eff)) | |
.f(Typeable.this); | |
} | |
@Override | |
public <E, M> __<M, Unit> save(DataSinkEffects2<E, M> eff, A a) { | |
return Typeables | |
.<A>cases() | |
.Unit((Leibniz<Unit, A> leibniz) -> eff.monad.pure(Unit.unit())) | |
.Boolean((Leibniz<Boolean, A> leibniz) -> eff.setBooleanM(leibniz.symm().coerce(a))) | |
.Integer((Leibniz<Integer, A> leibniz) -> eff.setIntegerM(leibniz.symm().coerce(a))) | |
.Long((Leibniz<Long, A> leibniz) -> eff.setIntegerM(leibniz.symm().coerce(a).intValue())) | |
.Double((Leibniz<Double, A> leibniz) -> eff.setDoubleM(leibniz.symm().coerce(a))) | |
.String((Leibniz<String, A> leibniz) -> eff.setStringM(leibniz.symm().coerce(a))) | |
.Product( | |
(ProductTypeable<A> productTypeable) -> | |
productTypeable.makeLoadSave().save(eff, a) | |
) | |
.Coproduct( | |
(CoproductTypeable<A> coproductTypeable) -> | |
coproductTypeable.makeLoadSave().save(eff, a) | |
) | |
.List((ListData<?, A> x) -> x.makeLoadSave().save(eff, a)) | |
.Invmap((InvmapData<?, A> x) -> x.makeLoadSave().save(eff, a)) | |
.f(Typeable.this); | |
} | |
}; | |
} | |
public Equal<A> makeEqual() { | |
return Typeables | |
.<A>cases() | |
.Unit((Leibniz<Unit,A> leibniz) -> Equal.equal(a -> b -> true).contramap((A x) -> leibniz.symm().coerce(x))) | |
.Boolean((Leibniz<Boolean,A> leibniz) -> Equal.booleanEqual.contramap((A x) -> leibniz.symm().coerce(x))) | |
.Integer((Leibniz<Integer,A> leibniz) -> Equal.intEqual.contramap((A x) -> leibniz.symm().coerce(x))) | |
.Long((Leibniz<Long,A> leibniz) -> Equal.longEqual.contramap((A x) -> leibniz.symm().coerce(x))) | |
.Double((Leibniz<Double,A> leibniz) -> Equal.doubleEqual.contramap((A x) -> leibniz.symm().coerce(x))) | |
.String((Leibniz<String,A> leibniz) -> Equal.stringEqual.contramap((A x) -> leibniz.symm().coerce(x))) | |
.Product(ProductTypeable::makeEqual) | |
.Coproduct(CoproductTypeable::makeEqual) | |
.List(ListData::makeEqual) | |
.Invmap(InvmapData::makeEqual) | |
.f(this); | |
} | |
public Ord<A> makeOrd() { | |
return Typeables | |
.<A>cases() | |
.Unit((Leibniz<Unit,A> leibniz) -> Ord.ord(a -> b -> Ordering.EQ).contramap((A x) -> leibniz.symm().coerce(x))) | |
.Boolean((Leibniz<Boolean,A> leibniz) -> Ord.booleanOrd.contramap((A x) -> leibniz.symm().coerce(x))) | |
.Integer((Leibniz<Integer,A> leibniz) -> Ord.intOrd.contramap((A x) -> leibniz.symm().coerce(x))) | |
.Long((Leibniz<Long,A> leibniz) -> Ord.longOrd.contramap((A x) -> leibniz.symm().coerce(x))) | |
.Double((Leibniz<Double,A> leibniz) -> Ord.doubleOrd.contramap((A x) -> leibniz.symm().coerce(x))) | |
.String((Leibniz<String,A> leibniz) -> Ord.stringOrd.contramap((A x) -> leibniz.symm().coerce(x))) | |
.Product(ProductTypeable::makeOrd) | |
.Coproduct(CoproductTypeable::makeOrd) | |
.List(ListData::makeOrd) | |
.Invmap(InvmapData::makeOrd) | |
.f(this); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment