Pseudo-English
Thursday, April 17th, 2008So I came across a feature to implement whose description read “X% of the time, do Y”. I thought for a second, and then wrote:
class Numeric def percent_of_the_time yield if rand(100) < self end end
This works great: 15.percent_of_the_time {puts "hello, world"}.
(My unit testing friends are probably freaking out over monkey-patching core classes to be nondeterministic. Good!)
Then I got to wondering: why can’t I do this in Lisp? In Arc some things are callable which normally aren’t, but that seems a bit too much like magic to me, and I’m not sure it’s easily extensible to this case.
The problem with making something pseudo-English is that you need to put the noun first, and in Lisp the verb always comes first.
The solution is more obvious than I’d thought: use a library like curly-infix, and then just write a normal function. Thus, in Lisp, it can be as simple as:
{15 percent-of-the-time (format t "hello, world")}
The only gotcha is that this looks suspiciously like a single-dispatch method call, but if you’re using CLOS generic functions, it’s not.
