2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,11 +1,14 @@
'''Task:''' Create a list of 10 functions, in the simplest manner possible
(anonymous functions are encouraged), such that the function at index <math>i</math>
(you may choose to start <math>i</math> from either 0 or 1), when run,
should return the square of the index, that is, <math>i^2</math>.
Display the result of running any but the last function,
to demonstrate that the function indeed remembers its value.
;Task:
Create a list of ten functions, in the simplest manner possible &nbsp; (anonymous functions are encouraged), &nbsp; such that the function at index&nbsp;&nbsp;<big> ''<b> i </b>'' </big> &nbsp; (you may choose to start &nbsp; <big> ''<b> i </b>'' </big> &nbsp; from either &nbsp; <big> '''0''' </big> &nbsp; or &nbsp; <big> '''1'''), </big> &nbsp; when run, should return the square of the index, &nbsp; that is, &nbsp; <big> ''<b> i </b>'' <sup>2</sup>.</big>
Display the result of running any but the last function, to demonstrate that the function indeed remembers its value.
;Goal:
Demonstrate how to create a series of independent closures based on the same template but maintain separate copies of the variable closed over.
'''Goal:''' To demonstrate how to create a series of independent closures based on the same template but maintain separate copies of the variable closed over.
In imperative languages, one would generally use a loop with a mutable counter variable.
For each function to maintain the correct number, it has to capture the ''value''
of the variable at the time it was created, rather than just a reference to the variable, which would have a different value by the time the function was run.
For each function to maintain the correct number, it has to capture the ''value'' of the variable at the time it was created, rather than just a reference to the variable, which would have a different value by the time the function was run.
See also: [[Multiple distinct objects]]

View file

@ -0,0 +1,19 @@
on run
set fns to {}
repeat with i from 1 to 10
set end of fns to closure(i)
end repeat
lambda() of item 3 of fns
end run
on closure(x)
script
on lambda()
return x * x
end lambda
end script
end closure

View file

@ -0,0 +1,42 @@
on run
lambda() of (item 3 of (map(closure, range(1, 10))))
end run
on closure(x)
script
on lambda()
return x * x
end lambda
end script
end closure
-- GENERIC FUNCTIONS
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
script mf
property lambda : f
end script
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to mf's lambda(item i of xs, i, xs)
end repeat
return lst
end map
-- range :: Int -> Int -> Int
on range(m, n)
set lng to (n - m) + 1
set base to m - 1
set lst to {}
repeat with i from 1 to lng
set end of lst to i + base
end repeat
return lst
end range

View file

@ -0,0 +1,2 @@
(def funcs (map #(fn [] (* % %)) (range 11)))
(printf "%d\n%d\n" ((nth funcs 3)) ((nth funcs 4)))

View file

@ -0,0 +1,3 @@
#var list := Array new &length:10 set &every: (&index:i) [ [ ^ i * i. ] ].
console writeLine:(list@3 eval).

View file

@ -0,0 +1,2 @@
funs = for i <- 0..9, do: (fn -> i*i end)
Enum.each(funs, &IO.puts &1.())

View file

@ -0,0 +1,6 @@
: xt-array here { a }
10 cells allot 10 0 do
:noname i ]] literal dup * ; [[ a i cells + !
loop a ;
xt-array 5 cells + @ execute .

View file

@ -0,0 +1 @@
25

View file

@ -0,0 +1,2 @@
blist := list(0,1,2,3,4,5,6,7,8,9) map(i,block(i,block(i*i)) call(i))
writeln(blist at(3) call) // prints 9

View file

@ -0,0 +1,6 @@
"use strict";
let funcs = [];
for (let i = 0; i < 10; ++i) {
funcs.push((i => () => i*i)(i));
}
console.log(funcs[3]());

View file

@ -0,0 +1,21 @@
(function () {
'use strict';
// Int -> Int -> [Int]
function range(m, n) {
return Array.apply(null, Array(n - m + 1))
.map(function (x, i) {
return m + i;
});
}
var lstFns = range(0, 10)
.map(function (i) {
return function () {
return i * i;
};
})
return lstFns[3]();
})();

View file

@ -0,0 +1 @@
let funcs = [...Array(10).keys()].map(i => () => i*i);

View file

@ -0,0 +1,4 @@
function Get-Closure ([double]$Number)
{
{param([double]$Sum) return $script:Number *= $Sum}.GetNewClosure()
}

View file

@ -0,0 +1,9 @@
for ($i = 1; $i -lt 11; $i++)
{
$total = Get-Closure -Number $i
[PSCustomObject]@{
Function = $i
Sum = & $total -Sum $i
}
}

View file

@ -0,0 +1,11 @@
$numbers = 1..20 | Get-Random -Count 10
foreach ($number in $numbers)
{
$total = Get-Closure -Number $number
[PSCustomObject]@{
Function = $number
Sum = & $total -Sum $number
}
}

View file

@ -1,14 +1,13 @@
/*REXX pgm has a list of 10 functions, each returns its invocation(idx)²*/
/*REXX program has a list of ten functions, each returns its invocation (index) squared.*/
do j=1 for 9 /*invoke random functions 9 times.*/
interpret 'CALL .'random(0,9) /*invoke a randomly selected func.*/
end /*j*/ /* [↑] the random func has no args*/
do j=1 for 9; ?=random(0, 9) /*invoke random functions nine times.*/
interpret 'CALL .'? /*invoke a randomly selected function. */
end /*j*/ /* [↑] the called function has no args*/
say 'The tenth invocation of .0 ' .0()
exit /*stick a fork in it, we're done.*/
/*─────────────────────────────────list of 10 functions─────────────────*/
/*[Below is the closest thing to anonymous functions in the REXX lang.] */
.0:return .(); .1:return .(); .2:return .(); .3:return .(); .4:return .()
.5:return .(); .6:return .(); .7:return .(); .8:return .(); .9:return .()
/*─────────────────────────────────. function───────────────────────────*/
.: if symbol('@')=='LIT' then @=0 /*handle 1st invoke*/; @=@+1; return @*@
say 'The tenth invocation of .0 ' .0()
exit /*stick a fork in it, we're all done. */
/*───────────────────────────[Below is the closest thing to anonymous functions in REXX]*/
.0: return .(); .1: return .(); .2: return .(); .3: return .(); .4: return .()
.5: return .(); .6: return .(); .7: return .(); .8: return .(); .9: return .()
/*──────────────────────────────────────────────────────────────────────────────────────*/
.: if symbol('@')=="LIT" then @=0 /* ◄───handle very 1st invoke*/; @=@+1; return @*@

View file

@ -0,0 +1,2 @@
procs = Array.new(10){|i| ->{i*i} } # -> creates a lambda
p procs[7].call # => 49