-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
11 changes: 11 additions & 0 deletions
11
core/src/commonMain/kotlin/it/unibo/tuprolog/core/visitors/DefaultTermVisitor.kt
This file contains 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
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) | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
core/src/commonMain/kotlin/it/unibo/tuprolog/core/visitors/ExhaustiveTermVisitor.kt
This file contains 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
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
14
utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/PyUtils.kt
This file contains 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
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) |
5 changes: 5 additions & 0 deletions
5
utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/IterableWrapper.kt
This file contains 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
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()) | ||
} |
12 changes: 12 additions & 0 deletions
12
utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/IteratorWrapper.kt
This file contains 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
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() | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
utils/src/jvmMain/kotlin/it/unibo/tuprolog/utils/impl/SequenceWrapper.kt
This file contains 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
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()) | ||
} |