REBOL 3 Docs | Guide | Concepts | Functions | Datatypes | Errors |
TOC < Back Next > | Updated: 6-Feb-2009 Edit History |
There are a few functions that operate on series as data sets. These functions allow you to perform operations such as finding the union or intersection between two series.
The unique function returns a unique set that contains no duplicate values.
Examples:
data: [Bill Betty Bob Benny Bart Bob Bill Bob]
probe unique data
[Bill Betty Bob Benny Bart]
print unique "abracadabra"
abrcd
The intersect function takes two series and returns a series that contains the values that are present in both series.
Examples:
probe intersect [Bill Bob Bart] [Bob Ted Fred]
[Bob]
lunch: [ham cheese bread carrot]
dinner: [ham salad carrot rice]
probe intersect lunch dinner
[ham carrot]
print intersect [1 3 2 4] [3 5 4 6]
3 4
string1: "CBAD" ; A B C D scrambled
string2: "EDCF" ; C D E F scrambled
print sort intersect string1 string2
CD
The intersection can be found between bitsets:
all-chars: "ABCDEFGHI"
charset1: charset "ABCDEF"
charset2: charset "DEFGHI"
charset3: intersect charset1 charset2
print find charset3 "E"
true
print find charset3 "B"
false
The /case refinement allows case-sensitive intersection:
probe intersect/case [Bill bill Bob bob] [Bart bill Bob]
[bill Bob]
The union function takes two series and returns a series that contains all the values from both series, but no duplicates.
Examples:
probe union [Bill Bob Bart] [Bob Ted Fred]
[Bill Bob Bart Ted Fred]
lunch: [ham cheese bread carrot]
dinner: [ham salad carrot rice]
probe union lunch dinner
[ham cheese bread carrot salad rice]
print union [1 3 2 4] [3 5 4 6]
1 3 2 4 5 6
string1: "CBDA" ; A B C D scrambled
string2: "EDCF" ; C D E F scrambled
print sort union string1 string2
ABCDEF
The union function can also be used on bitsets:
charset1: charset "ABCDEF"
charset2: charset "DEFGHI"
charset3: union charset1 charset2
print find charset3 "C"
true
print find charset3 "G"
true
The /case refinement allows case-sensitive unions:
probe union/case [Bill bill Bob bob] [bill Bob]
[Bill bill Bob bob]
The exclude function takes two series and returns a series that contains all the values of the first series, less the values of the second.
probe exclude [1 2 3 4] [1 2 3 5]
[4]
probe exclude [Bill Bob Bart] [Bob Ted Fred]
[Bill Bart]
lunch: [ham cheese bread carrot]
dinner: [ham salad carrot rice]
probe exclude lunch dinner
[cheese bread]
string1: "CBAD" ; A B C D scrambled
string2: "EDCF" ; C D E F scrambled
print sort difference string1 string2
AB
The /case refinement allows case-sensitive exclusion:
probe exclude/case [Bill bill Bob bob] [Bart bart bill Bob]
[Bill bob]
The difference function takes two series and returns a series that contains all of the values not in common with both series.
Examples:
probe difference [1 2 3 4] [1 2 3 5]
[4 5]
probe difference [Bill Bob Bart] [Bob Ted Fred]
[Bill Bart Ted Fred]
lunch: [ham cheese bread carrot]
dinner: [ham salad carrot rice]
probe difference lunch dinner
[cheese bread salad rice]
string1: "CBAD" ; A B C D scrambled
string2: "EDCF" ; C D E F scrambled
print sort difference string1 string2
ABEF
The /case refinement allows case-sensitive differences.
probe difference/case [Bill bill Bob bob] [Bart bart bill Bob]
[Bill bob Bart bart]
A variation of the difference function is the exclude function. It returns the values that are in the first series but not found in the second series. For example:
probe exclude [1 2 3 4] [1 2 3 5]
[4]
Notice that the above result does not contain 5, as was the case with difference in the prior section.
probe exclude [Bill Bob Bart] [Bob Ted Fred]
[Bill Bart]
probe exclude "abcde" "ace"
"bd"
TOC < Back Next > | REBOL.com - WIP Wiki | Feedback Admin |