Matthew Good
04/28/2019, 3:08 PMcombinator(val).and(val).and(val).and(val) ...
as at this point the only thing i can think of is to implement it as a linked list but i already use the functions provided by the linked list implementation in combinator
val i = input("1234") // i.value is set to "1234"
val combinator = i.combinator("12")
if (
combinator.peek() // if "1234" starts with "12"
) {
combinator.pop() // i.value is now "34" from "1234"
}
this is my current attempt at making such https://pl.kotl.in/1p-y2OEJA (using a binary tree or similar) which currently fails with n4 it.value = 1
n4 it.value = 2
n5 it.value = 1
n5 it.value = 3
instead of n4 it.value = 1
n4 it.value = 2
n5 it.value = 1
n5 it.value = 2
n5 it.value = 3
for fun main() {
val tree = Tree()
val n1 = tree.Node(1)
val n2 = tree.Node(2)
val n3 = tree.Node(3)
val n4 = n1.append(n2)
val n5 = n4.append(n3)
n1.forEach { println("n1 it.value = ${it.value}") }
n2.forEach { println("n2 it.value = ${it.value}") }
n3.forEach { println("n3 it.value = ${it.value}") }
n4.forEach { println("n4 it.value = ${it.value}") }
n5.forEach { println("n5 it.value = ${it.value}") }
}
Pavlo Liapota
04/28/2019, 3:40 PMMatthew Good
04/28/2019, 4:03 PMNode
is the actual class
abstract class Node {
fun and(other: Node) = AndNode(this, other)
abstract var value: Int
}
Pavlo Liapota
04/28/2019, 4:08 PMMatthew Good
04/28/2019, 4:11 PMpeek()
would be (not tested but would be assumed to work)
Parser.IsSequenceOnceAnd.peek() {
val x = this.iterator()
var m = 0
val max = this.count()
while (x.hasNext()) {
val y = x.next()
if (y.peek()) {
if (y.pop()) m++
else break
} else break
// break on false return to avoid iterating a potentially super long chain of sequences if by chance the first sequence does not match
}
if (m == max) return true
return false
}
assumes class IsSequenceOnceAnd
is a variation of IsSequenceOnce
with peek()
and pop()
methods modified to work with an iterator()
, and made specifically for handling IsSequenceOnce.and
function
eg fun and(other: IsSequenceOnce) = IsSequenceOnceAnd(this, other)
karelpeeters
04/28/2019, 5:16 PMMatthew Good
04/29/2019, 10:57 AM