First-class functions: Difference between revisions
Content deleted Content added
Line 240: | Line 240: | ||
<lang applescript>on run {} |
<lang applescript>on run {} |
||
set lstFn to { |
set lstFn to {sin_, cos_, cube_} |
||
set lstInvFn to { |
set lstInvFn to {asin_, acos_, croot_} |
||
-- Form a list of three composed function objects, |
-- Form a list of three composed function objects, |
||
Line 247: | Line 247: | ||
-- application of each composed function (base function composed with inverse) to 0.5 |
-- application of each composed function (base function composed with inverse) to 0.5 |
||
map(zipWith(mCompose, lstFn, lstInvFn) |
map(testWithHalf, zipWith(mCompose, lstFn, lstInvFn)) |
||
⚫ | |||
--> {0.5, 0.5, 0.5} |
--> {0.5, 0.5, 0.5} |
||
Line 254: | Line 255: | ||
on testWithHalf(mf) |
on testWithHalf(mf) |
||
mf's |
mf's lambda(0.5) |
||
end testWithHalf |
end testWithHalf |
||
Line 261: | Line 262: | ||
on mCompose(f, g) |
on mCompose(f, g) |
||
script |
script |
||
on |
on lambda(x) |
||
mReturn(f)'s |
mReturn(f)'s lambda(mReturn(g)'s lambda(x)) |
||
end |
end lambda |
||
end script |
end script |
||
end mCompose |
end mCompose |
||
-- |
-- map :: (a -> b) -> [a] -> [b] |
||
on map( |
on map(f, xs) |
||
set mf to mReturn(f) |
set mf to mReturn(f) |
||
⚫ | |||
⚫ | |||
set lng to length of xs |
set lng to length of xs |
||
⚫ | |||
repeat with i from 1 to lng |
repeat with i from 1 to lng |
||
set end of lst to mf's |
set end of lst to mf's lambda(item i of xs, i, xs) |
||
end repeat |
end repeat |
||
return lst |
return lst |
||
end map |
end map |
||
-- zipWith generalises zip by zipping with the function given as the first argument, |
-- zipWith generalises zip by zipping with the function given as the first argument, |
||
-- instead of a tupling function |
-- instead of a tupling function |
||
-- (a -> b -> c) -> [a] -> [b] -> [c] |
-- (a -> b -> c) -> [a] -> [b] -> [c] |
||
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] |
|||
on zipWith(f, xs, ys) |
on zipWith(f, xs, ys) |
||
set |
set lng to length of xs |
||
if lng is not length of ys then return missing value |
|||
⚫ | |||
if lngX > 0 and lngY > 0 then |
|||
⚫ | |||
⚫ | |||
if lngX > 1 and lngY > 1 then |
|||
set lst to lst & zipWith(f, items 2 thru -1 of xs, items 2 thru -1 of ys) |
|||
⚫ | |||
end if |
|||
⚫ | |||
⚫ | |||
repeat with i from 1 to lng |
|||
⚫ | |||
⚫ | |||
return lst |
return lst |
||
end zipWith |
end zipWith |
||
-- An ordinary AppleScript handler function |
-- An ordinary AppleScript handler function |
||
Line 304: | Line 302: | ||
on mReturn(f) |
on mReturn(f) |
||
script |
script |
||
property |
property lambda : f |
||
end script |
end script |
||
end mReturn |
end mReturn |
||
on |
on sin:r |
||
(do shell script "echo 's(" & r & ")' | bc -l") as real |
(do shell script "echo 's(" & r & ")' | bc -l") as real |
||
end |
end sin: |
||
on |
on cos:r |
||
(do shell script "echo 'c(" & r & ")' | bc -l") as real |
(do shell script "echo 'c(" & r & ")' | bc -l") as real |
||
end |
end cos: |
||
on |
on cube:x |
||
x ^ 3 |
x ^ 3 |
||
end |
end cube: |
||
on |
on croot:x |
||
x ^ (1 / 3) |
x ^ (1 / 3) |
||
end |
end croot: |
||
on |
on asin:r |
||
(do shell script "echo 'a(" & r & "/sqrt(1-" & r & "^2))' | bc -l") as real |
(do shell script "echo 'a(" & r & "/sqrt(1-" & r & "^2))' | bc -l") as real |
||
end |
end asin: |
||
on |
on acos:r |
||
(do shell script "echo 'a(sqrt(1-" & r & "^2)/" & r & ")' | bc -l") as real |
(do shell script "echo 'a(sqrt(1-" & r & "^2)/" & r & ")' | bc -l") as real |
||
end |
end acos: |
||
</lang> |
</lang> |
||