diff --git a/docs/Iterable.md b/docs/Iterable.md index 66f07b3..c4d874c 100644 --- a/docs/Iterable.md +++ b/docs/Iterable.md @@ -68,6 +68,20 @@ These, again, does the thing: >>> void +## map and mapNotNull + +Used to transform either the whole iterable stream or also skipping som elements from it: + + val source = [1,2,3,4] + // transform every element to string or null: + assertEquals(["n1", "n2", null, "n4"], source.map { if( it == 3 ) null else "n"+it } ) + + // transform every element to stirng, skipping 3: + assertEquals(["n1", "n2", "n4"], source.mapNotNull { if( it == 3 ) null else "n"+it } ) + + >>> void + + ## Instance methods: | fun/method | description | diff --git a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjIterable.kt b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjIterable.kt index 389a2c1..13aa53f 100644 --- a/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjIterable.kt +++ b/lynglib/src/commonMain/kotlin/net/sergeych/lyng/obj/ObjIterable.kt @@ -158,6 +158,23 @@ val ObjIterable by lazy { ObjList(result) } + addFnDoc( + name = "mapNotNull", + doc = "Transform elements by applying the given lambda unless it returns null.", + params = listOf(ParamDoc("transform")), + returns = type("lyng.List"), + isOpen = true, + moduleName = "lyng.stdlib" + ) { + val fn = requiredArg(0) + val result = mutableListOf() + thisObj.toFlow(this).collect { + val transformed = fn.call(this, it) + if( transformed != ObjNull) result += transformed + } + ObjList(result) + } + addFnDoc( name = "take", doc = "Take the first N elements and return them as a list.",