While coming up with a relevance recently, I realised that part of it would probably make a good opportunity for people to try to come up with a reasonably succinct solution.
Golf, anyone? (I certainly don’t condone the whole golfing process in working code, as some code can be quite unreadable even without condensing it to the point where, in comparison, line noise is easier to understand.)
Given the types of queries that come up in this forum, I’d say this is a fairly reasonable one to find a solution for, as no tuples or sets are necessary (although Ben or Zak might prove me wrong). Go ahead and fire up your brain and give it a try.
Problem: Using the returned time value of ‘Now’, come up with the Now formatted equivalent of the following weeks Thursday (same time is fine). So, if it was Tuesday, you’d only bump it forward 2 days, but on Wednesday you would add 8 days (Thursday, add 7, etc.).
Tue, 04 Jan 2011 09:01:23 +0000 -> Thu, 06 Jan 2011 09:01:23 +0000
Wed, 05 Jan 2011 09:12:34 +0000 -> Thu, 13 Jan 2011 09:12:34 +0000
I’m a little confused. So the results are supposed to give the Thursday of NEXT week or tomorrow (since today is Weds)? What if today is Thursday? Cidermark & Ben’s examples show the Thursday of next week, but the description of the puzzle seems to indicate whatever is the next upcoming Thursday.
I figured that if I posted my initial code, it might give a clue to how I shortened mine, so I’ll show my final answer(and I’ll add that I had used ‘Th’ before Noah added his comment):
(now+(2+it)*day)whose(it as string starts with"Th")of(1;2;3;4;5;6;7)
68
Paul - I was thinking there would be an offset to manage whether the target day was for this week or next week, but that shouldn’t make a big difference in the calculations, as it would be quite easy to adjust. Apologies if my spec was misleading.
Good one. I was on the right track in my debugger, but for some reason I kept doing
elements of set of (1;2;3;4;5;6;7)
rather than just using
(1;2;3;4;5;6;7)
. Duh.
At first I was trying to use a time/date range forward 1 week and extract the one that starts with “Th”. But I wasn’t able to figure out how to pull out all the dates of a date range. I still think there should be a way to specify things like
times of (now & week)
.
Then it would have worked using
times whose (it as string starts with “Th”) of (now & (6*day))
it whose (it as string starts with “Th”) of (now+it*day) of integers in (1,7)
Though the integers keyword takes away any golfing advantage, at least it’s cleaner to read. Also, now that it’s Thursday I should note that if you want it to capture today as being the closest Thursday, you should use
icing on the cake… here’s a real world use case. The Tuesday which is two weeks after Patch Tuesday is when we want a maintenance window to open:
it whose (it as string starts with “Tu”) of (first tuesday of current month_and_year + it * day) of integers in (15,21)
7 days after first tuesday is second tuesday.
14 days after is third tuesday, which we know we don’t want, so offset to 15.
21 days is as far as we want to go.
I checked that this rounds the horn into next month and year.
why would you want to do that as opposed to:
first tuesday of month_and_year of current date+3*week