Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
4
Task/Closures-Value-capture/00-META.yaml
Normal file
4
Task/Closures-Value-capture/00-META.yaml
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
---
|
||||
category:
|
||||
- Functions and subroutines
|
||||
from: http://rosettacode.org/wiki/Closures/Value_capture
|
||||
14
Task/Closures-Value-capture/00-TASK.txt
Normal file
14
Task/Closures-Value-capture/00-TASK.txt
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
;Task:
|
||||
Create a list of ten functions, in the simplest manner possible (anonymous functions are encouraged), such that the function at index <big> ''<b> i </b>'' </big> (you may choose to start <big> ''<b> i </b>'' </big> from either <big> '''0''' </big> or <big> '''1'''), </big> when run, should return the square of the index, that is, <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.
|
||||
|
||||
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.
|
||||
|
||||
See also: [[Multiple distinct objects]]
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
[(() -> Int)] funcs
|
||||
L(i) 10
|
||||
funcs.append(() -> @=i * @=i)
|
||||
print(funcs[3]())
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
[1:10]PROC(BOOL)INT squares;
|
||||
|
||||
FOR i FROM 1 TO 10 DO
|
||||
HEAP INT captured i := i;
|
||||
squares[i] := ((REF INT by ref i, INT by val i,BOOL b)INT:(INT i = by ref i; (b|by ref i := 0); by val i*i))
|
||||
(captured i, captured i,)
|
||||
OD;
|
||||
|
||||
FOR i FROM 1 TO 8 DO print(squares[i](i MOD 2 = 0)) OD;
|
||||
print(new line);
|
||||
FOR i FROM 1 TO 10 DO print(squares[i](FALSE)) OD
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
fns: {n: x; {n expt 2}} map range[10]
|
||||
(8 elem fns)[]
|
||||
30
Task/Closures-Value-capture/Ada/closures-value-capture.ada
Normal file
30
Task/Closures-Value-capture/Ada/closures-value-capture.ada
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
with Ada.Text_IO;
|
||||
|
||||
procedure Value_Capture is
|
||||
|
||||
protected type Fun is -- declaration of the type of a protected object
|
||||
entry Init(Index: Natural);
|
||||
function Result return Natural;
|
||||
private
|
||||
N: Natural := 0;
|
||||
end Fun;
|
||||
|
||||
protected body Fun is -- the implementation of a protected object
|
||||
entry Init(Index: Natural) when N=0 is
|
||||
begin -- after N has been set to a nonzero value, it cannot be changed any more
|
||||
N := Index;
|
||||
end Init;
|
||||
function Result return Natural is (N*N);
|
||||
end Fun;
|
||||
|
||||
A: array (1 .. 10) of Fun; -- an array holding 10 protected objects
|
||||
|
||||
begin
|
||||
for I in A'Range loop -- initialize the protected objects
|
||||
A(I).Init(I);
|
||||
end loop;
|
||||
|
||||
for I in A'First .. A'Last-1 loop -- evaluate the functions, except for the last
|
||||
Ada.Text_IO.Put(Integer'Image(A(I).Result));
|
||||
end loop;
|
||||
end Value_Capture;
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
on run
|
||||
set fns to {}
|
||||
|
||||
repeat with i from 1 to 10
|
||||
set end of fns to closure(i)
|
||||
end repeat
|
||||
|
||||
|λ|() of item 3 of fns
|
||||
end run
|
||||
|
||||
on closure(x)
|
||||
script
|
||||
on |λ|()
|
||||
x * x
|
||||
end |λ|
|
||||
end script
|
||||
end closure
|
||||
|
|
@ -0,0 +1,54 @@
|
|||
-- CLOSURE --------------------------------------------------------------------
|
||||
|
||||
script closure
|
||||
on |λ|(x)
|
||||
script
|
||||
on |λ|()
|
||||
x * x
|
||||
end |λ|
|
||||
end script
|
||||
end |λ|
|
||||
end script
|
||||
|
||||
|λ|() of (item 3 of (map(closure, enumFromTo(1, 10))))
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS ----------------------------------------------------------
|
||||
|
||||
-- enumFromTo :: Int -> Int -> [Int]
|
||||
on enumFromTo(m, n)
|
||||
if n < m then
|
||||
set d to -1
|
||||
else
|
||||
set d to 1
|
||||
end if
|
||||
set lst to {}
|
||||
repeat with i from m to n by d
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
end enumFromTo
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to |λ|(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property |λ| : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
funcs: [ø]
|
||||
|
||||
loop 1..10 'f ->
|
||||
'funcs ++ function [] with 'f [
|
||||
f * f
|
||||
]
|
||||
|
||||
print call funcs\3 []
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
)abbrev package TESTP TestPackage
|
||||
TestPackage() : with
|
||||
test: () -> List((()->Integer))
|
||||
== add
|
||||
test() == [(() +-> i^2) for i in 1..10]
|
||||
|
|
@ -0,0 +1 @@
|
|||
[x() for x in test()]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
[1,4,9,16,25,36,49,64,81,100]
|
||||
Type: List(Integer)
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
((main {
|
||||
{ iter
|
||||
1 take bons 1 take
|
||||
dup cp
|
||||
{*} cp
|
||||
3 take
|
||||
append }
|
||||
10 times
|
||||
collect !
|
||||
{eval %d nl <<} each }))
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
100
|
||||
81
|
||||
64
|
||||
49
|
||||
36
|
||||
25
|
||||
16
|
||||
9
|
||||
4
|
||||
1
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
( -1:?i
|
||||
& :?funcs
|
||||
& whl
|
||||
' ( 1+!i:<10:?i
|
||||
& !funcs ()'(.$i^2):?funcs
|
||||
)
|
||||
& whl'(!funcs:%?func %?funcs&out$(!func$))
|
||||
);
|
||||
12
Task/Closures-Value-capture/C++/closures-value-capture.cpp
Normal file
12
Task/Closures-Value-capture/C++/closures-value-capture.cpp
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
#include <iostream>
|
||||
#include <functional>
|
||||
#include <vector>
|
||||
|
||||
int main() {
|
||||
std::vector<std::function<int()> > funcs;
|
||||
for (int i = 0; i < 10; i++)
|
||||
funcs.push_back([=]() { return i * i; });
|
||||
for ( std::function<int( )> f : funcs )
|
||||
std::cout << f( ) << std::endl ;
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
var captor = (Func<int, Func<int>>)(number => () => number * number);
|
||||
var functions = Enumerable.Range(0, 10).Select(captor);
|
||||
foreach (var function in functions.Take(9))
|
||||
{
|
||||
Console.WriteLine(function());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
0
|
||||
1
|
||||
4
|
||||
9
|
||||
16
|
||||
25
|
||||
36
|
||||
49
|
||||
64
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
class Program
|
||||
{
|
||||
static void Main( string[] args )
|
||||
{
|
||||
List<Func<int>> l = new List<Func<int>>();
|
||||
for ( int i = 0; i < 10; ++i )
|
||||
{
|
||||
// This is key to avoiding the closure trap, because
|
||||
// the anonymous delegate captures a reference to
|
||||
// outer variables, not their value. So we create 10
|
||||
// variables, and each created anonymous delegate
|
||||
// has references to that variable, not the loop variable
|
||||
var captured_val = i;
|
||||
l.Add( delegate() { return captured_val * captured_val; } );
|
||||
}
|
||||
|
||||
l.ForEach( delegate( Func<int> f ) { Console.WriteLine( f() ); } );
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
0
|
||||
1
|
||||
4
|
||||
9
|
||||
16
|
||||
25
|
||||
36
|
||||
49
|
||||
64
|
||||
41
Task/Closures-Value-capture/C/closures-value-capture-1.c
Normal file
41
Task/Closures-Value-capture/C/closures-value-capture-1.c
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
|
||||
typedef int (*f_int)();
|
||||
|
||||
#define TAG 0xdeadbeef
|
||||
int _tmpl() {
|
||||
volatile int x = TAG;
|
||||
return x * x;
|
||||
}
|
||||
|
||||
#define PROT (PROT_EXEC | PROT_WRITE)
|
||||
#define FLAGS (MAP_PRIVATE | MAP_ANONYMOUS)
|
||||
f_int dupf(int v)
|
||||
{
|
||||
size_t len = (void*)dupf - (void*)_tmpl;
|
||||
f_int ret = mmap(NULL, len, PROT, FLAGS, 0, 0);
|
||||
char *p;
|
||||
if(ret == MAP_FAILED) {
|
||||
perror("mmap");
|
||||
exit(-1);
|
||||
}
|
||||
memcpy(ret, _tmpl, len);
|
||||
for (p = (char*)ret; p < (char*)ret + len - sizeof(int); p++)
|
||||
if (*(int *)p == TAG) *(int *)p = v;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
f_int funcs[10];
|
||||
int i;
|
||||
for (i = 0; i < 10; i++) funcs[i] = dupf(i);
|
||||
|
||||
for (i = 0; i < 9; i++)
|
||||
printf("func[%d]: %d\n", i, funcs[i]());
|
||||
|
||||
return 0;
|
||||
}
|
||||
9
Task/Closures-Value-capture/C/closures-value-capture-2.c
Normal file
9
Task/Closures-Value-capture/C/closures-value-capture-2.c
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
func[0]: 0
|
||||
func[1]: 1
|
||||
func[2]: 4
|
||||
func[3]: 9
|
||||
func[4]: 16
|
||||
func[5]: 25
|
||||
func[6]: 36
|
||||
func[7]: 49
|
||||
func[8]: 64
|
||||
33
Task/Closures-Value-capture/C/closures-value-capture-3.c
Normal file
33
Task/Closures-Value-capture/C/closures-value-capture-3.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
void init(void)
|
||||
{
|
||||
t = intern(lit("t"));
|
||||
x = intern(lit("x"));
|
||||
}
|
||||
|
||||
val square(val env)
|
||||
{
|
||||
val xbind = assoc(env, x); /* look up binding of variable x in env */
|
||||
val xval = cdr(xbind); /* value is the cdr of the binding cell */
|
||||
return num(cnum(xval) * cnum(xval));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
val funlist = nil, iter;
|
||||
|
||||
init();
|
||||
|
||||
for (i = 0; i < 10; i++) {
|
||||
val closure_env = cons(cons(x, num(i)), nil);
|
||||
funlist = cons(func_f0(closure_env, square), funlist);
|
||||
}
|
||||
|
||||
for (iter = funlist; iter != nil; iter = cdr(iter)) {
|
||||
val fun = car(iter);
|
||||
val square = funcall(fun, nao);
|
||||
|
||||
printf("%d\n", cnum(square));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
shared void run() {
|
||||
|
||||
//create a list of closures with a list comprehension
|
||||
value closures = [for(i in 0:10) () => i ^ 2];
|
||||
|
||||
for(i->closure in closures.indexed) {
|
||||
print("closure number ``i`` returns: ``closure()``");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
(def funcs (map #(fn [] (* % %)) (range 11)))
|
||||
(printf "%d\n%d\n" ((nth funcs 3)) ((nth funcs 4)))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
# Generate an array of functions.
|
||||
funcs = ( for i in [ 0...10 ] then do ( i ) -> -> i * i )
|
||||
|
||||
# Call each function to demonstrate value capture.
|
||||
console.log func() for func in funcs
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
CL-USER> (defparameter alist
|
||||
(loop for i from 1 to 10
|
||||
collect (cons i (let ((i i))
|
||||
(lambda () (* i i))))))
|
||||
ALIST
|
||||
CL-USER> (funcall (cdr (assoc 2 alist)))
|
||||
4
|
||||
CL-USER> (funcall (cdr (assoc 8 alist)))
|
||||
64
|
||||
10
Task/Closures-Value-capture/D/closures-value-capture-1.d
Normal file
10
Task/Closures-Value-capture/D/closures-value-capture-1.d
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import std.stdio;
|
||||
|
||||
void main() {
|
||||
int delegate()[] funcs;
|
||||
|
||||
foreach (i; 0 .. 10)
|
||||
funcs ~= (i => () => i ^^ 2)(i);
|
||||
|
||||
writeln(funcs[3]());
|
||||
}
|
||||
5
Task/Closures-Value-capture/D/closures-value-capture-2.d
Normal file
5
Task/Closures-Value-capture/D/closures-value-capture-2.d
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
void main() {
|
||||
import std.stdio, std.range, std.algorithm;
|
||||
|
||||
10.iota.map!(i => () => i ^^ 2).map!q{ a() }.writeln;
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
program Project1;
|
||||
|
||||
type
|
||||
TFuncIntResult = reference to function: Integer;
|
||||
|
||||
// use function that returns anonymous method to avoid capturing the loop variable
|
||||
function CreateFunc(i: Integer): TFuncIntResult;
|
||||
begin
|
||||
Result :=
|
||||
function: Integer
|
||||
begin
|
||||
Result := i * i;
|
||||
end;
|
||||
end;
|
||||
|
||||
var
|
||||
Funcs: array[0..9] of TFuncIntResult;
|
||||
i: integer;
|
||||
begin
|
||||
// create 10 anonymous functions
|
||||
for i := Low(Funcs) to High(Funcs) do
|
||||
Funcs[i] := CreateFunc(i);
|
||||
|
||||
// call all 10 functions
|
||||
for i := Low(Funcs) to High(Funcs) do
|
||||
Writeln(Funcs[i]());
|
||||
end.
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
var xs = []
|
||||
let num = 10
|
||||
|
||||
for n in 0..<num {
|
||||
xs.Add((n => () => n * n)(n))
|
||||
}
|
||||
|
||||
for x in xs {
|
||||
print(x())
|
||||
}
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(define (fgen i) (lambda () (* i i)))
|
||||
(define fs (for/vector ((i 10)) (fgen i))) ;; vector of 10 anonymous functions
|
||||
((vector-ref fs 5)) ;; calls fs[5]
|
||||
→ 25
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import system'routines;
|
||||
import extensions;
|
||||
|
||||
public program()
|
||||
{
|
||||
var functions := Array.allocate(10).populate:(int i => {^ i * i} );
|
||||
|
||||
functions.forEach:(func) { console.printLine(func()) }
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
funs = for i <- 0..9, do: (fn -> i*i end)
|
||||
Enum.each(funs, &IO.puts &1.())
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
;; -*- lexical-binding: t; -*-
|
||||
(mapcar #'funcall
|
||||
(mapcar (lambda (x)
|
||||
(lambda ()
|
||||
(* x x)))
|
||||
'(1 2 3 4 5 6 7 8 9 10)))
|
||||
;; => (1 4 9 16 25 36 49 64 81 100)
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
-module(capture_demo).
|
||||
-export([demo/0]).
|
||||
|
||||
demo() ->
|
||||
Funs = lists:map(fun (X) ->
|
||||
fun () ->
|
||||
X * X
|
||||
end
|
||||
end,
|
||||
lists:seq(1,10)),
|
||||
lists:foreach(fun (F) ->
|
||||
io:fwrite("~B~n",[F()])
|
||||
end, Funs).
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
[<EntryPoint>]
|
||||
let main argv =
|
||||
let fs = List.init 10 (fun i -> fun () -> i*i)
|
||||
do List.iter (fun f -> printfn "%d" <| f()) fs
|
||||
0
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
[<EntryPoint>]
|
||||
let main argv =
|
||||
let fs = List.map (fun i -> fun () -> i*i) [0..9]
|
||||
do List.iter (fun f -> printfn "%d" <| f()) fs
|
||||
0
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
[<EntryPoint>]
|
||||
let main argv =
|
||||
let fs = List.mapi (fun i x -> fun () -> i*i) (List.replicate 10 None)
|
||||
do List.iter (fun f -> printfn "%d" <| f()) fs
|
||||
0
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
[<EntryPoint>]
|
||||
let main argv =
|
||||
let fs = Seq.initInfinite (fun i -> fun () -> i*i)
|
||||
do Seq.iter (fun f -> printfn "%d" <| f()) (Seq.take 10 fs)
|
||||
0
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
USING: io kernel locals math prettyprint sequences ;
|
||||
|
||||
[let
|
||||
! Create a sequence of 10 quotations
|
||||
10 iota [
|
||||
:> i ! Bind lexical variable i
|
||||
[ i i * ] ! Push a quotation to calculate i squared
|
||||
] map :> seq
|
||||
|
||||
{ 3 8 } [
|
||||
dup pprint " squared is " write
|
||||
seq nth call .
|
||||
] each
|
||||
]
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
USING: fry io kernel math prettyprint sequences ;
|
||||
|
||||
! Push a sequence of 10 quotations
|
||||
10 iota [
|
||||
'[ _ dup * ] ! Push a quotation ( i -- i*i )
|
||||
] map
|
||||
|
||||
{ 3 8 } [
|
||||
dup pprint " squared is " write
|
||||
over nth call .
|
||||
] each
|
||||
drop
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
class Closures
|
||||
{
|
||||
Void main ()
|
||||
{
|
||||
// define a list of functions, which take no arguments and return an Int
|
||||
|->Int|[] functions := [,]
|
||||
|
||||
// create and store a function which returns i*i for i in 0 to 10
|
||||
(0..10).each |Int i|
|
||||
{
|
||||
functions.add (|->Int| { i*i })
|
||||
}
|
||||
|
||||
// show result of calling function at index position 7
|
||||
echo ("Function at index: " + 7 + " outputs " + functions[7].call)
|
||||
}
|
||||
}
|
||||
|
|
@ -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 .
|
||||
|
|
@ -0,0 +1 @@
|
|||
25
|
||||
|
|
@ -0,0 +1,33 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Type Closure
|
||||
Private:
|
||||
index As Integer
|
||||
Public:
|
||||
Declare Constructor(index As Integer = 0)
|
||||
Declare Function Square As Integer
|
||||
End Type
|
||||
|
||||
Constructor Closure(index As Integer = 0)
|
||||
This.index = index
|
||||
End Constructor
|
||||
|
||||
Function Closure.Square As Integer
|
||||
Return index * index
|
||||
End Function
|
||||
|
||||
Dim a(1 To 10) As Closure
|
||||
|
||||
' create Closure objects which capture their index
|
||||
For i As Integer = 1 To 10
|
||||
a(i) = Closure(i)
|
||||
Next
|
||||
|
||||
' call the Square method on all but the last object
|
||||
For i As Integer = 1 to 9
|
||||
Print a(i).Square
|
||||
Next
|
||||
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
15
Task/Closures-Value-capture/Go/closures-value-capture.go
Normal file
15
Task/Closures-Value-capture/Go/closures-value-capture.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
fs := make([]func() int, 10)
|
||||
for i := range fs {
|
||||
i := i
|
||||
fs[i] = func() int {
|
||||
return i * i
|
||||
}
|
||||
}
|
||||
fmt.Println("func #0:", fs[0]())
|
||||
fmt.Println("func #3:", fs[3]())
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
def closures = (0..9).collect{ i -> { -> i*i } }
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
assert closures instanceof List
|
||||
assert closures.size() == 10
|
||||
closures.each { assert it instanceof Closure }
|
||||
println closures[7]()
|
||||
|
|
@ -0,0 +1 @@
|
|||
fs = map (\i _ -> i * i) [1 .. 10]
|
||||
|
|
@ -0,0 +1 @@
|
|||
fs = [const $ i * i | i <- [1 .. 10]]
|
||||
|
|
@ -0,0 +1 @@
|
|||
fs = take 10 coFs where coFs = [const $ i * i | i <- [1 ..]]
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
> :t fs
|
||||
fs :: [b -> Integer]
|
||||
> map ($ ()) fs
|
||||
[1,4,9,16,25,36,49,64,81,100]
|
||||
> fs !! 9 $ ()
|
||||
100
|
||||
> fs !! 8 $ undefined
|
||||
81
|
||||
15
Task/Closures-Value-capture/Icon/closures-value-capture.icon
Normal file
15
Task/Closures-Value-capture/Icon/closures-value-capture.icon
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
procedure main(args) # Closure/Variable Capture
|
||||
every put(L := [], vcapture(1 to 10)) # build list of index closures
|
||||
write("Randomly selecting L[",i := ?*L,"] = ",L[i]()) # L[i]() calls the closure
|
||||
end
|
||||
|
||||
# The anonymous 'function', as a co-expression. Most of the code is standard
|
||||
# boilerplate needed to use a co-expression as an anonymous function.
|
||||
|
||||
procedure vcapture(x) # vcapture closes over its argument
|
||||
return makeProc { repeat { (x[1]^2) @ &source } }
|
||||
end
|
||||
|
||||
procedure makeProc(A) # the makeProc PDCO from the UniLib Utils package
|
||||
return (@A[1], A[1])
|
||||
end
|
||||
2
Task/Closures-Value-capture/Io/closures-value-capture.io
Normal file
2
Task/Closures-Value-capture/Io/closures-value-capture.io
Normal 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
|
||||
3
Task/Closures-Value-capture/J/closures-value-capture-1.j
Normal file
3
Task/Closures-Value-capture/J/closures-value-capture-1.j
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
constF=:3 :0
|
||||
{.''`(y "_)
|
||||
)
|
||||
2
Task/Closures-Value-capture/J/closures-value-capture-2.j
Normal file
2
Task/Closures-Value-capture/J/closures-value-capture-2.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
flist=: constF"0 i.10
|
||||
slist=: constF"0 *:i.10
|
||||
4
Task/Closures-Value-capture/J/closures-value-capture-3.j
Normal file
4
Task/Closures-Value-capture/J/closures-value-capture-3.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
flist @.3
|
||||
3"_
|
||||
slist @.3
|
||||
9"_
|
||||
4
Task/Closures-Value-capture/J/closures-value-capture-4.j
Normal file
4
Task/Closures-Value-capture/J/closures-value-capture-4.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
flist @.4''
|
||||
4
|
||||
slist @.4''
|
||||
16
|
||||
4
Task/Closures-Value-capture/J/closures-value-capture-5.j
Normal file
4
Task/Closures-Value-capture/J/closures-value-capture-5.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
flist@.(?9) ''
|
||||
7
|
||||
slist@.(?9) ''
|
||||
25
|
||||
9
Task/Closures-Value-capture/J/closures-value-capture-6.j
Normal file
9
Task/Closures-Value-capture/J/closures-value-capture-6.j
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
( VL=. (<@:((<'"')(0:`)(,^:)&_))"0@:(^&2)@:i. 10 ) NB. Producing a list of boxed anonymous verbs (functions)
|
||||
┌───┬───┬───┬───┬────┬────┬────┬────┬────┬────┐
|
||||
│0"_│1"_│4"_│9"_│16"_│25"_│36"_│49"_│64"_│81"_│
|
||||
└───┴───┴───┴───┴────┴────┴────┴────┴────┴────┘
|
||||
|
||||
{::&VL 5 NB. Evoking the 6th verb (function)
|
||||
25"_
|
||||
{::&VL 5 '' NB. Invoking the 6th verb with a dummy argument ('')
|
||||
25
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
import java.util.function.Supplier;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class ValueCapture {
|
||||
public static void main(String[] args) {
|
||||
ArrayList<Supplier<Integer>> funcs = new ArrayList<>();
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int j = i;
|
||||
funcs.add(() -> j * j);
|
||||
}
|
||||
|
||||
Supplier<Integer> foo = funcs.get(3);
|
||||
System.out.println(foo.get()); // prints "9"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
import java.util.List;
|
||||
import java.util.function.IntSupplier;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
public interface ValueCapture {
|
||||
public static void main(String... arguments) {
|
||||
List<IntSupplier> closures = IntStream.rangeClosed(0, 10)
|
||||
.<IntSupplier>mapToObj(i -> () -> i * i)
|
||||
.collect(toList())
|
||||
;
|
||||
|
||||
IntSupplier closure = closures.get(3);
|
||||
System.out.println(closure.getAsInt()); // prints "9"
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
var funcs = [];
|
||||
for (var i = 0; i < 10; i++) {
|
||||
funcs.push( (function(i) {
|
||||
return function() { return i * i; }
|
||||
})(i) );
|
||||
}
|
||||
window.alert(funcs[3]()); // alerts "9"
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
<script type="application/javascript;version=1.7">
|
||||
var funcs = [];
|
||||
for (var i = 0; i < 10; i++) {
|
||||
let (i = i) {
|
||||
funcs.push( function() { return i * i; } );
|
||||
}
|
||||
}
|
||||
window.alert(funcs[3]()); // alerts "9"
|
||||
</script>
|
||||
|
|
@ -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]());
|
||||
|
|
@ -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]();
|
||||
|
||||
})();
|
||||
|
|
@ -0,0 +1 @@
|
|||
let funcs = [...Array(10).keys()].map(i => () => i*i);
|
||||
|
|
@ -0,0 +1 @@
|
|||
funcs = [ () -> i^2 for i = 1:10 ]
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
// create an array of 10 anonymous functions which return the square of their index
|
||||
val funcs = Array(10){ fun(): Int = it * it }
|
||||
// call all but the last
|
||||
(0 .. 8).forEach { println(funcs[it]()) }
|
||||
}
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
> (set funcs (list-comp ((<- m (lists:seq 1 10)))
|
||||
(lambda () (math:pow m 2))))
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
(#Fun<lfe_eval.23.101079464> #Fun<lfe_eval.23.101079464>
|
||||
#Fun<lfe_eval.23.101079464> #Fun<lfe_eval.23.101079464>
|
||||
#Fun<lfe_eval.23.101079464> #Fun<lfe_eval.23.101079464>
|
||||
#Fun<lfe_eval.23.101079464> #Fun<lfe_eval.23.101079464>
|
||||
#Fun<lfe_eval.23.101079464> #Fun<lfe_eval.23.101079464>)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
> (funcall (car funcs))
|
||||
1.0
|
||||
> (funcall (cadr funcs))
|
||||
4.0
|
||||
> (funcall (cadddr funcs))
|
||||
16.0
|
||||
> (funcall (lists:nth 8 funcs))
|
||||
64.0
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
{def A
|
||||
{A.new
|
||||
{S.map {lambda {:x} {* :x :x}}
|
||||
{S.serie 0 10}
|
||||
}}}
|
||||
|
||||
{A.get 3 {A}} // equivalent to A[3]
|
||||
-> 9
|
||||
{A.get 4 {A}}
|
||||
-> 16
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
functions := 10 times to (Array) map {
|
||||
takes '[i].
|
||||
proc { (i) * (i). }.
|
||||
}.
|
||||
|
||||
functions visit { println: $1 call. }.
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
-- parent script "CallFunction"
|
||||
|
||||
property _code
|
||||
|
||||
-- if the function is supposed to return something, the code must contain a line that starts with "res="
|
||||
on new (me, code)
|
||||
me._code = code
|
||||
return me
|
||||
end
|
||||
|
||||
on call (me)
|
||||
----------------------------------------
|
||||
-- If custom arguments were passed, evaluate them in the current context.
|
||||
-- Note: in the code passed to the constructor they have to be referenced
|
||||
-- as arg[1], arg[2], ...
|
||||
arg = []
|
||||
repeat with i = 3 to the paramCount
|
||||
arg[i-2] = param(i)
|
||||
end repeat
|
||||
----------------------------------------
|
||||
res = VOID
|
||||
do(me._code)
|
||||
return res
|
||||
end
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
funcs = []
|
||||
repeat with i = 1 to 10
|
||||
code = "res="&i&"*"&i
|
||||
funcs[i] = script("CallFunction").new(code)
|
||||
end repeat
|
||||
|
||||
put call(funcs[3], _movie)
|
||||
-- 9
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
funcs = []
|
||||
repeat with i = 1 to 10
|
||||
code = ""
|
||||
put "res = "&i&"*"&i &RETURN after code
|
||||
put "repeat with i = 1 to arg.count" &RETURN after code
|
||||
put " res = res + arg[i]" &RETURN after code
|
||||
put "end repeat" after code
|
||||
funcs[i] = script("CallFunction").new(code)
|
||||
end repeat
|
||||
|
||||
put call(funcs[3], _movie, 23)
|
||||
-- 32
|
||||
|
||||
put call(funcs[7], _movie, 4, 5, 6)
|
||||
-- 64
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
:- object(value_capture).
|
||||
|
||||
:- public(show/0).
|
||||
show :-
|
||||
integer::sequence(1, 10, List),
|
||||
meta::map(create_closure, List, Closures),
|
||||
meta::map(call_closure, List, Closures).
|
||||
|
||||
create_closure(Index, [Double]>>(Double is Index*Index)).
|
||||
|
||||
call_closure(Index, Closure) :-
|
||||
call(Closure, Result),
|
||||
write('Closure '), write(Index), write(' : '), write(Result), nl.
|
||||
|
||||
:- end_object.
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
| ?- value_capture::show.
|
||||
Closure 1 : 1
|
||||
Closure 2 : 4
|
||||
Closure 3 : 9
|
||||
Closure 4 : 16
|
||||
Closure 5 : 25
|
||||
Closure 6 : 36
|
||||
Closure 7 : 49
|
||||
Closure 8 : 64
|
||||
Closure 9 : 81
|
||||
Closure 10 : 100
|
||||
yes
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
funcs={}
|
||||
for i=1,10 do
|
||||
table.insert(funcs, function() return i*i end)
|
||||
end
|
||||
funcs[2]()
|
||||
funcs[3]()
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
Dim Base 0, A(10)
|
||||
For i=0 to 9 {
|
||||
a(i)=lambda i -> i**2
|
||||
}
|
||||
For i=0 to 9 {
|
||||
Print a(i)()
|
||||
}
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
document a$
|
||||
For i=0 to 9 {
|
||||
a$=format$("{0:0:-20}",a(i)())+{
|
||||
}
|
||||
}
|
||||
Clipboard a$
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
Inventory Alfa
|
||||
For i=0 to 9 {
|
||||
Append Alfa, i:=lambda i -> i**2
|
||||
}
|
||||
For i=0 to 9 {
|
||||
Print Alfa(i)()
|
||||
}
|
||||
|
||||
Beta=Stack
|
||||
Stack Beta {
|
||||
For i=0 to 9 {
|
||||
Data lambda i -> i**2
|
||||
}
|
||||
}
|
||||
Def Fun(X)=X()
|
||||
\\ reading functions from position 1 to 10
|
||||
For i=0 to 9 {
|
||||
Print fun(stackitem(Beta,i+1))
|
||||
}
|
||||
\\ pop functions form stack Beta
|
||||
Stack Beta {
|
||||
While not empty {
|
||||
Read M
|
||||
Print M()
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
> L := map( i -> (() -> i^2), [seq](1..10) ):
|
||||
> seq( L[i](),i=1..10);
|
||||
1, 4, 9, 16, 25, 36, 49, 64, 81, 100
|
||||
> L[4]();
|
||||
16
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Function[i, i^2 &] /@ Range@10
|
||||
->{1^2 &, 2^2 &, 3^2 &, 4^2 &, 5^2 &, 6^2 &, 7^2 &, 8^2 &, 9^2 &, 10^2 &}
|
||||
|
||||
%[[2]][]
|
||||
->4
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
using System.Console;
|
||||
|
||||
module Closures
|
||||
{
|
||||
Main() : void
|
||||
{
|
||||
def f(x) { fun() { x ** 2 } }
|
||||
def funcs = $[f(x) | x in $[0 .. 10]].ToArray(); // using array for easy indexing
|
||||
|
||||
WriteLine($"$(funcs[4]())");
|
||||
WriteLine($"$(funcs[2]())");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
var funcs: seq[proc(): int] = @[]
|
||||
|
||||
for i in 0..9:
|
||||
(proc =
|
||||
let x = i
|
||||
funcs.add(proc (): int = x * x))()
|
||||
|
||||
for i in 0..8:
|
||||
echo "func[", i, "]: ", funcs[i]()
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
let () =
|
||||
let cls = Array.init 10 (fun i -> (function () -> i * i)) in
|
||||
Random.self_init ();
|
||||
for i = 1 to 6 do
|
||||
let x = Random.int 9 in
|
||||
Printf.printf " fun.(%d) = %d\n" x (cls.(x) ());
|
||||
done
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
use Collection.Generic;
|
||||
|
||||
class Capture {
|
||||
function : Main(args : String[]) ~ Nil {
|
||||
funcs := Vector->New()<FuncHolder<IntHolder> >;
|
||||
|
||||
for(i := 0; i < 10; i += 1;) {
|
||||
funcs->AddBack(FuncHolder->New(\() ~ IntHolder : () => i * i)<IntHolder>);
|
||||
};
|
||||
|
||||
each(i : funcs) {
|
||||
func := funcs->Get(i)->Get()<IntHolder>;
|
||||
func()->Get()->PrintLine();
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
NSMutableArray *funcs = [[NSMutableArray alloc] init];
|
||||
for (int i = 0; i < 10; i++) {
|
||||
[funcs addObject:[^ { return i * i; } copy]];
|
||||
}
|
||||
|
||||
int (^foo)(void) = funcs[3];
|
||||
NSLog(@"%d", foo()); // logs "9"
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
: newClosure(i) #[ i sq ] ;
|
||||
10 seq map(#newClosure) at(7) perform .
|
||||
|
|
@ -0,0 +1 @@
|
|||
vector(10,i,()->i^2)[5]()
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
$funcs = array();
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$funcs[] = function () use ($i) { return $i * $i; };
|
||||
}
|
||||
echo $funcs[3](), "\n"; // prints 9
|
||||
?>
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
$funcs = array();
|
||||
for ($i = 0; $i < 10; $i++) {
|
||||
$funcs[] = create_function('', '$i = ' . var_export($i, true) . '; return $i * $i;');
|
||||
}
|
||||
echo $funcs[3](), "\n"; // prints 9
|
||||
?>
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
my @f = map(sub { $_ * $_ }, 0 .. 9); # @f is an array of subs
|
||||
print $f[$_](), "\n" for (0 .. 8); # call and print all but last
|
||||
|
|
@ -0,0 +1,38 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #000080;font-style:italic;">-- First some generic handling stuff, handles partial_args
|
||||
-- of any mixture of any length and element types.</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">closures</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">add_closure</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">partial_args</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">closures</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">closures</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">rid</span><span style="color: #0000FF;">,</span><span style="color: #000000;">partial_args</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">closures</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (return an integer id)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">call_closure</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">id</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">args</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #004080;">integer</span> <span style="color: #000000;">rid</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">partial_args</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">closures</span><span style="color: #0000FF;">[</span><span style="color: #000000;">id</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">call_func</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rid</span><span style="color: #0000FF;">,</span><span style="color: #000000;">partial_args</span><span style="color: #0000FF;">&</span><span style="color: #000000;">args</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- The test routine to be made into a closure, or ten
|
||||
-- Note that all external references/captured variables must
|
||||
-- be passed as arguments, and grouped together on the lhs</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">square</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #000080;font-style:italic;">-- Create the ten closures as asked for.
|
||||
-- Here, cids is just {1,2,3,4,5,6,7,8,9,10}, however ids would be more
|
||||
-- useful for a mixed bag of closures, possibly stored all over the shop.
|
||||
-- Likewise add_closure could have been a procedure for this demo, but
|
||||
-- you would probably want the function in a real-world application.</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cids</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000080;font-style:italic;">--for i=11 to 20 do -- alternative test</span>
|
||||
<span style="color: #000000;">cids</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">add_closure</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">routine_id</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"square"</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">i</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000080;font-style:italic;">-- And finally call em (this loop is blissfully unaware what function
|
||||
-- it is actually calling, and what partial_arguments it is passing)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">call_closure</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cids</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],{}))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">square</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">tid</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"i"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tid</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (setd valid here too)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">*</span><span style="color: #000000;">i</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">tids</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000080;font-style:italic;">--for i=11 to 20 do</span>
|
||||
<span style="color: #000000;">tids</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">({{</span><span style="color: #008000;">"i"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">}})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">10</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">square</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tids</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<!--
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
def power2
|
||||
dup *
|
||||
enddef
|
||||
|
||||
getid power2 10 repeat
|
||||
|
||||
len for
|
||||
dup rot swap get rot swap exec print " " print
|
||||
endfor
|
||||
|
||||
nl
|
||||
|
||||
/# Another mode #/
|
||||
len for
|
||||
var i
|
||||
i get i swap exec print " " print
|
||||
endfor
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(setq FunList
|
||||
(make
|
||||
(for @N 10
|
||||
(link (curry (@N) () (* @N @N))) ) ) )
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue