-
-
Save rockymeza/1244432 to your computer and use it in GitHub Desktop.
relation algebra
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Relation(schema, data) | |
{ | |
this.schema = schema; | |
this.data = data; | |
// only supports equals for now | |
this.s = function(attribute, value) { | |
var index = this.schema.indexOf(attribute); | |
var new_data = this.data.filter(function(datum) { | |
return datum[index] == value; | |
}); | |
return new Relation(schema, new_data); | |
} | |
this.p = function(attribute/* , attributeN */) { | |
var schema = []; | |
var mapping = {}; | |
for ( var i = 0, _len = arguments.length; i < _len; i++) | |
{ | |
var attribute = arguments[i]; | |
schema.push(attribute); | |
mapping[attribute] = this.schema.indexOf(attribute); | |
}; | |
var new_data = this.data.map(function(datum) { | |
var new_datum = []; | |
for (attribute in mapping) | |
{ | |
new_datum.push(datum[mapping[attribute]]); | |
}; | |
return new_datum; | |
}); | |
return new Relation(schema, new_data); | |
}; | |
}; | |
// db | |
Student = new Relation(['sID', 'sName', 'GPA', 'sizeHS'], | |
[ | |
[1, 'Rocky', 4.0, 1000] | |
, [2, 'Gavin', 3.9, 800] | |
, [3, 'Paula', 4.2, 2200] | |
]); | |
Application = new Relation(['sID', 'cName', 'major', 'dec'], | |
[ | |
[1, 'UCD', 'PSCI', 'A'] | |
, [1, 'MIT', 'CS', 'A'] | |
, [2, 'MIT', 'CS', 'R'] | |
, [3, 'Metro', 'CS', 'A'] | |
]); | |
// tests | |
console.log(Student.s('sName', 'Rocky')); | |
console.log(Student.s('sName', 'Rocky').p('sID')); | |
console.log(Student.p('sID', 'GPA')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment