September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -5,15 +5,13 @@ on run
set end of fns to closure(i)
end repeat
lambda() of item 3 of fns
|λ|() of item 3 of fns
end run
on closure(x)
script
on lambda()
return x * x
end lambda
on |λ|()
x * x
end |λ|
end script
end closure

View file

@ -1,42 +1,54 @@
on run
-- CLOSURE --------------------------------------------------------------------
lambda() of (item 3 of (map(closure, range(1, 10))))
script closure
on |λ|(x)
script
on |λ|()
x * x
end |λ|
end script
end |λ|
end script
end run
on closure(x)
script
on lambda()
return x * x
end lambda
end script
end closure
|λ|() of (item 3 of (map(closure, enumFromTo(1, 10))))
-- GENERIC FUNCTIONS ----------------------------------------------------------
-- 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)
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
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
-- 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
-- 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

View file

@ -1,3 +1,8 @@
#var list := Array new &length:10 set &every: (&index:i) [ [ ^ i * i. ] ].
import system'routines.
console writeLine:(list@3 eval).
program =
[
var functions := Array new length:10; populate(:i)( [ ^ i * i ] ).
functions forEach(:func) [ console writeLine(func eval). ].
].

View file

@ -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]()) }
}

View file

@ -0,0 +1,35 @@
-- First some generic handling stuff, handles partial_args
-- of any mixture of any length and element types.
sequence closures = {}
function add_closure(integer rid, sequence partial_args)
closures = append(closures,{rid,partial_args})
return length(closures) -- (return an integer id)
end function
function call_closure(integer id, sequence args)
{integer rid, sequence partial_args} = closures[id]
return call_func(rid,partial_args&args)
end function
-- 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
function square(integer i)
return i*i
end function
-- 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.
sequence cids = {}
for i=1 to 10 do
--for i=11 to 20 do -- alternative test
cids &= add_closure(routine_id("square"),{i})
end for
-- And finally call em (this loop is blissfully unaware what function
-- it is actually calling, and what partial_arguments it is passing)
for i=1 to 10 do
printf(1," %d",call_closure(cids[i],{}))
end for

View file

@ -0,0 +1,13 @@
function square(integer tid)
integer i = getd("i",tid) -- (setd valid here too)
return i*i
end function
sequence tids = {}
for i=1 to 10 do
--for i=11 to 20 do
tids &= new_dict({{"i",i}})
end for
for i=1 to 10 do
printf(1," %d",square(tids[i]))
end for

View file

@ -1,6 +0,0 @@
list = {}
(1..10).each {|i| list[i] = proc {i * i}}
p list[3].call #=> 9
p list[7][] #=> 49
i = 5
p list[3].call #=> 9

View file

@ -1,5 +0,0 @@
list = {}
for i in 1..10 do list[i] = proc {i * i} end
p list[3][] #=> 100
i = 5
p list[3][] #=> 25

View file

@ -1,7 +1,7 @@
var f = (
0 ..^ 9 -> map {|i| func(j){i * j} }
);
10.of {|i| func(j){i * j} }
)
0 ..^ 8 -> each { |j|
say f[j](j);
9.times { |j|
say f[j](j)
}

View file

@ -1,7 +1,7 @@
var f = 10.of { |i|
var f = (1..10).map { |i|
func(j){i * j}
}
9.times { |j|
say f[j-1](j);
for j (1..9) {
say f[j-1](j)
}

View file

@ -0,0 +1,4 @@
(0).pump(10,List,fcn(i){i*i}.fp)[8]() //-->64
list:=(0).pump(10,List,fcn(i){i*i}.fp);
foreach n in (list.len()-1) { list[n]().println() }
list.run(True).println()