• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
A footnote on closures and composition
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

A footnote on closures and composition


  • Subject: A footnote on closures and composition
  • From: jif <email@hidden>
  • Date: Tue, 08 Mar 2016 14:09:54 +0000

As a footnote to Chris Page’s excellent post in January, which I have just read with interest:


http://lists.apple.com/archives/applescript-users/2016/Jan/msg00152.html


First, a minor point, where Chris’s example used an assignment to bind x to 0, 

to incrementBy(n)
set x to 0
script
to next()
set x to x + n
end next
end script
end incrementBy

I notice in myself a slight preference for using properties of the script object, here initially rewriting Chris’s example to:

to incrementBy(n)
script
property x : 0

to next()
set x to x + n
end next
end script
end incrementBy


Then, more generally, I am, FWIW, finding it useful, when I want more composable AppleScript, to delegate script-wrapping to a couple of higher order functions.

Something like the mReturn function in this AppleScript definition of a dot product as essentially:


sum(zipWith(my product, xs, ys))

https://rosettacode.org/wiki/Dot_product#AppleScript


or the mClosure in this more obviously stretched experiment with expressing set comprehensions in functionally composed AppleScript:

https://rosettacode.org/wiki/List_comprehensions#AppleScript


One variant on Chris’s example might then be (if we draw on an mClosure library function):


on incrementBy(n)

mClosure(my next, {x:0, n:n})

end incrementBy

to next()
tell my closure
set its x to (its x) + (its n)
end tell
end next


on run

set inc to incrementBy(3)

{inc's next(), inc's next(), inc's next()}

-- {3, 6, 9}

end run




-- Handler -> Record -> Script
on mClosure(f, recBindings)
script
property closure : recBindings
property lambda : f
end script
end mClosure



 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Prev by Date: Re: Insert page in Preview
  • Next by Date: How to speed up scripting Apple's Reminders app
  • Previous by thread: Re: Insert page in Preview
  • Next by thread: How to speed up scripting Apple's Reminders app
  • Index(es):
    • Date
    • Thread