Created
December 15, 2017 00:16
-
-
Save clinuxrulz/b9611590ea2ef3f8458d79224a5f3ed3 to your computer and use it in GitHub Desktop.
Alt experiment many() some() fix()
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 org.highj.typeclass1.alternative; | |
import org.derive4j.hkt.__; | |
import org.highj.data.List; | |
import org.highj.data.tuple.T1; | |
import org.highj.data.tuple.T2; | |
import org.highj.function.F1; | |
import org.highj.typeclass1.LazifyH; | |
import org.highj.typeclass1.monad.Applicative; | |
class AltUtil { | |
private static final boolean DO_CALL_SOME = true; | |
private static final boolean DO_CALL_MANY = false; | |
static <F,A> __<F,List<A>> some(Alt<F> alt, Applicative<F> applicative, LazifyH<F> lazify, __<F,A> fa) { | |
return AltUtil.<F,A>someOrMany(alt, applicative, lazify).apply(T2.of(fa, DO_CALL_SOME)); | |
} | |
static <F,A> __<F,List<A>> many(Alt<F> alt, Applicative<F> applicative, LazifyH<F> lazify, __<F,A> fa) { | |
return AltUtil.<F,A>someOrMany(alt, applicative, lazify).apply(T2.of(fa, DO_CALL_MANY)); | |
} | |
private static <F,A> F1<T2<__<F,A>,Boolean>,__<F,List<A>>> someOrMany(Alt<F> alt, Applicative<F> applicative, LazifyH<F> lazify) { | |
return F1.fix( | |
(T1<F1<T2<__<F,A>,Boolean>,__<F,List<A>>>> rec) -> { | |
F1<T2<__<F,A>,Boolean>,T1<__<F,List<A>>>> rec2 = (T2<__<F,A>,Boolean> in) -> T1.of$(() -> rec._1().apply(in)); | |
return (F1<T2<__<F, A>, Boolean>, __<F, List<A>>>) (T2<__<F, A>, Boolean> in) -> { | |
__<F, A> fa = in._1(); | |
@SuppressWarnings("PointlessBooleanExpression") boolean callingSome = in._2() == DO_CALL_SOME; | |
if (callingSome) { | |
// some() implementation | |
return applicative.apply2( | |
(A head) -> (List<A> tail) -> List.Cons(head, tail), | |
fa, | |
lazify.lazifyH(rec2.apply(T2.of(fa, DO_CALL_SOME))) | |
); | |
} else { | |
// many() inplementation | |
return alt.mplus(lazify.lazifyH(rec2.apply(T2.of(fa, DO_CALL_MANY))), applicative.pure(List.Nil())); | |
} | |
}; | |
} | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment