All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.it.unibo.tuprolog.solve.Utils.kt Maven / Gradle / Ivy

There is a newer version: 1.0.4
Show newest version
@file:JvmName("Utils")

package it.unibo.tuprolog.solve

import it.unibo.tuprolog.core.Clause
import it.unibo.tuprolog.core.Directive
import it.unibo.tuprolog.core.Struct
import it.unibo.tuprolog.core.operators.Operator
import it.unibo.tuprolog.core.operators.OperatorSet
import it.unibo.tuprolog.solve.library.Libraries
import it.unibo.tuprolog.solve.library.Library
import it.unibo.tuprolog.theory.Theory
import kotlin.jvm.JvmName

/** Performs the given [action] on each element, giving a lookahead hint (i.e. if there's another element to process after). */
inline fun  Iterator.forEachWithLookahead(action: (T, Boolean) -> Unit) {
    while (hasNext()) {
        action(next(), hasNext())
    }
}

/** Performs the given [action] on each element, giving a lookahead hint (i.e. if there's another element to process after). */
inline fun  Iterable.forEachWithLookahead(action: (T, Boolean) -> Unit) =
    iterator().forEachWithLookahead(action)

/** Performs the given [action] on each element, giving a lookahead hint (i.e. if there's another element to process after). */
inline fun  Sequence.forEachWithLookahead(action: (T, Boolean) -> Unit) =
    iterator().forEachWithLookahead(action)

fun Iterable.getAllOperators(): Sequence {
    return asSequence()
        .filterIsInstance()
        .map { it.body }
        .filterIsInstance()
        .filter { it.arity == 3 && it.functor == "op" }
        .map { Operator.fromTerm(it) }
        .filterNotNull()
}

fun Library.getAllOperators(): Sequence {
    return operators.asSequence()
}

fun Libraries.getAllOperators(): Sequence {
    return operators.asSequence()
}

fun getAllOperators(libraries: Libraries, vararg theories: Theory): Sequence {
    return libraries.getAllOperators() + sequenceOf(*theories).flatMap { it.getAllOperators() }
}

fun Sequence.toOperatorSet(): OperatorSet {
    return OperatorSet(this)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy