[PHP] Re: Arbitrary mathematical relations, not just hashes

2008-04-07 Thread Jenda Krynicky
From: "Kelly Jones" <[EMAIL PROTECTED]>
> 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.

One of the reasons why you can't find them as easily is efficiency. 
If people could ask for all items in %color whose value is 'red', 
they would. Without noticing that it's much more expensive than the 
other way around.

@items = grep {$color{$_} eq 'red'} keys %color;

is not too complex, but it complex enough to suggest that there is 
more work involved. The only way both the direction could be equaly 
complex would be to keep two hash tables internaly instead of one. 
Which would double the memory consumption.

If you do need to do this more often you can build an inverse data 
structure. Which if it was a one-to-one relation would be just

 %inv_color = reverse %color;

in Perl, of course once the relation is many to one than the inverse 
one will be more complex to build.

  my %inv_color;
  while (my ($k,$v) = each %color) {
push @{$inv_color{$v}}, $k;
  }

and of course will be harder to work with. But there is no way around 
that.


What is it you are actually trying to acomplish?

Jenda
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


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



[PHP] Re: Arbitrary mathematical relations, not just hashes

2008-04-07 Thread Jenda Krynicky
From: Julian Leviston <[EMAIL PROTECTED]>
> You could use ActiveRecord.

Without a database? I guess not. You'd still need at least SQLite. 
But you are right, you could use ActiveRecord to obtain a nice object 
oriented wrapper around the database so that it doesn't scare you.

Or, assuming you do store that data into a database table (of course 
using indexes on both columns) you could use Perl and choose how to 
access the data. With OO or without OO (horrors).

> Julian.
> 
> Learn Ruby on Rails!

I always wondered what happens to a ruby lying on rails when a train 
finaly comes ...
= [EMAIL PROTECTED] === http://Jenda.Krynicky.cz =
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
-- Terry Pratchett in Sourcery


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



[PHP] Re: Arbitrary mathematical relations, not just hashes

2008-04-06 Thread Mr. Shawn H. Corey
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.0002 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