Relations are just a special form of sets, so all the operations available on sets are also available on relations. Your add operation would actually be
myReln += < "a", 1 >;
or
myReln = myReln + < "a", 1 >;
Removing items from a relation it similar to adding items to a relation. If you wanted to remove the tuple < "a", 1>
from myReln
, you would just write
myReln = myReln - < "a", 1 >;
or, as a shorthand
myReln -= < "a", 1 >;
If you don't know the entire tuple, but know you want to remove any tuple that starts with "a"
, you have (at least) two choices. The easiest is to use the domainX
function in the Relation
library:
import Relation;
myReln = domainX(myReln, {"a"});
This will remove any tuples that have "a"
as their first element. You could also have more than one item in this set, so if you want to remove any tuples starting with either "a"
or "b"
you could say:
myReln = domainX(myReln, {"a","b"});
The other option is to use pattern matching and comprehensions -- basically, to rebuild the relation by looking at each item in the relation and deciding if you should keep it. That would look like:
myReln = { <a,b> | <a,b> <- myReln, a != "a" };
Modification is then just some series of additions and deletions. Since the relations are immutable, we don't have a concept of in-place modification.
You can find documentation on Rascal sets here: http://tutor.rascal-mpl.org/Rascal/Rascal.html#/Rascal/Expressions/Values/Set/Set.html. Special operations defined just for relations (again, sets of tuples) is here: http://tutor.rascal-mpl.org/Rascal/Rascal.html#/Rascal/Expressions/Values/Relation/Relation.html