Skip to content

Instantly share code, notes, and snippets.

@larryv
Last active August 29, 2015 14:12
Show Gist options
  • Save larryv/56f09b769185825cf5a0 to your computer and use it in GitHub Desktop.
Save larryv/56f09b769185825cf5a0 to your computer and use it in GitHub Desktop.
simple fold implementation
on foldLeft given operator:op, initial:z, sequence:xs
(*
AppleScript handlers can't call handlers that are passed
as parameters because the enclosing script object
doesn't recognize the parameter name as a handler. Work
around this by doing the computation inside an embedded
script object. (See Neuberg, p. 150.)
*)
repeat until xs = {}
set z to op's f(z, item 1 of xs)
set xs to rest of xs
end repeat
return z
end foldLeft
on filter thru predicate given sequence:xs
script predicateOp
on f(z, x)
if predicate's p(x) then set end of z to x
return z
end f
end script
return foldLeft given operator:predicateOp, initial:{}, sequence:xs
end filter
(*
References:
Neuberg, Matt. "AppleScript: The Definitive Guide", 2nd ed.
ISBN 0-596-10211-9
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment