From: Andreas Launila Date: 2008-05-05T23:19:57+09:00 Subject: Re: How would you design regexps in the integer domain? Eivind Eklund wrote: > > I'd implement this using a generic repeat operator instead of > hardcoding * and +, and using a generic comparator instead of > restricting to Integers. > > The declaration for this would be something like > > # Use repeat(0, nil, ...) for * > # Use repeat(1, nil, ...) for + > # Use repeat(0, 1, ...) for ? > def repeat(minimum, maximum, repeated_expression) > def or(*expressions) > > So you'd convert your example "(17|1 5)*" to repeat(0, nil, or(17, [1, 5])) > I like that idea, especially using the arrays to group integers. It seems like a rather economic way to do it. I think "any" would fit better than "or" though (since it can take more than two arguments). The example in its entirety would then be [repeat(0, nil, any(17, [1, 5])), 4711] I would also consider swapping the position of repeated_expression to the first argument of repeat, since it seems like a more natural order to me ("repeat expression between 0 and 4 times"). I'm a bit split on using nil for infinity, but that is minor. I will need to explore the idea, but it feels like a good fit to me. Thank you. > Actually, as I thought of after I had written the above, I have > written code that does part of this already, and is generic - it's > available in types.rb (http://people.freebsd.org/~eivind/ruby/types/). > If you want it, I think I can extend that to support repeats tonight. > Feel free to use code from it under any license that absolves me of > legal blame (ie, the only reason I don't put it in the public domain > is because that expose me to legal liability.) > Thanks for the offer, but there's no need to add additional support. The important part is the idea, which you have provided. -- Andreas Launila