Re: Arbitrary mathematical relations, not just hashes

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, 2008-04-06 at 16:52 -0700, Kelly Jones wrote:
> Many programming languages (including Perl, Ruby, and PHP) support hashes:
> 
> $color['apple'] = 'red';
> $color['ruby'] = 'red';
> 
> $type['apple'] = 'fruit';
> $type['ruby'] = 'gem';
> 
> This quickly lets me find the color or type of a given item.
> 
> In this sense, color() and type() are like mathematical functions.
> 
> However, I can't easily find all items whose $color is 'red', nor all
> items whose $type is 'fruit'. In other words, color() and type()
> aren't full mathematical relations.
> 
> Of course, I could create the inverse function as I go along:
> 
> $inverse_color['red'] = ['apple', 'ruby']; # uglyish, assigning list to value
> 
> and there are many other ways to do this, but they all seem kludgey.
> 
> Is there a clean way to add 'relation' support to Perl, Ruby, or PHP?

Yes, create a class/object to handle the relationship.  That way, the
details are hidden and may be anything you want.

E.g.

use Relationship;

my $color = new Relatinship;
$color->add( 'apple', 'red' );
$color->add( 'ruby', 'red' );

my $type = new Relationship;
$type->add( 'apple', 'fruit' );
$type->add( 'ruby', 'gem' );

my @red_things = $color->find( undef, 'red' );


-- 
Just my 0.00000002 million dollars worth,
    Shawn

99% of you are just big dumb apes!

+------------\
| Shangri La   \
| 40,000 KM     /
+------------/


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux