Created
January 28, 2019 00:03
-
-
Save bjonnh/0b7dbd3b9045aeff6593c73c9b3dcdc3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 arrowdebug | |
import arrow.core.left | |
import arrow.core.right | |
import arrow.effects.IO | |
import arrow.effects.extensions.io.monad.binding | |
import kotlinx.coroutines.runBlocking | |
sealed class Error : RuntimeException() | |
object ClientInvalidQuery : Error() | |
object ClientQueryTimeout : Error() | |
object NotInTransaction : Error() | |
data class RAWSparqlResult( | |
val variables: List<String>, | |
val values: List<List<String>> | |
) | |
data class SparqlResult( | |
val variables: List<String>, | |
val values: List<List<String>> | |
) | |
class Dataset { | |
var running = false | |
fun start() { | |
running = true | |
} | |
fun end() { | |
running = false | |
} | |
} | |
class QueryEngine( | |
val query: String? = null, | |
val dataset: Dataset? = null | |
) { | |
fun execute(): RAWSparqlResult { | |
//throw ClientQueryTimeout | |
if (dataset?.running == false) throw NotInTransaction | |
return RAWSparqlResult(listOf("s", "p", "o"), listOf(listOf("a", "b", "c"))) | |
} | |
} | |
class TestClass { | |
val dataset = Dataset() | |
private fun <A> toIO(f: suspend () -> A): IO<A> = | |
IO.async { _, cb -> | |
try { | |
runBlocking { cb(f().right()) } | |
} catch (e: Throwable) { | |
cb(e.left()) | |
} | |
} | |
fun executeQuery(query: String, dataset: Dataset): QueryEngine { | |
// throw ClientInvalidQuery | |
return QueryEngine(query, dataset) | |
} | |
fun query(query: String): IO<SparqlResult> = | |
binding { | |
IO { dataset.start() }.bind() | |
val qe = toIO { | |
executeQuery(query, dataset) | |
}.bind() | |
val result = toIO { | |
val results = qe.execute() | |
SparqlResult(results.variables, | |
results.values.map { result -> | |
results.variables.map { | |
it + "Something" | |
} | |
} | |
) | |
} | |
IO { dataset.end() }.bind() | |
result.bind() | |
} | |
} | |
fun main() { | |
val testclass = TestClass() | |
println(testclass.query("Test").unsafeRunSync()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment