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 56.0x :
<syntaxhighlight lang="elena">import extensions;
Line 561 ⟶ 564:
var counter := 1;
var makeItem := (item){ var retVal := counter.toPrintable() + separator + item + (forward newLine)newLineConstant; counter += 1; ^ retVal };
^ 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}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Nested function 01.png]]
In '''[https://formulae.org/?example=Nested_function this]''' page you can see the program(s) related to this task and their results.
 
[[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 ...but outsidewe can do that, thanks to mutability of arrays behaving as "sandbox" of mutability.
<syntaxhighlight lang="scheme">
{def makeItem
{lambda {:c :item}
{div}{A.first {A.set! 0 {+ {A.first :c} 1} :c}}: :item}}
-> makeItem
 
{def makeList
 
{def makeItem
{lambda {:cs :a :itemi}
{div}{A.first {A.set! 0 {+ {A.first :ca} 1} :ca}}:s :itemi}}
 
{lambda {:s}
{S.map {{lambda {:cs :a :i} {makeItem :cs :i}} {A.new 0}}a :si}}}
:s {A.new 0}}
-> makeList
first second third
}}} {
-> makeList
 
{makeList first second third.}
->
1:. first
2:. second
3:. third
</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
"" 1 {
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="ecmascriptwren">var makeList = Fn.new { |sep|
var counter = 0
var makeItem = Fn.new { |name|
9,485

edits