Re: Best practices- nested loops or handlers or both
Re: Best practices- nested loops or handlers or both
- Subject: Re: Best practices- nested loops or handlers or both
- From: Eric Geoffroy <email@hidden>
- Date: Mon, 01 Nov 2004 15:03:46 -0800
Adam, I thank you. The reward of taking a great big long meandering
script and seeing it honed to perfect sharpness is great. and Wow,
Thanksgiving is the perfect test!
Recursive handlers are something I want to master. I don't know why
it's so tough on my poor little brain. At least with working examples I
have a place to start.
- Eric
On Nov 1, 2004, at 10:51 AM, Adam K. Wuellner wrote:
On Nov 1, 2004, at 12:03 PM, Eric Geoffroy wrote:
d is date in list
Loop 1 tests d and if it's a weekend, bumps it to next day.
Loop 2 tests d and if it's a holiday, bumps it to next day.
Now those two loops need to be nested in case loop 2 moves d to a
weekend.
... and, obviously, it doesn't stop there. If you start on
Thanksgiving, for example, you'll need loop 2 twice and loop 1 twice:
(Thursday -loop 2-> Friday -loop 2-> Saturday -loop 1-> Sunday -loop
1-> Monday)
What you want, probably, is a recursive handler that test for both
weekend-ness and holiday-ness, increments the date if either are true,
and calls itself again to see if it should continue processing.
So, say you've got good isWeekend(d) and isHoliday(d) handlers, each
returning true or false. Then you can create a recursive handler to
increment the 'd' parameter until both tests are false. Consider this
pseudo code:
on getNextBusinessDay(d)
if isWeekend(d) or isHoliday(d) then
d = d + 1
getNextBusinessDay(d)
end if
return d
end getNextBusinessDay
If d is either a holiday or a weekend, then it gets bumped, and we
call the handler again. If it's still either holiday or weekend, then
it gets bumped, and we call the handler again... If at any point
(iteration) d is not a holiday or weekend, the if...end if block is
passed over and the value d is returned from the handler.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden