Time window matcher

October 25th, 2007

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.

Popularity: 3% [?]


5 Responses to “Time window matcher”

  1. admin on October 25, 2007 6:24 pm

    Well, now that I think about it, I could do a better parser with a regular-expression-cond form. . .
    What an idea!

  2. Zach Beane on October 26, 2007 6:10 am

    Are you aware of CL-PPCRE:REGISTER-GROUPS-BIND? How does it differ from your SCAN-AND-BIND?

  3. admin on October 26, 2007 1:02 pm

    You’re right, they’re the same. This tells me I need to read the docs a little better.

    Thanks!

  4. Anton on November 4, 2007 9:23 am

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

  5. LOOP Macro Love | LispCast on November 5, 2007 12:26 am

    […] 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 […]

Trackback URI | Comments RSS

Leave a Reply

Name (required)

Email (required)

Website

Speak your mind