Skip to content

Commit

Permalink
+ PyUtils on jvm
Browse files Browse the repository at this point in the history
  • Loading branch information
gciatto committed Sep 6, 2021
1 parent f6956cc commit e77b629
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<T> : AbstractTermVisitor<T>() {
override fun <X : Term> join(term: X, f1: (X) -> T, vararg fs: (X) -> T): T =
sequenceOf(f1, *fs).map { it(term) }.first()

companion object {
@JvmStatic
@JsName("of")
fun <X> of(defaultValue: (Term) -> X): DefaultTermVisitor<X> =
object : DefaultTermVisitor<X>() {
override fun defaultValue(term: Term): X = defaultValue(term)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<T> : AbstractTermVisitor<T>() {
override fun <X : Term> join(term: X, f1: (X) -> T, vararg fs: (X) -> T): T =
sequenceOf(f1, *fs).map { it(term) }.last()

companion object {
@JvmStatic
@JsName("of")
fun <X> of(defaultValue: (Term) -> X): ExhaustiveTermVisitor<X> =
object : ExhaustiveTermVisitor<X>() {
override fun defaultValue(term: Term): X = defaultValue(term)
}
}
}
14 changes: 14 additions & 0 deletions utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/PyUtils.kt
Original file line number Diff line number Diff line change
@@ -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 <T> iterator(iterator: Iterator<T>): MutableIterator<T> = IteratorWrapper(iterator)

fun <T> iterable(iterable: Iterable<T>): MutableIterable<T> = IterableWrapper(iterable)

fun <T> iterable(sequence: Sequence<T>): MutableIterable<T> = SequenceWrapper(sequence)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package it.unibo.tuprolog.utils.impl

internal class IterableWrapper<T>(private val wrapped: Iterable<T>) : MutableIterable<T> {
override fun iterator(): MutableIterator<T> = IteratorWrapper(wrapped.iterator())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package it.unibo.tuprolog.utils.impl

internal class IteratorWrapper<T>(private val wrapped: Iterator<T>) : MutableIterator<T> {
override fun hasNext(): Boolean = wrapped.hasNext()

override fun next(): T = wrapped.next()

override fun remove() = when (wrapped) {
is MutableIterator<T> -> wrapped.remove()
else -> throw UnsupportedOperationException()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package it.unibo.tuprolog.utils.impl

internal class SequenceWrapper<T>(private val wrapped: Sequence<T>) : MutableIterable<T> {
override fun iterator(): MutableIterator<T> = IteratorWrapper(wrapped.iterator())
}

0 comments on commit e77b629

Please sign in to comment.