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 import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych" 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 // Removed legacy buildscript classpath declarations; plugins are applied via the plugins DSL below

View File

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

View File

@ -2446,7 +2446,7 @@ class ScriptTest {
for( x in set ) println(x) for( x in set ) println(x)
assert([1,2,3] == set.toList()) assert([1,2,3] == set.toList())
set += 4 set += 4
assert(set.toList() == [1,2,3,4]) assertEquals(set.toList(), [1,2,3,4])
assert(set == Set(1,2,3,4)) assert(set == Set(1,2,3,4))
val s1 = [1, 2].toSet() 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 @Test
fun testLet() = runTest { fun testLet() = runTest {
eval( eval(