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: Neil Faiman <email@hidden>
- Date: Mon, 1 Nov 2004 19:25:48 -0500
The example below isn't quite right. As written, the recursive call
computes the proper next business day, but nothing gets done with it;
instead, we return the value that was assigned by the assignment "d = d
+ 1", even if it got re-incremented in the recursive call. This ought
to be:
on getNextBusinessDay(d)
if isWeekend(d) or isHoliday(d) then
return getNextBusinessDay(d+1)
else
return d
end if
end getNextBusinessDay
But recursion is overkill in this example. A simple loop works just as
well:
on getNextBusinessDay(d)
repeat while isWeekend(d) or isHoliday(d)
d = d + 1
end repeat
return d
end getNextBusinessDay
Regards,
Neil Faiman
On Nov 1, 2004, at 6:03 PM, Eric Geoffroy wrote:
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:
email@hidden
This email sent to email@hidden
_______________________________________________
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