[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[QUIZ.SUMMARY] Dice Roller (#61

matthew.moss.coder

1/12/2006 7:05:00 AM

12 Answers

James Gray

1/12/2006 3:02:00 PM

0

On Jan 12, 2006, at 1:04 AM, Matthew Moss wrote:

> Thanks to everyone for the great submissions and lively discussion
> on the
> mailing list (and the correction to the quiz that I missed!).

I want to thank Matthew Moss for a wonderful problem and a
sensational summary! I also happen to know that Matthew already has
another quiz in the queue for a week from tomorrow. How cool is that?

Matthew, you rock.

James Edward Gray II



Christian Neukirchen

1/12/2006 3:23:00 PM

0

Matthew Moss <matthew.moss.coder@gmail.com> writes:

> First off we have the random number generation; most solutions had this or a
> very similar variant. So the call 3.d(6) will roll and accumulate three
> six-sided dice, and the expression 3.d(6) is almost a valid dice expression.
> It is in Christian's initialization method that dice expressions are turned
> into Ruby expressions:
>
> def initialize(dice)
> @src = dice.gsub(/d(%|00)(\D|$)/, 'd100\2').
> gsub(/d(\d+)/, 'd(\1)').
> gsub(/(\d+|\))d/, '\1.d').
> gsub(/\d+/) { $&.gsub(/^0+/, '') }
>
> @dice = eval "lambda { #@src }"
> end
>
> (I've left out a bit of error checking; see the full solution to see
> everything.) The first substitution fixes up percentage and double-zero
> values as 100. The second turns 'd' as a binary operator into 'd' as a
> method call. The third substitution provides the default dice count of 1
> if it wasn't specified. And the last substitution removes leading zeroes of
> integers; this last substitution prevents the Ruby from interpreting values
> as octal.

The third substitution doesn't provide a default count (Dice#d does that),
instead, it converts 2d(6) to 2.d(6).

> The morphed express is saved away in a lambda, which allows Christian to
> reevaluate the expression repeatedly without performing those substitutions
> every time roll is called.

I could have evaluated @src each time without substituting anew, but a
lambda only makes it need to *parse* once.

Thanks for the nice writeup,
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...


Gregory Seidman

1/12/2006 3:55:00 PM

0

On Thu, Jan 12, 2006 at 04:04:58PM +0900, Matthew Moss wrote:
[...]
} Most solutions fell into one of a few types. I'll cover each a bit and point
} out anything in particular that attracted my attention.
}
} The first class of solutions massaged the input expression a bit, then used
} Ruby's eval method to do the work.
[...]
} The second class of solutions involved using a parsing library or parser
} generator tool.
[...]
} And now we're at the third class of solutions: the handcrafted parsers.
} About nine people handcrafted parsers, mostly recursive descent which is
} rather easy to code.
[...]

I want to go through my handcrafted parser solution, which may have gotten
missed in the shuffle. This is especially likely since I also submitted one
using the eval method. In fact, my handcrafted parser used eval, but only
for the +, -, /, and * operators.

The important bit is that all of the nodes in my syntax tree respond to
to_i with the computed result of its subexpression. This was largely to
make numbers behave the same way that my syntactic elements did, and vice
versa. My parsing method looks like this (with a fail check to make sure
nothing went wrong after each line, mainly for debugging purposes) to
create the syntax tree:

1) token_array = tokenize(expression)
uses String#scan and checks for garbage
2) token_array = validate_and_cook(token_array)
replaces some tokens with symbols, deals with dX => 1dX, d00 =>
d100, d% => d100, makes sure open parens have close parens and
the operators and operands are in grammatically appropriate places
3) parse_tokens(token_array)
a) token_array = parse_parens(token_array)
finds parenthesized expressions and runs the subarray through
parse_tokens; nested parens are handled recursively. Note that
there is no need for error checking, since the expression has
already been validated.
b) token_array = parse_ops(token_array, /^d$/) if token_array.length > 1
finds all d operators and creates a DieOperator object with its
left and right operands. Since all the operators in this grammar
are left-associative, parse_ops is left-associative. The regex is
used to determine if the token is the operator(s) being handled.
c) token_array = parse_ops(token_array, /^[*\/]$/) if token_array.length > 1
same as b, but for * and / operators. In this case, ArithOperator
objects are created. The parse_ops method uses an OpsClass hash
that matches the operator (d, *, /, +, or -) to the appropriate
class to call new on.
d) token_array = parse_ops(token_array, /^[-+]$/) if token_array.length > 1
same as c, but for + and - operators. Still using ArithOperator
objects.

