It’s possible that you may find one or two other useful things in the library which that comes from.
If you are curious about snapping together these pre-existing ‘lego-blocks’ of Applescript code to build small tools for Tinderbox, then the first functions to experiment with are probably map, filter and foldl (reduce):
Examples, FWIW: (click to expand the source code)
use AppleScript version "2.4"
use framework "Foundation"
use scripting additions
-- MAP, FILTER, FOLD/REDUCE
-- (Applescript examples)
on run
-- MAP ------------------------------------------------
map(dbl, {1, 2, 3, 4, 5})
--> {2, 4, 6, 8, 10}
map(toUpper, {"alpha", "beta", "gamma"})
--> {"ALPHA", "BETA", "GAMMA"}
map(odd, enumFromTo(1, 10))
--> {true, false, true, false, true, false, true, false, true, false}
-- FILTER ---------------------------------------------
filter(odd, enumFromTo(1, 10))
--> {1, 3, 5, 7, 9}
filter(even, enumFromTo(1, 10))
--> {2, 4, 6, 8, 10}
script longer
on |λ|(x)
4 < length of x
end |λ|
end script
filter(longer, {"alpha", "beta", "gamma", "delta", "epsilon", "zeta", "eta"})
--> {"alpha", "gamma", "delta", "epsilon"}
-- FOLD / REDUCE (here fold left: foldl) --------------
foldl(plus, 0, enumFromTo(1, 10))
--> 55
foldl(mul, 1, enumFromTo(1, 10))
-- 3628800
foldl(max, 0, {81, 27, 25, 64, 121, 16, 100})
--> 121
end run
-- Sample functions ---------------------------------------
on dbl(x)
2 * x
end dbl
on plus(a, b)
a + b
end plus
on mul(a, b)
a * b
end mul
on max(a, b)
if b > a then
b
else
a
end if
end max
-- GENERIC ------------------------------------------------
-- https://github.com/RobTrew/prelude-applescript
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m ≤ n then
set lst to {}
repeat with i from m to n
set end of lst to i
end repeat
return lst
else
return {}
end if
end enumFromTo
-- even :: Int -> Bool
on even(x)
0 = x mod 2
end even
-- 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
-- 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
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- 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
-- odd :: Int -> Bool
on odd(x)
not even(x)
end odd
-- toUpper :: String -> String
on toUpper(str)
set ca to current application
((ca's NSString's stringWithString:(str))'s ¬
uppercaseStringWithLocale:(ca's NSLocale's currentLocale())) as text
end toUpper