Function composition: Difference between revisions

m
Line 220:
We can generalise this lifting with an '''mReturn''' or '''mInject''' function, which injects a handler into a script for us. This allows use to write higher-order composition and pipelining functions which take a pair (or sequence of) ordinary handlers as arguments, and return a first class script object. (We can also use mReturn to equip AppleScript with '''map''' and '''fold''' functions which take a list and an ordinary handler as arguments).
 
<lang applescript>-- FUNCTIONS TO COMPOSE --------------------------------------------- COMPOSITION OF A LIST OF FUNCTIONS ----------
 
-- compose :: [(a -> a)] -> (a -> a)
on compose(fs)
script
on |λ|(x)
script
on |λ|(a, f)
mReturn(f)'s |λ|(a)
end |λ|
end script
foldr(result, x, fs)
end |λ|
end script
end compose
 
 
-- TEST ---------------------------------------------- TEST -------------------------
on root(x)
x ^ 0.5
Line 234 ⟶ 251:
end half
 
-- TEST -----------------------------------------------------------------------
on run
Line 242 ⟶ 258:
end run
 
-- GENERIC FUNCTIONS ----------------------------------------------------------
 
-------------------- GENERIC FUNCTIONS -------------------
-- compose :: [(a -> a)] -> (a -> a)
on compose(fs)
script
on |λ|(x)
script
on |λ|(a, f)
mReturn(f)'s |λ|(a)
end |λ|
end script
foldr(result, x, fs)
end |λ|
end script
end compose
 
-- foldr :: (a -> b -> a) -> a -> [b] -> a
9,655

edits