First-class functions: Difference between revisions
→{{header|AppleScript}}
Line 240:
<lang applescript>on run {}
set lstFn to {
set lstInvFn to {
-- Form a list of three composed function objects,
Line 247:
-- application of each composed function (base function composed with inverse) to 0.5
map(testWithHalf, zipWith(mCompose, lstFn, lstInvFn)
--> {0.5, 0.5, 0.5}
Line 254 ⟶ 255:
on testWithHalf(mf)
mf's
end testWithHalf
Line 261 ⟶ 262:
on mCompose(f, g)
script
on
mReturn(f)'s
end
end script
end mCompose
--
on map(
set mf to mReturn(f)
set lst to {}▼
set lng to length of xs
▲ set lst to {}
repeat with i from 1 to lng
set end of lst to mf's
end repeat
return lst
end map
-- zipWith generalises zip by zipping with the function given as the first argument,
-- instead of a tupling function
-- (a -> b -> c) -> [a] -> [b] -> [c]
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set
set lst to {}▼
set mf to mReturn(f)▼
set end of lst to mf's call(item 1 of xs, item 1 of ys)▼
end if▼
▲ set lst to {}
repeat with i from 1 to lng
return lst
end zipWith
-- An ordinary AppleScript handler function
Line 304 ⟶ 302:
on mReturn(f)
script
property
end script
end mReturn
on
(do shell script "echo 's(" & r & ")' | bc -l") as real
end
on
(do shell script "echo 'c(" & r & ")' | bc -l") as real
end
on
x ^ 3
end
on
x ^ (1 / 3)
end
on
(do shell script "echo 'a(" & r & "/sqrt(1-" & r & "^2))' | bc -l") as real
end
on
(do shell script "echo 'a(sqrt(1-" & r & "^2)/" & r & ")' | bc -l") as real
end
</lang>
|