From f1c2d614b54aaeacb521666248ea3f641a002597 Mon Sep 17 00:00:00 2001 From: Alexander Ioffe Date: Mon, 25 Dec 2023 23:41:25 -0500 Subject: [PATCH] Fix Then0 cases --- build.gradle.kts | 2 +- decomat-core/build.gradle.kts | 6 ++++ .../main/kotlin/io/decomat/ThenPattern1.kt | 18 +++++++++++ decomat-core/src/templates/Pattern3.ftl | 4 +-- .../src/test/kotlin/io/decomat/ThenTest.kt | 30 +++++++++++++++++++ 5 files changed, 57 insertions(+), 3 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 9821e40..7e19193 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -21,7 +21,7 @@ apply(plugin = "io.github.gradle-nexus.publish-plugin") allprojects { group = "io.exoquery" - version = "0.0.2" + version = "0.0.3" apply(plugin = "kotlin") apply(plugin = "maven-publish") diff --git a/decomat-core/build.gradle.kts b/decomat-core/build.gradle.kts index 754731d..1db06d5 100644 --- a/decomat-core/build.gradle.kts +++ b/decomat-core/build.gradle.kts @@ -36,6 +36,12 @@ tasks.register("template_base", TemplateTask::class) { into("build/templates/io/decomat") } +//tasks.withType { +// kotlinOptions{ +// freeCompilerArgs = listOf("-Xno-optimize") +// } +//} + //tasks.register("template", Sync::class) { // dependsOn("template_base") // from("build/templates/io/decomat") diff --git a/decomat-core/src/main/kotlin/io/decomat/ThenPattern1.kt b/decomat-core/src/main/kotlin/io/decomat/ThenPattern1.kt index bd105f8..80ac85b 100644 --- a/decomat-core/src/main/kotlin/io/decomat/ThenPattern1.kt +++ b/decomat-core/src/main/kotlin/io/decomat/ThenPattern1.kt @@ -1,5 +1,23 @@ package io.decomat +fun case(pat: Pattern0) = ThenIs(pat, {true}) + +class ThenIs( + override val pat: Pattern0, + override val check: (R) -> Boolean +): Stage, R> { + inline fun useComponents(r: R, f: (R) -> O): O { + return f(r) + } + + inline fun thenIf(crossinline f: (R) -> Boolean) = ThenIs(pat) { r: R -> useComponents(r, f) } + inline fun thenIfThis(crossinline f: R.() -> (R) -> Boolean) = ThenIs(pat) { r: R -> useComponents(r, f(r)) } + inline fun then(crossinline f: (R) -> O): Case = StageCase(pat, check) { r: R -> useComponents(r, f) } + inline fun thenThis(crossinline f: R.() -> (R) -> O): Case = StageCase(pat, check) { r: R -> useComponents(r, f(r)) } +} + + + fun , R1, R> case(pat: Pattern1) = Then0(pat, {true}) class Then0, R1, R>( diff --git a/decomat-core/src/templates/Pattern3.ftl b/decomat-core/src/templates/Pattern3.ftl index 5aad04b..5c2a020 100644 --- a/decomat-core/src/templates/Pattern3.ftl +++ b/decomat-core/src/templates/Pattern3.ftl @@ -99,8 +99,8 @@ [@output file="decomat-core/build/templates/io/decomat/Then2.kt"] package io.decomat -[#list 0..3 as i1] - [#list 0..3 as i2] +[#list 0..2 as i1] + [#list 0..2 as i2] [#-- fun , P2: Pattern0, P11: Pattern, R11, R1, R2, R> case(pat: Pattern2) = Then10(pat, {true}) --] fun case(pat: [@Pattern 2 /]) = Then${i1}${i2}(pat, {true}) diff --git a/decomat-core/src/test/kotlin/io/decomat/ThenTest.kt b/decomat-core/src/test/kotlin/io/decomat/ThenTest.kt index 6e5ceea..d869e7e 100644 --- a/decomat-core/src/test/kotlin/io/decomat/ThenTest.kt +++ b/decomat-core/src/test/kotlin/io/decomat/ThenTest.kt @@ -4,11 +4,41 @@ import kotlin.test.Test public class ThenTest: DecomatTest { // Just check that the types don't fail + @Test + fun `ThenIs - Is`() = + assert( + case(Is()).then { a -> Res1(a) }.eval(foo) == Res1(foo) + ) + @Test fun typesTest() { case(FlatMap_M(Is(), Is())).then { a: Query, b: Query -> Res2(a, b) } } + @Test + fun `Then0 - distinct(Is) - Filter`() = + assert( + case(Distinct_M(Is())).then { (a) -> Res1(a) }.eval(Distinct(foo)) == Res1(foo) + ) + + @Test + fun `Then0 - distinct(Is)`() = + assert( + case(Distinct_M(Is())).then { (a) -> Res1(a) }.eval(Distinct(foo)) == Res1(foo) + ) + + @Test + fun `Then1 - distinct(distinct(Is))`() = + assert( + case(Distinct_M(Distinct_M(Is()))).then { (a) -> Res1(a) }.eval(Distinct(Distinct(foo))) == Res1(foo) + ) + + @Test + fun `Then2 - distinct(flatMap(Is, Is))`() = + assert( + case(Distinct_M(FlatMap_M(Is(), Is()))).then { (a, b) -> Res2(a, b) }.eval(Distinct(FlatMap(foo, bar))) == Res2(foo, bar) + ) + @Test fun `Then00 - flatMap(Is, Is) - Filter`() = assert(