On Mar 20, 2004, at 1:34 AM, Mauricio FernáÏdez wrote: > On Sat, Mar 20, 2004 at 08:15:40AM +0900, Mark Hubbart wrote: >>> The need for intersection is obvious. I don't immediately see the >>> need >>> for union, however. >> >> DayOfMonth(1).difference(DayOfWeek(:sunday)).union( >> DayOfMonth(2).intersection(DayOfWeek(:monday)) >> ) > > This weighs a ton (reminds me of Java), how about > (DayOfMonth(1) & not(DayOfWeek(:sunday))) | # maybe .not ? > (DayOfMonth(2) & DayOfWeek(:monday) ) I agree :) I wouldn't ever suggest using huge, non-verbal method names like that in real life. I used them there mainly for clarity; since I was demonstrating usefulness of union and difference. perhaps, using possible real syntax: Tempexp.new{ every(:dom=>1).except(:sunday) & every(:dom=>1).when(:monday) } You could make standard methods (every, except, when, and, not...) and alias them to the appropriate & ^ | operators. This would let someone present it in the way they feel is clearest. > or even > (dom(1) & not(dow(:sunday))) | (dom(2) & dow(:monday)) > > It's pretty easy to implement with the same technique Criteria uses > (building the AST using Ruby expressions). > > Since the temporal expression must be scoped somehow (you want that for > a particular month, or a number of them), a naŭ×e implementation could > even use set operations (using Array); e.g. > > (for month == March 2004) > (note: SomeDayObject objects carry complete date info, only the dom is > shown here for briefness) > DayOfMonth(1) => [SomeDayObject(1)] > DayOfWeek(:sunday) => [SomeDayObject(7), SomeDayObject(14), > SomeDayObject(21),SomeDayObject(28)] > > DayOfMonth(1) - DayOfWeek(:sunday) => [SomeDayObject(1)] > > Intersection is Array#&, union would be Array#+. You'd then just have > to take the first element if you only want one day, or the whole array > if you want a set. Interesting idea... :) > >>> For example, you can create simple expressions ... > >> Hmm, should we overload && and || for these? Maybe someone already >> has. > > && or || can't be overloaded; & and | work nicely though. well, that sucks. I didn't know that, but I tried it, and you are right! >> I believe that this imaginary code would give you every first of the >> month, unless it falls on a sunday, then it gives the second of the >> month. Trchic! (yeah, I probly misspelled that.) > > I'd expect it to give out the first of the month unless it falls on a > sunday, or the 2nd of the month if it is a monday (else nothing), but > maybe I just have different semantics in mind. Correct. :) I believe that it comes out to the same thing, though. But yours is more literally correct. It really means first-of-the-month non-sundays and second-of-the-month mondays (implying that sunday was first-of-the-month, that month) Cheers, --Mark