The ArithOperator class is what uses eval. It is given the operator as a
string, and its to_i method calls eval "#{@left.to_i}#{@op}#{@right.to_i}"
to produce its value.

The points I'm trying to make are:

1) the grammar was so simple that there was no need for a proper
recursive-descent parser
2) duck typing is way cool and let me treat numbers, DieOperators, and
ArithOperators identically
3) a little eval let me use one class instead of four for ArithOperator
4) doing all that validation ahead of time let me replace my syntax tree
with using eval for the whole expression without giving up my nice,
clear failure messages

} Thanks to everyone for the great submissions and lively discussion on the
} mailing list (and the correction to the quiz that I missed!). I just hope
} when we all sit down to play that I don't see any of you with all characters
} stats of 18.

This one was loads of fun, if not as challenging as the Numeric Maze (which
was my first). It's a pity I'll be out of town this weekend and won't be
able to do the next one.

--Greg



matthew.moss.coder

1/12/2006 10:04:00 PM

0

And I can see I already that I need to read my regexps a little more
carefully. The third regexp in Christian Neukirchen's solution doesn't
provide the default 1 to the 'd' operator; it actually turns the 'd'
operator into a method call on Integer. And the second regexp sets up
parenthesis around the number following the 'd' as arguments for that call.
Silly me....


On 1/12/06, James Edward Gray II <james@grayproductions.net> wrote:
>
> On Jan 12, 2006, at 1:04 AM, Matthew Moss wrote:
>
> > Thanks to everyone for the great submissions and lively discussion
> > on the
> > mailing list (and the correction to the quiz that I missed!).
>
> I want to thank Matthew Moss for a wonderful problem and a
> sensational summary! I also happen to know that Matthew already has
> another quiz in the queue for a week from tomorrow. How cool is that?
>
> Matthew, you rock.
>
> James Edward Gray II
>
>
>

Bill Kelly

1/12/2006 10:15:00 PM

0

From: "Matthew Moss" <matthew.moss.coder@gmail.com>
>
> Rather than change the 'd' operator into a method call, he made a slightly
> different twist and rolled the dice in method_missing. Perhaps Bill didn't
> know that classes could be extended, or maybe he was hacking around for fun.

Yes I could probably dominate the "cheesiest approach" category. :)
I did extend a class in my "solution"... String#die_to_meth :-P


Thanks for a fun quiz and an excellent summary.


Regards,

Bill




matthew.moss.coder

1/12/2006 10:27:00 PM

0

Ya, you did extend a class.

Man, I must really be tired. Or forgot how to read. Or had a few brain cells
short out. Or something. Or be really tired.



On 1/12/06, Bill Kelly <billk@cts.com> wrote:
>
> From: "Matthew Moss" <matthew.moss.coder@gmail.com>
> >
> > Rather than change the 'd' operator into a method call, he made a
> slightly
> > different twist and rolled the dice in method_missing. Perhaps Bill
> didn't
> > know that classes could be extended, or maybe he was hacking around for
> fun.
>
> Yes I could probably dominate the "cheesiest approach" category. :)
> I did extend a class in my "solution"... String#die_to_meth :-P
>
>
> Thanks for a fun quiz and an excellent summary.
>
>
> Regards,
>
> Bill
>
>
>
>

J. Ryan Sobol

1/13/2006 7:57:00 PM

0

On Jan 12, 2006, at 10:22 AM, Christian Neukirchen wrote:

> Matthew Moss <matthew.moss.coder@gmail.com> writes:
>
>> @dice = eval "lambda { #@src }"
>>
>> The morphed express is saved away in a lambda, which allows
>> Christian to
>> reevaluate the expression repeatedly without performing those
>> substitutions
>> every time roll is called.
>
> I could have evaluated @src each time without substituting anew, but a
> lambda only makes it need to *parse* once.

