First-class functions: Difference between revisions

Added standard functions as required by the task, one in second position of a sequence and one in first position which requires precising its type. Added a comment about this.
(Removed pragmas {.procvar.} and {.closure.} no longer needed.)
(Added standard functions as required by the task, one in second position of a sequence and one in first position which requires precising its type. Added a comment about this.)
Line 2,017:
 
=={{header|Nim}}==
{{trans|ES6}} 
 
<lang nim>from math import nil
Note that when defining a sequence <code>@[a, b, c]</code>, “a” defines the type of the elements. So, if there is an ambiguity, we need to precise the type.
 
For instance <code>@[math.sin, cos]</code> wouldn’t compile as there exists two “sin” functions, one for “float32” and one for “float64”.
 
So, we have to write either <code>@[(proc(x: float64): float64]) math.sin, cos]</code> to avoid the ambiguity or make sure there is no ambiguity as regards the first element.
 
Here, in first sequence, we put in first position our function “sin” defined only for “float64” and in second position the standard one “math.cos”. And in second sequence, we used the type <code>MF64 = proc(x: float64): float64</code> to suppress the ambiguity.
 
<lang nim>from math import nil # Require qualifier to access functions.
 
proctype cosMF64 = proc(x: float64) : float64 =
 
proc cube(x: float64) : float64 =
Line 2,033 ⟶ 2,044:
proc sin(x: float64) : float64 =
math.sin(x)
 
proc asin(x: float64) : float64 =
math.arcsin(x)
proc cos(x: float64) : float64 =
math.cos(x)
proc acos(x: float64) : float64 =
math.arccos(x)
 
var fun = @[sin, math.cos, cube]
var inv = @[asinMF64 math.arcsin, acos, cuberoot]
 
for i in 0..2:
echo $compose(inv[i], fun[i])(0.5)</lang>
Output:
<pre>0.5
Anonymous user