com.github.mperry.fg.SkiCalculus.groovy Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionalgroovy-main Show documentation
Show all versions of functionalgroovy-main Show documentation
FunctionalGroovy enhances FunctionalJava for Groovy
package com.github.mperry.fg
import fj.F
import fj.F2
import fj.data.Either
import groovy.transform.TypeChecked
import groovy.transform.TypeCheckingMode
import static fj.data.Either.left
/**
* Created by MarkPerry on 14/01/14.
*/
@TypeChecked
class SkiCalculus {
def X I(X x) {
x
}
def F K(X x) {
{ Y y -> x } as F
}
def B S(F> x, F y, Z z) {
(x.f(z)).f(y.f(z))
}
/**
* true
* @param x
* @param y
* @return
*/
@TypeChecked(TypeCheckingMode.SKIP)
def X True(X x, Y y) {
K(x).f(y)
}
def F2 True_() {
{ X x, Y y -> True(x, y) } as F2
}
/**
* false
* @param x
* @param y
* @return
*/
@TypeChecked(TypeCheckingMode.SKIP)
def Y False(X x, Y y) {
K(y).f(K(x).f(y))
}
def F2 False_() {
{ X x, Y y -> False(x, y) } as F2
}
def Either not(F2 f) {
def val = f.f(False_(), True_())
(val instanceof A) ? left(val) : Either.right(val)
}
@TypeChecked(TypeCheckingMode.SKIP)
def F2 or(F2 a, F2 b) {
a.f(True_(), b)
}
@TypeChecked(TypeCheckingMode.SKIP)
def F2 and(F2 a, F2 b) {
a.f(b, False_())
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy