Title
m

Matthew Good

04/28/2019, 3:08 PM
also does anyone know how to correctly accomplish combinators,
``combinator(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``````
``````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}") }
}``````
p

Pavlo Liapota

04/28/2019, 3:40 PM
I am not sure, what you want, but I hope this will help https://pl.kotl.in/2TEpumbxJ
m

Matthew Good

04/28/2019, 4:03 PM
@Pavlo Liapota the values themselves cannot be a list eg assuming
``Node``
is the actual class
``````abstract class Node {
fun and(other: Node) = AndNode(this, other)

abstract var value: Int
}``````
p

Pavlo Liapota

04/28/2019, 4:08 PM
What do you want to achieve?
m

Matthew Good

04/28/2019, 4:11 PM
ill try to make a more complete minimal example
@Pavlo Liapota https://pl.kotl.in/lcZsg4y4_ the general idea for a combinator's
``peek()``
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)``
k

karelpeeters

04/28/2019, 5:16 PM
You're going to have to narrow down that question, that's a lot of code to go trough...
m

Matthew Good

04/29/2019, 10:57 AM
never mind, i ended up using a list to implement it