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()) +}