• 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
Re: Best practices- nested loops or handlers or both
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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 16:58:59 -0800

OK. Worked for two+ hours and can't figure out how to return from the isWeekend(d) handler. I scoured the Neuberg book and I'm missing something.

Do I want to return true or return d?
The error says: "No result was returned from some part of this expression." Here's the handler. (It's okay to laugh. I'm not proud)


on isWeekend(d)
	if ((weekday of d) as string) contains "Saturday" or "Sunday" then
		return true
	end if
end isWeekend

Thanks,
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.




Eric Geoffroy
Media Producer
Peachpit Press
(510) 524-2178 x214

_______________________________________________
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


  • Follow-Ups:
    • Re: Best practices- nested loops or handlers or both
      • From: Dave Balderstone <email@hidden>
References: 
 >Best practices- nested loops or handlers or both (From: Eric Geoffroy <email@hidden>)
 >Re: Best practices- nested loops or handlers or both (From: "Adam K. Wuellner" <email@hidden>)

  • Prev by Date: Re: Best practices- nested loops or handlers or both
  • Next by Date: get ip proxy and port
  • Previous by thread: Re: Best practices- nested loops or handlers or both
  • Next by thread: Re: Best practices- nested loops or handlers or both
  • Index(es):
    • Date
    • Thread