Time window matcher
Well, I did it: I succumbed to the allure and challenge of someone who posted a Ruby quiz on Usenet:
this is ruby quiz 144, anybody feel like doing it in CL?
Time Window (#144)
by Brian Candler
Write a Ruby class which can tell you whether the current time (or any
given time) is within a particular “time window”. Time windows are
defined by strings in the following format:
ruby
# 0700-0900 # every day between these times
# Sat Sun # all day Sat and Sun, no other times
# Sat Sun 0700-0900 # 0700-0900 on Sat and Sun only
# Mon-Fri 0700-0900 # 0700-0900 on Monday to Friday only
# Mon-Fri 0700-0900; Sat Sun # ditto plus all day Sat and Sun
# Fri-Mon 0700-0900 # 0700-0900 on Fri Sat Sun Mon
# Sat 0700-0800; Sun 0800-0900 # 0700-0800 on Sat, plus 0800-0900 on Sun
I was intrigued. So I did it. It’s a bit messy, as these things usually go, but it passes all the unit tests. I did it first in a Lispy way, by making it sexpression instead of strings, then I tacked a string parser on top of it.
You can find the code here: Ruy Quiz #144 Solved in Common Lisp . It requires lisp-unit, cl-utilities, cl-ppcre, and s-utils.
[edit: a new version is available, which I describe a bit here]
Please feel free to comment. I’d love feedback.
One of the things I love about doing these things is that you get a nice set of new utility functions when I refactor them. Sometimes I feel like if it didn’t require so many lines of code, then there’s nothing I really gain from it, besides the main function I write.
Here are some of the utility goodies I made:
- scan-and-bind macro, which binds variables from a regex match
- make-time function, which has the parameters of encode-universal-time reversed with default values
- (so I can write (make-time 2007 10 24) instead of (encode-universal-time 0 0 0 24 10 2007))
- hour-of and minute-of which give the hour and minute, respectively, from a universal time
- other minor functions. Check out the source if you want to find out more
I like these kinds of challenges. They help me grow as a programmer. And I think that fun competition between languages is healthy for all languages involved. One of the reasons I decided to take the challenge is that I feel like Common Lisp is challenged a lot by people who don’t understand it. I don’t know if this is the case for this particular post, but usually what ends up happening is that the challenge is easily solved in CL, then a lengthy argument ensues about how it wasn’t done correctly or whether it can be done in fewer lines of code in another language.
This pains me, because there are a lot of claims that Lisp dialects have many advantages over other languages. And many popular languages claim to have been inspired by Lisp. The question I think that comes to mind is: “if Lisp is so great, why hasn’t it been proven after so many challenges?”
Well, I’m not sure about this (and I’d love your comments) but I think one reason is that the Common Lisp community doesn’t challenge other language communities. Common Lisp is good at a lot of things. If we find those things it’s best at, and showed those things to the world, I bet people will understand. For instance, I believe that while other languages create mini-languages in character strings, in Common Lisp, we do it in the language itself, which might be hard to understand if you haven’t done it before.
By taking a proactive position, we might show that we are up for the challenge
So, right now, I’m looking for a challenge to post to the comp.lang.ruby newsgroup. I want one that will rattle them to the bone about the inadequacy of their language. Nothing big, just a whopper to get them thinking. Do you have any ideas? Let’s give them something to talk about.
Related Posts
Popularity: 2% [?]
Filed under community |5 Responses to “Time window matcher”
Leave a Reply

Well, now that I think about it, I could do a better parser with a regular-expression-cond form. . .
What an idea!
Are you aware of CL-PPCRE:REGISTER-GROUPS-BIND? How does it differ from your SCAN-AND-BIND?
You’re right, they’re the same. This tells me I need to read the docs a little better.
Thanks!
> So, right now, I’m looking for a challenge to post to
> the comp.lang.ruby newsgroup. I want one that will
> rattle them to the bone about the inadequacy of
> their language. Nothing big, just a whopper to get
> them thinking. Do you have any ideas? Let’s give
> them something to talk about.
As a last resort we may just write to comp.lang.ruby that they are idiots.
[…] I’ve re-written my time-window code. It’s cleaner, it compiles the matchers to nested lambdas (so it’s faster), and now it […]