Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,25 @@
-- This handler takes a script object (singer)
-- with another handler (call).
on sing about topic by singer
call of singer for "Of " & topic & " I sing"
end sing
-- Define a handler in a script object,
-- then pass the script object.
script cellos
on call for what
say what using "Cellos"
end call
end script
sing about "functional programming" by cellos
-- Pass a different handler. This one is a closure
-- that uses a variable (voice) from its context.
on hire for voice
script
on call for what
say what using voice
end call
end script
end hire
sing about "closures" by (hire for "Pipe Organ")

View file

@ -0,0 +1,115 @@
on run
-- PASSING FUNCTIONS AS ARGUMENTS TO
-- MAP, FOLD/REDUCE, AND FILTER, ACROSS A LIST
set lstRange to {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
map(squared, lstRange)
--> {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100}
foldl(summed, 0, map(squared, lstRange))
--> 385
filter(isEven, lstRange)
--> {0, 2, 4, 6, 8, 10}
-- OR MAPPING OVER A LIST OF FUNCTIONS
map(testFunction, {doubled, squared, isEven})
--> {{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
-- {0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100},
-- {true, false, true, false, true, false, true, false, true, false, true}}
end run
-- testFunction :: (a -> b) -> [b]
on testFunction(f)
map(f, {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10})
end testFunction
-- MAP, REDUCE, FILTER
-- Returns a new list consisting of the results of applying the
-- provided function to each element of the first list
-- 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
-- Applies a function against an accumulator and
-- each list element (from left-to-right) to reduce it
-- to a single return value
-- In some languages, like JavaScript, this is called reduce()
-- Arguments: function, initial value of accumulator, list
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- Sublist of those elements for which the predicate
-- function returns true
-- 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 |λ|(v, i, xs) then set end of lst to v
end repeat
return lst
end tell
end filter
-- 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
-- HANDLER FUNCTIONS TO BE PASSED AS ARGUMENTS
-- squared :: Number -> Number
on squared(x)
x * x
end squared
-- doubled :: Number -> Number
on doubled(x)
x * 2
end doubled
-- summed :: Number -> Number -> Number
on summed(a, b)
a + b
end summed
-- isEven :: Int -> Bool
on isEven(x)
x mod 2 = 0
end isEven

View file

@ -0,0 +1,3 @@
{{0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20},
{0, 1, 4, 9, 16, 25, 36, 49, 64, 81, 100},
{true, false, true, false, true, false, true, false, true, false, true}}

View file

@ -0,0 +1,11 @@
script aScript
on aHandler(aParameter)
say aParameter
end aHandler
end script
on receivingHandler(passedScript)
passedScript's aHandler("Hello")
end receivingHandler
receivingHandler(aScript)

View file

@ -0,0 +1,13 @@
on aHandler(aParameter)
say aParameter
end aHandler
on receivingHandler(passedHandler)
script o
property h : passedHandler
end script
o's h("Hello")
end receivingHandler
receivingHandler(aHandler)