in reply to Tied and Entangled, Quantum fun.

What are you buying with Quantum:: that I don't get with my plain $distribution->[ rand @$distribution ]? Outside of having a really bloated module?

package Quaint; use strict; use Tie::Scalar; use Carp; use UNIVERSAL 'isa'; sub TIESCALAR { my ( $pck, @distribution ) = @_; croak "No distribution given." unless @distribution; bless { data => \ @distribution, state => $state }, $pck; } sub FETCH { my $self = shift; my $distribution = $self->{distribution}; $distribution->[ rand @$distribution ]; } sub STORE { my ( $self, $distref ) = @_; croak "No distribution given." unless isa($distref, "ARRAY"); my $orig_dist = $self->{'distribution'}; push @$orig_dist, @$distref; }

Replies are listed 'Best First'.
Re: Re: Tied and Entangled, Quantum fun.
by pernod (Chaplain) on Jul 25, 2003 at 17:39 UTC
    What are you buying with Quantum:: that I don't get with my plain $distribution-> rand @$distribution ? Outside of having a really bloated module?

    Nothing, really. But does your version provide any other distributions than uniform? At first glance, it seems to me that your solution lacks the possibility to provide the frequency of the different elements. That can be worked around using the following (untested) code, though.

    My version:

    tie my $variable, "Quaint", ( 1 => "one", 2 => "two", 1 => "three" );

    Your version:

    tie my $variable, "Quaint",( "one", "two", "two", "three" );

    I happen to prefer the first notation, but that is merely a matter of taste. On the other hand, I think you loose the possibility to specify frequencies using complex number. I may be wrong on this one though, so I agree that the only thing I loose with your version is Quantum::Cool.

    Come to think of it, I can't use complex numbers to specify frequencies right now. Thank you for pointing it out for me :o)

    pernod
    --
    Mischief. Mayhem. Soap.