[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Arbitrary mathematical relations, not just hashes

Kelly Jones

4/6/2008 11:53:00 PM

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?

Is there a language that handles mathematical relations naturally/natively?

I realize SQL does all this and more, but that seems like overkill for
something this simple?

--
We're just a Bunch Of Regular Guys, a collective group that's trying
to understand and assimilate technology. We feel that resistance to
new ideas and technology is unwise and ultimately futile.

5 Answers

Daniel Finnie

4/7/2008 12:26:00 AM

0

type.find {|k,v| v == "red"} (will find one match)
type.find_all {|k,v| v == "red"} (will find all matchs)

Alternately, type.invert['red'] (will find one match)

Dan

On Sun, Apr 6, 2008 at 7:52 PM, Kelly Jones <kelly.terry.jones@gmail.com> 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?
>
> Is there a language that handles mathematical relations naturally/natively?
>
> I realize SQL does all this and more, but that seems like overkill for
> something this simple?
>
> --
> We're just a Bunch Of Regular Guys, a collective group that's trying
> to understand and assimilate technology. We feel that resistance to
> new ideas and technology is unwise and ultimately futile.
>
>

Eric Mahurin

4/7/2008 12:54:00 AM

0

[Note: parts of this message were removed to make it a legal post.]

On 4/6/08, Kelly Jones <kelly.terry.jones@gmail.com> wrote:
>
> 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.
>

Is there a language that handles mathematical relations naturally/natively?


Why "native" support? What's wrong with just a library/package/module/class
that does this? If you don't find what you want with various ruby database
API's out there, you could make your own data-structure for doing this. I
think one of the most generic structures for holding multi dimensional/keyed
data would be a kd-tree:

http://en.wikipedia.org/wi...

In a kd-tree, data is stored and accessed by multiple dimensions (or keys).
It is a great geometric data structure, but could also be used as a
database.

M. Edward (Ed) Borasky

4/7/2008 3:05:00 AM

0

Eric Mahurin wrote:
> On 4/6/08, Kelly Jones <kelly.terry.jones@gmail.com> wrote:
>> 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.
>>
>
> Is there a language that handles mathematical relations naturally/natively?

Prolog?


Julian Leviston

4/7/2008 4:02:00 AM

0

You could use ActiveRecord.

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) VIDEO #3
OUT NOW
http://sensei.ze...



On 07/04/2008, at 9:52 AM, 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?
>
> Is there a language that handles mathematical relations naturally/
> natively?
>
> I realize SQL does all this and more, but that seems like overkill for
> something this simple?
>
> --
> We're just a Bunch Of Regular Guys, a collective group that's trying
> to understand and assimilate technology. We feel that resistance to
> new ideas and technology is unwise and ultimately futile.
>






M. Edward (Ed) Borasky

4/7/2008 4:20:00 AM

0

Julian Leviston wrote:
> You could use ActiveRecord.

With SQLite as the database, it might be pretty fast unless the
relations are huge. If the problems are large, something like Erlang's
"Mnesia" database might be better than ActiveRecord.