fixed bug with callable argument in calls, added more docs

This commit is contained in:
Sergey Chernov 2025-06-02 18:33:45 +04:00
parent 4a597886e6
commit 29c643eed2
3 changed files with 26 additions and 3 deletions

View File

@ -316,6 +316,14 @@ one could be with ellipsis that means "the rest pf arguments as List":
assert( { a, b...-> [a,...b] }(100, 1, 2, 3) == [100, 1, 2, 3]) assert( { a, b...-> [a,...b] }(100, 1, 2, 3) == [100, 1, 2, 3])
void void
### Using lambda as the parameter
fun mapValues(iterable, transform) {
var result = []
for( x in iterable ) result += transform(x)
}
assert( [11, 21, 31] == mapValues( [1,2,3], { it*10+1 }))
>>> void
# Lists (aka arrays) # Lists (aka arrays)

View File

@ -499,7 +499,7 @@ class Compiler(
else -> { else -> {
cc.previous() cc.previous()
parseStatement(cc)?.let { args += ParsedArgument(it, t.pos) } parseExpression(cc)?.let { args += ParsedArgument(it, t.pos) }
?: throw ScriptError(t.pos, "Expecting arguments list") ?: throw ScriptError(t.pos, "Expecting arguments list")
} }
} }

View File

@ -1138,7 +1138,8 @@ class ScriptTest {
@Test @Test
fun testIsPrimeSampleBug() = runTest { fun testIsPrimeSampleBug() = runTest {
eval(""" eval(
"""
fun naive_is_prime(candidate) { fun naive_is_prime(candidate) {
val x = if( candidate !is Int) candidate.toInt() else candidate val x = if( candidate !is Int) candidate.toInt() else candidate
var divisor = 1 var divisor = 1
@ -1151,6 +1152,20 @@ class ScriptTest {
} }
naive_is_prime(4) naive_is_prime(4)
""".trimIndent()) """.trimIndent()
)
}
@Test
fun testLambdaAsFnCallArg() = runTest {
eval(
"""
fun mapValues(iterable, transform) {
var result = []
for( x in iterable ) result += transform(x)
}
assert( [11, 21, 31] == mapValues( if( true) [1,2,3] else [10], { it*10+1 }))
""".trimIndent()
)
} }
} }