Closures/Value capture: Difference between revisions

m
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(2 intermediate revisions by 2 users not shown)
Line 649:
</syntaxhighlight>
=={{header|Elena}}==
ELENA 46.1x :
<syntaxhighlight lang="elena">import system'routines;
import extensions;
Line 655:
public program()
{
var functions := Array.allocate(10).populate::(int i => { ^ i * i} );
functions.forEach::(func) { console.printLine(func()) }
}</syntaxhighlight>
{{out}}
Line 1,034:
slist@.(?9) ''
25</syntaxhighlight>
 
===Using temporary locales===
The problem statement "Demonstrate how to create a series of independent closures based on the same template but maintain separate copies of the variable closed over" conflicts with the problem title "Value capture" in languages have sufficient abstraction to distinguish between value capture and variable and variable capture. This conflict even appears in J, and in general cases can require treatment of issues well outside the scope of this task.
 
Still, to address the task description, we should include a "variable capture" implementation, which in J could imply the use of "temporary [[j:Vocabulary/Locales#Summary_of_the_Locale_Mechanism|locales]]" despite the fact that this approach would not satisfy the "simplest fashion possible" requirement.
 
For example, we could define an adverb 'geni' which takes a base function (which in this case will be <code>*:</code> -- a function which squares an argument) and a value (which in this case will be an index), creates a locale where that value will be stored in a variable named <code>i</code> and then returns an anonymous function which takes a reference to the locale (rather than the value) and extracts the value from the locale to generate the result.
 
We'll also use J's nuvoc <code><nowiki>{{</nowiki></code> ... <code><nowiki>}}</nowiki></code> nesting definitional mechanism which implicitly determines the type of a definition instead of explicitly representing the definition types (<code>1 :</code>, <code>2 :</code>, <code>3 :</code>, ...) which discourages nesting blocks.
 
<syntaxhighlight lang=J>
geni=: {{
N=. cocreate''
i__N=. y
N
}}
task=: {{ u {{ u {{ u i__n [ y }} (geni y)`'' }}"0 i. y }}
</syntaxhighlight>
 
This would be really bad form if we were intending to be useful, but - as described above - this approach is somewhat relevant to the task requirements.
 
Example use:
<syntaxhighlight lang=J>
fns=: *: task 10
fns@.3 ''
9
fns@.5 ''
25
fns@.7 ''
49
</syntaxhighlight>
 
 
===Tacit (unorthodox) version===
In J only adverbs and conjunctions (functionals) can produce verbs (functions)... Unless they are forced to cloak as verbs; in this instance, the rank conjunction (“) cloaks as a dyadic verb. (NoteThis thatdoes not work in recent versions of J as this takes advantage of a bug/feature where the interpreter does not produce a result with [http://www.jsoftware.com/help/dictionary/dictb.htm the correct shape]):
 
<syntaxhighlight lang="j"> ( VL=. (<@:((<'"')(0:`)(,^:)&_))"0@:(^&2)@:i. 10 ) NB. Producing a list of boxed anonymous verbs (functions)
Line 1,047 ⟶ 1,079:
{::&VL 5 '' NB. Invoking the 6th verb with a dummy argument ('')
25</syntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|8+}}
6,951

edits