Nested function: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
(→{{header|Lambdatalk}}: adding lambdatalk task) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(9 intermediate revisions by 7 users not shown) | |||
Line 519:
writeln(makeList(". "));
}</syntaxhighlight>
{{out}}
<pre>1. first
2. second
3. third</pre>
=={{header|Delphi}}==
Line 525 ⟶ 531:
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module NestedFunction {
static String makeList(String separator) {▼
{▼
▲ static String makeList(String separator)
{▼
Int counter = 1;
Line 536 ⟶ 540:
+ makeItem("second")
+ makeItem("third");
void run() {
@Inject Console console;
console.print(makeList(". "));
}
}
</syntaxhighlight>
Line 554 ⟶ 557:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import extensions;
Line 561 ⟶ 564:
var counter := 1;
var makeItem := (item){ var retVal := counter.toPrintable() + separator + item +
^ makeItem("first") + makeItem("second") + makeItem("third")
Line 595 ⟶ 598:
IO.write Nested.makeList(". ")</syntaxhighlight>
{{out}}
<pre>
1. first
2. second
3. third
</pre>
=={{header|EMal}}==
<syntaxhighlight lang="emal">
fun makeList = List by text separator
int counter = 0
fun makeItem = text by text item
return ++counter + separator + item
end
return text[makeItem("first"), makeItem("second"), makeItem("third")]
end
for each text item in makeList(". ") do writeLine(item) end
</syntaxhighlight>
{{out}}
<pre>
Line 778 ⟶ 799:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Nested_function}}
'''Solution'''
[[File:Fōrmulæ - Nested function 01.png]]
[[File:Fōrmulæ - Nested function 02.png]]
[[File:Fōrmulæ - Nested function 03.png]]
=={{header|Go}}==
Line 1,014 ⟶ 1,039:
=={{header|Lambdatalk}}==
Lambdatalk has neither closures nor states
<syntaxhighlight lang="scheme">
{def makeItem▼
{lambda {:c :item}▼
{div}{A.first {A.set! 0 {+ {A.first :c} 1} :c}}: :item}}▼
{def makeList
▲ {def makeItem
{lambda {:s}
{S.map {{lambda {:
:s {A.new 0}}
-> makeList▼
first second third
▲-> makeList
{makeList
->
1
2
3
</syntaxhighlight>
Line 1,476 ⟶ 1,504:
3. third
</pre>
=={{header|RPL}}==
===Fully compliant===
The outer function creates the inner function, then deletes it at its last execution step.
The <code>\n</code> substring must be replaced by a <code>Newline</code> character when typing the code.
≪
≪ + OVER →STR SWAP + ROT SWAP + SWAP 1 + ≫ ''''MakeItem'''' STO
3 PICK "first\n" '''MakeItem''' 3 PICK "second\n" '''MakeItem''' 3 PICK "third\n" '''MakeItem'''
DROP SWAP DROP
''''MakeItem'''' PURGE
≫
‘'''MakeList'''’ STO
". " '''MakeList'''
{{out}}
<pre>
1: "1. first
2. second
3. third"
</pre>
===Unnamed nested functions===
It is more idiomatic in RPL to use unnamed nested functions, which allows the use of local variables and then increases code readability.
≪ "" 1
1 3 '''FOR''' j
3 PICK { "first\n" "second\n" "third\n" } j GET
→ sep item ≪ DUP →STR sep + item + ROT SWAP + SWAP 1 + ≫
'''NEXT'''
DROP SWAP DROP
≫
=={{header|Ruby}}==
Line 1,771 ⟶ 1,829:
=={{header|Wren}}==
<syntaxhighlight lang="
var counter = 0
var makeItem = Fn.new { |name|
|