Arbitrary mathematical relations, not just hashes

K

Kelly Jones

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?
 
D

Daniel Finnie

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
 
E

Eric Mahurin

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

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/wiki/Kd-tree

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

M. Edward (Ed) Borasky

Julian said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top