Call a function: Difference between revisions

 
(7 intermediate revisions by the same user not shown)
Line 3,668:
 
=={{header|langur}}==
Functions are first-order, and may be passed around, whether user-defined or built-in.
User-defined and built-in functions can be called using parentheses.
 
There are several ways to call a function in langur.
Built-in functions can also be called using an "unbounded list," which ends at a line return, EOF, closing parenthesis, curly brace, or square bracket, or before a comma preceding a line return.
 
=== parentheses ===
You can always call a function using parentheses.
<syntaxhighlight lang="langur">.x()
# call user-defined function</syntaxhighlight lang="langur">
x()
</syntaxhighlight>
 
<syntaxhighlight lang="langur">write(.key, ": ", .value)
write(somestring)
# call built-in with parentheses</syntaxhighlight>
</syntaxhighlight>
 
=== unbounded argument lists ===
In statement context, you can call a function with an unbounded list of arguments.
<syntaxhighlight lang="langur">write .key, ": ", .value
# call built-in with unbounded list</syntaxhighlight>
 
<syntaxhighlight lang="langur">writeln "numbers: ", join ", ", [.a1, .a2, .a3, .a4]
writeln a, b, c
# unbounded lists on writeln and join
# later function join takes remaining arguments</syntaxhighlight>
 
=== forwarding operator ===
<syntaxhighlight lang="langur">writeln "numbers: ", join(", ", [.a1, .a2, .a3, .a4]), " === "
When passing a single argument, you can use the forwarding operator.
# unbounded list on writeln
# join using parentheses so it doesn't take remaining arguments</syntaxhighlight>
 
<syntaxhighlight lang="langur">val .sum = foldfrom(
a -> len
fn(.sum, .i, .c) { .sum + number(.c, 36) * .weight[.i] },
# passes a to the len() function
0,
</syntaxhighlight>
pseries len .code,
split .code,
)
# split, pseries, and len using unbounded lists, ending before comma preceding line return</syntaxhighlight>
 
=== recursion ===
<syntaxhighlight lang="langur">for .key in sort(keys .tests) {
Use the fn token with double parentheses for recursion.
...
 
}
<syntaxhighlight lang="langur">.x()
# unbounded list on keys bounded by closing parenthesis of sort</syntaxhighlight>
val fibonacci = fn x:if x < 2 { x } else { fn((x - 1)) + fn((x - 2)) }
</syntaxhighlight>
 
=== argument expansion ===
Use the expansion operator (...) to pass a list as multiple arguments. The following example works if wordsets is a list of lists, and passes each list as a separate argument to the mapX function.
 
<syntaxhighlight lang="langur">write .key, ": ", .value
mapX(amb, wordsets...)
</syntaxhighlight>
 
=={{header|Latitude}}==
1,007

edits