From e77b629daa992f05ae99fdb56c65283a7f422082 Mon Sep 17 00:00:00 2001 From: Giovanni Ciatto Date: Mon, 6 Sep 2021 17:06:45 +0200 Subject: [PATCH] + PyUtils on jvm --- .../tuprolog/core/visitors/DefaultTermVisitor.kt | 11 +++++++++++ .../core/visitors/ExhaustiveTermVisitor.kt | 11 +++++++++++ .../kotlin/it/unibo/tuprolog/utils/PyUtils.kt | 14 ++++++++++++++ .../unibo/tuprolog/utils/impl/IterableWrapper.kt | 5 +++++ .../unibo/tuprolog/utils/impl/IteratorWrapper.kt | 12 ++++++++++++ .../unibo/tuprolog/utils/impl/SequenceWrapper.kt | 5 +++++ 6 files changed, 58 insertions(+) create mode 100644 utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/PyUtils.kt create mode 100644 utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/IterableWrapper.kt create mode 100644 utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/IteratorWrapper.kt create mode 100644 utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/SequenceWrapper.kt diff --git a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/visitors/DefaultTermVisitor.kt b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/visitors/DefaultTermVisitor.kt index 4af3f1937..7d06fd910 100644 --- a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/visitors/DefaultTermVisitor.kt +++ b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/visitors/DefaultTermVisitor.kt @@ -1,8 +1,19 @@ package it.unibo.tuprolog.core.visitors import it.unibo.tuprolog.core.Term +import kotlin.js.JsName +import kotlin.jvm.JvmStatic abstract class DefaultTermVisitor : AbstractTermVisitor() { override fun join(term: X, f1: (X) -> T, vararg fs: (X) -> T): T = sequenceOf(f1, *fs).map { it(term) }.first() + + companion object { + @JvmStatic + @JsName("of") + fun of(defaultValue: (Term) -> X): DefaultTermVisitor = + object : DefaultTermVisitor() { + override fun defaultValue(term: Term): X = defaultValue(term) + } + } } diff --git a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/visitors/ExhaustiveTermVisitor.kt b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/visitors/ExhaustiveTermVisitor.kt index f47139077..9588694b2 100644 --- a/core/src/commonMain/kotlin/it/unibo/tuprolog/core/visitors/ExhaustiveTermVisitor.kt +++ b/core/src/commonMain/kotlin/it/unibo/tuprolog/core/visitors/ExhaustiveTermVisitor.kt @@ -1,8 +1,19 @@ package it.unibo.tuprolog.core.visitors import it.unibo.tuprolog.core.Term +import kotlin.js.JsName +import kotlin.jvm.JvmStatic abstract class ExhaustiveTermVisitor : AbstractTermVisitor() { override fun join(term: X, f1: (X) -> T, vararg fs: (X) -> T): T = sequenceOf(f1, *fs).map { it(term) }.last() + + companion object { + @JvmStatic + @JsName("of") + fun of(defaultValue: (Term) -> X): ExhaustiveTermVisitor = + object : ExhaustiveTermVisitor() { + override fun defaultValue(term: Term): X = defaultValue(term) + } + } } diff --git a/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/PyUtils.kt b/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/PyUtils.kt new file mode 100644 index 000000000..b86967f19 --- /dev/null +++ b/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/PyUtils.kt @@ -0,0 +1,14 @@ +@file:JvmName("PyUtils") + +package it.unibo.tuprolog.utils + +import it.unibo.tuprolog.utils.impl.IterableWrapper +import it.unibo.tuprolog.utils.impl.IteratorWrapper +import it.unibo.tuprolog.utils.impl.SequenceWrapper +import kotlin.jvm.JvmName + +fun iterator(iterator: Iterator): MutableIterator = IteratorWrapper(iterator) + +fun iterable(iterable: Iterable): MutableIterable = IterableWrapper(iterable) + +fun iterable(sequence: Sequence): MutableIterable = SequenceWrapper(sequence) diff --git a/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/IterableWrapper.kt b/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/IterableWrapper.kt new file mode 100644 index 000000000..68e4bcf89 --- /dev/null +++ b/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/IterableWrapper.kt @@ -0,0 +1,5 @@ +package it.unibo.tuprolog.utils.impl + +internal class IterableWrapper(private val wrapped: Iterable) : MutableIterable { + override fun iterator(): MutableIterator = IteratorWrapper(wrapped.iterator()) +} diff --git a/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/IteratorWrapper.kt b/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/IteratorWrapper.kt new file mode 100644 index 000000000..864ff04bd --- /dev/null +++ b/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/IteratorWrapper.kt @@ -0,0 +1,12 @@ +package it.unibo.tuprolog.utils.impl + +internal class IteratorWrapper(private val wrapped: Iterator) : MutableIterator { + override fun hasNext(): Boolean = wrapped.hasNext() + + override fun next(): T = wrapped.next() + + override fun remove() = when (wrapped) { + is MutableIterator -> wrapped.remove() + else -> throw UnsupportedOperationException() + } +} diff --git a/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/SequenceWrapper.kt b/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/SequenceWrapper.kt new file mode 100644 index 000000000..5b95363e3 --- /dev/null +++ b/utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/SequenceWrapper.kt @@ -0,0 +1,5 @@ +package it.unibo.tuprolog.utils.impl + +internal class SequenceWrapper(private val wrapped: Sequence) : MutableIterable { + override fun iterator(): MutableIterator = IteratorWrapper(wrapped.iterator()) +}