Jump to content

Function definition: Difference between revisions

mNo edit summary
 
Line 2,208:
=== parameters ===
Parameters are defined within parentheses after the fn token. To specify no parameters, use an empty set of parentheses.
<syntaxhighlight lang="langur">val .multiply = fn(.x, .y) { .x * .y }
.multiply(3, 4)</syntaxhighlight>
 
=== operator implied functions ===
Operator implied functions are built using an infix operator between curly braces on an fn token.
 
<syntaxhighlight lang="langur">val .multiply = fn{*}
.multiply(3, 4)</syntaxhighlight>
 
=== nil left partially implied functions ===
These are built with an infix operator and a right-hand operand inside the fn{... } tokens.
 
<syntaxhighlight lang="langur">val .times3 = fn{* 3}
map .times3, [1, 2, 3]</syntaxhighlight>
 
=== impure functions (I/O) ===
Impure functions must be declared as such.
<syntaxhighlight>val .writeit = impure fn(.x) { writeln .x }</syntaxhighlight>
 
Impure functions cannot be passed to pure functions.
990

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.