From 8fae4709ed5e06d78939b5657c733819d904f8ba Mon Sep 17 00:00:00 2001 From: sergeych Date: Thu, 27 Nov 2025 20:54:33 +0100 Subject: [PATCH] fix #61 set +/- collection gives set with/without items in collection --- lynglib/build.gradle.kts | 2 +- .../sergeych/lyng/obj/ObjKotlinIterator.kt | 2 +- .../kotlin/net/sergeych/lyng/obj/ObjSet.kt | 41 +++++++++++++++---- lynglib/src/commonTest/kotlin/ScriptTest.kt | 14 ++++++- 4 files changed, 47 insertions(+), 12 deletions(-) diff --git a/lynglib/build.gradle.kts b/lynglib/build.gradle.kts index 6445cdc..772cb2b 100644 --- a/lynglib/build.gradle.kts +++ b/lynglib/build.gradle.kts @@ -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 diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjKotlinIterator.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjKotlinIterator.kt index 5634080..96f6531 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjKotlinIterator.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjKotlinIterator.kt @@ -96,4 +96,4 @@ suspend fun Obj.enumerate(scope: Scope, callback: suspend (Obj) -> Boolean) { } if (closeIt) iterator.invokeInstanceMethod(scope, "cancelIteration") { ObjVoid } -} \ No newline at end of file +} diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjSet.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjSet.kt index 7e538b6..5b929ea 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjSet.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjSet.kt @@ -34,8 +34,19 @@ class ObjSet(val set: MutableSet = mutableSetOf()) : Obj() { return ObjSet( if (other is ObjSet) (set + other.set).toMutableSet() - else - (set + other).toMutableSet() + else { + if( other.isInstanceOf(ObjIterable) ) { + val otherSet = mutableSetOf() + other.enumerate(scope) { + otherSet += it + true + } + (set + otherSet).toMutableSet() + } + else { + (set + other).toMutableSet() + } + } ) } @@ -51,12 +62,14 @@ class ObjSet(val set: MutableSet = 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() + other.enumerate(scope) { + otherSet += it + true } + set += otherSet } - set += other + else set += other } } return this @@ -70,9 +83,19 @@ class ObjSet(val set: MutableSet = 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() + other.enumerate(scope) { + otherSet += it + true + } + ObjSet((set - otherSet).toMutableSet()) + } + else -> + scope.raiseIllegalArgument("set operator - requires another set or Iterable") + } } override fun toString(): String { diff --git a/lynglib/src/commonTest/kotlin/ScriptTest.kt b/lynglib/src/commonTest/kotlin/ScriptTest.kt index b286722..33e0bd2 100644 --- a/lynglib/src/commonTest/kotlin/ScriptTest.kt +++ b/lynglib/src/commonTest/kotlin/ScriptTest.kt @@ -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(