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

@ -39,7 +39,7 @@ on map(f, xs)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to lambda(item i of xs, i, xs)
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
@ -58,7 +58,7 @@ on foldl(f, startValue, xs)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
@ -68,14 +68,13 @@ end foldl
-- Sublist of those elements for which the predicate
-- function returns true
-- filter :: (a -> Bool) -> [a] -> [a]
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(f, xs)
tell mReturn(f)
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if lambda(v, i, xs) then set end of lst to v
if |λ|(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
@ -88,7 +87,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn

View file

@ -1,7 +1,7 @@
fun main(args: Array<String>) {
val list = listOf(1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0)
var a = list.map({ x -> x + 2 }).average()
var h = list.map({ x -> x * x }).average()
var g = list.map({ x -> x * x * x }).average()
val a = list.map({ x -> x + 2 }).average()
val h = list.map({ x -> x * x }).average()
val g = list.map({ x -> x * x * x }).average()
println("A = %f G = %f H = %f".format(a, g, h))
}

View file

@ -0,0 +1,56 @@
say callit(.routines~fib, 10)
say callit(.routines~fact, 6)
say callit(.routines~square, 13)
say callit(.routines~cube, 3)
say callit(.routines~reverse, 721)
say callit(.routines~sumit, 1, 2)
say callit(.routines~sumit, 2, 4, 6, 8)
-- call the provided routine object with the provided variable number of arguments
::routine callit
use arg function
args = arg(2, 'a') -- get all arguments after the first to pass along
return function~callWith(args) -- and pass along the call
::routine cube
use arg n
return n**3
::routine square
use arg n
return n**2
::routine reverse
use arg n
return reverse(n)
::routine fact
use arg n
accum = 1
loop j = 2 to n
accum = accum * j
end
return accum
::routine sumit
use arg n
accum = 0
do i over arg(1, 'a') -- iterate over the array of args
accum += i
end
return accum
::routine fib
use arg n
if n == 0 then
return n
if n == 1 then
return n
last = 0
next = 1
loop j = 2 to n;
current = last + next
last = next
next = current
end
return current

View file

@ -1,2 +1,10 @@
/x_times_3_sub_1 {3 * 1 sub}.
[0 1 2 3 4] {x_times_3_sub_1} map
% operator example
% 'ifelse' is passed a boolean and two procedures
/a 5 def
a 0 gt { (Hello!) } { (World?) } ifelse ==
% procedure example
% 'bar' is loaded onto the stack and passed to 'foo'
/foo { exec } def
/bar { (Hello, world!) } def
/bar load foo ==

View file

@ -1,18 +1,12 @@
/*REXX program demonstrates passing a function (as a name) to another function. */
n=3735928559
funcName = 'fib' ; q= 10; call someFunk funcName, q; call tell
funcName = 'fact' ; q= 6; call someFunk funcName, q; call tell
funcName = 'square' ; q= 13; call someFunk funcName, q; call tell
funcName = 'cube' ; q= 3; call someFunk funcName, q; call tell
q=721; call someFunk 'reverse',q; call tell
say copies('', 30) /*display a nice separator fence */
say 'done as' d2x(n). /*prove that variable N is still intact*/
/*REXX program demonstrates the passing of a name of a function to another function. */
call function 'fact' , 6; say right( 'fact{'$"} = ", 30) result
call function 'square' , 13; say right( 'square{'$"} = ", 30) result
call function 'cube' , 3; say right( 'cube{'$"} = ", 30) result
call function 'reverse', 721; say right( 'reverse{'$"} = ", 30) result
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cube: return n**3
fact: !=1; do j=2 to n; !=!*j; end; return !
fib: if n<2 then return n; _=0; a=0; b=1; do j=2 to n; _=a+b; a=b; b=_; end; return _
reverse: return 'REVERSE'(n)
someFunk: procedure; arg ?,n; signal value (?); say result 'result'; return
square: return n**2
tell: say right(funcName'('q") = ",20) result; return
cube: return $**3
fact: procedure expose $; !=1; do j=2 to $; !=!*j; end; return !
function: arg ?.; parse arg ,$; signal value (?.)
reverse: return 'REVERSE'($)
square: return $**2

View file

@ -0,0 +1,11 @@
Sub HigherOrder()
Dim result As Single
result = first("second")
MsgBox result
End Sub
Function first(f As String) As Single
first = Application.Run(f, 1) + 2
End Function
Function second(x As Single) As Single
second = x / 2
End Function

View file

@ -0,0 +1 @@
fcn f(g){ g() } fcn g{ "Hello World!".println() }

View file

@ -0,0 +1,2 @@
fcn f(g){ g() }
fcn(g){ g() }(fcn{ "Hello World!".println() } )