Hey Christian,

I didn't have time to write my own solution to quiz #61, so I'm going
over yours, which is similar to what I was kicking around in my
head. Unfortunately, I confused by your eval statement, which is
wrapped over a new Proc object. Instead, why not wrap the lambda
around the eval like so?

@dice = lambda { eval @src }

The both _seem_ to produce the same output, yet conceptually, my
approach makes more sense to me. Perhaps I'm missing something
crucial in my statement like...

Does eval parse @src for each call() message? Or does the parsing
happen on initialization of the Proc (like I would expect)? I
suppose I could trace the execution using the debugger, but I'm
unaware how to step inside a Kernel method like eval or lambda with it.

~ ryan ~


James Gray

1/13/2006 8:04:00 PM

0

On Jan 13, 2006, at 1:57 PM, J. Ryan Sobol wrote:

> On Jan 12, 2006, at 10:22 AM, Christian Neukirchen wrote:
>
>> Matthew Moss <matthew.moss.coder@gmail.com> writes:
>>
>>> @dice = eval "lambda { #@src }"
>>>
>>> The morphed express is saved away in a lambda, which allows
>>> Christian to
>>> reevaluate the expression repeatedly without performing those
>>> substitutions
>>> every time roll is called.
>>
>> I could have evaluated @src each time without substituting anew,
>> but a
>> lambda only makes it need to *parse* once.
>
> Hey Christian,
>
> I didn't have time to write my own solution to quiz #61, so I'm
> going over yours, which is similar to what I was kicking around in
> my head. Unfortunately, I confused by your eval statement, which
> is wrapped over a new Proc object. Instead, why not wrap the
> lambda around the eval like so?
>
> @dice = lambda { eval @src }
>
> The both _seem_ to produce the same output, yet conceptually, my
> approach makes more sense to me. Perhaps I'm missing something
> crucial in my statement like...
>
> Does eval parse @src for each call() message?

Yes. You pass lambda() a block of code that is executed each time
the code it called.

James Edward Gray II


Logan Capaldo

1/13/2006 8:09:00 PM

0


On Jan 13, 2006, at 2:57 PM, J. Ryan Sobol wrote:

> On Jan 12, 2006, at 10:22 AM, Christian Neukirchen wrote:
>
>> Matthew Moss <matthew.moss.coder@gmail.com> writes:
>>
>>> @dice = eval "lambda { #@src }"
>>>
>>> The morphed express is saved away in a lambda, which allows
>>> Christian to
>>> reevaluate the expression repeatedly without performing those
>>> substitutions
>>> every time roll is called.
>>
>> I could have evaluated @src each time without substituting anew,
>> but a
>> lambda only makes it need to *parse* once.
>
> Hey Christian,
>
> I didn't have time to write my own solution to quiz #61, so I'm
> going over yours, which is similar to what I was kicking around in
> my head. Unfortunately, I confused by your eval statement, which
> is wrapped over a new Proc object. Instead, why not wrap the
> lambda around the eval like so?
>
> @dice = lambda { eval @src }
>
> The both _seem_ to produce the same output, yet conceptually, my
> approach makes more sense to me. Perhaps I'm missing something
> crucial in my statement like...
>
> Does eval parse @src for each call() message? Or does the parsing
> happen on initialization of the Proc (like I would expect)? I
> suppose I could trace the execution using the debugger, but I'm
> unaware how to step inside a Kernel method like eval or lambda with
> it.
>
> ~ ryan ~
>

@dice.call has eval parse @src everytime (@src may have changed). By
doing something like eval "lambda { #{@src} }" you parse @src once
and get back a lambda.


J. Ryan Sobol

1/13/2006 8:15:00 PM

0

On Jan 13, 2006, at 3:03 PM, James Edward Gray II wrote:

>> Does eval parse @src for each call() message?
>
> Yes. You pass lambda() a block of code that is executed each time
> the code it called.

Executed yes. But when is it parsed / checked for valid grammar,
syntax, etc.? Did you mean to say that the block of code is parsed
AND executed for each call() it receives?

~ ryan ~