fix #61 set +/- collection gives set with/without items in collection

This commit is contained in:
Sergey Chernov 2025-11-27 20:54:33 +01:00
parent d118d29429
commit 8fae4709ed
4 changed files with 47 additions and 12 deletions

View File

@ -21,7 +21,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych"
version = "1.0.3"
version = "1.0.4-SNPASHOT"
// Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below

View File

@ -96,4 +96,4 @@ suspend fun Obj.enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) {
}
if (closeIt)
iterator.invokeInstanceMethod(scope, "cancelIteration") { ObjVoid }
}
}

View File

@ -34,8 +34,19 @@ class ObjSet(val set: MutableSet<Obj> = mutableSetOf()) : Obj() {
return ObjSet(
if (other is ObjSet)
(set + other.set).toMutableSet()
else
(set + other).toMutableSet()
else {
if( other.isInstanceOf(ObjIterable) ) {
val otherSet = mutableSetOf<Obj>()
other.enumerate(scope) {
otherSet += it
true
}
(set + otherSet).toMutableSet()
}
else {
(set + other).toMutableSet()
}
}
)
}
@ -51,12 +62,14 @@ class ObjSet(val set: MutableSet<Obj> = mutableSetOf()) : Obj() {
else -> {
if (other.isInstanceOf(ObjIterable)) {
val i = other.invokeInstanceMethod(scope, "iterable")
while (i.invokeInstanceMethod(scope, "hasNext").toBool()) {
set += i.invokeInstanceMethod(scope, "next")
val otherSet = mutableSetOf<Obj>()
other.enumerate(scope) {
otherSet += it
true
}
set += otherSet
}
set += other
else set += other
}
}
return this
@ -70,9 +83,19 @@ class ObjSet(val set: MutableSet<Obj> = mutableSetOf()) : Obj() {
}
override suspend fun minus(scope: Scope, other: Obj): Obj {
if (other !is ObjSet)
scope.raiseIllegalArgument("set operator - requires another set")
return ObjSet(set.minus(other.set).toMutableSet())
return when {
other is ObjSet -> ObjSet(set.minus(other.set).toMutableSet())
other.isInstanceOf(ObjIterable) -> {
val otherSet = mutableSetOf<Obj>()
other.enumerate(scope) {
otherSet += it
true
}
ObjSet((set - otherSet).toMutableSet())
}
else ->
scope.raiseIllegalArgument("set operator - requires another set or Iterable")
}
}
override fun toString(): String {

View File

@ -2446,7 +2446,7 @@ class ScriptTest {
for( x in set ) println(x)
assert([1,2,3] == set.toList())
set += 4
assert(set.toList() == [1,2,3,4])
assertEquals(set.toList(), [1,2,3,4])
assert(set == Set(1,2,3,4))
val s1 = [1, 2].toSet()
@ -2466,6 +2466,18 @@ class ScriptTest {
)
}
@Test
fun testSetAddRemoveSet() = runTest {
eval("""
val s1 = Set( 1, 2 3)
val s2 = Set( 3, 4 5)
assertEquals( Set(1,2,3,4,5), s1 + s2 )
assertEquals( Set(1,2,3,4,5), s1 + s2.toList() )
assertEquals( Set(1,2), s1 - s2 )
assertEquals( Set(1,2), s1 - s2.toList() )
""".trimIndent())
}
@Test
fun testLet() = runTest {
eval(