naif
Naive sets.
The sets are limited in what they can store b/c are based on aspic. Anything that is not serializable by JSON.stringify cannot be stored.
example usage
var naif = require('naif');
// call the naif function with zero or more array arguments:
var e = naif();
e.empty(); //true
var s1 = naif(['hello','there', 'this'],['is', 'is', 'is', 'a', 'set']);
s1.size(); //6
var s2 = naif(['hello','again'],['one',2, 3.1415, {'six' : 7}]);
s2.size(); //6
// we can do set operations using logical language:
var s1_intersect_s2 = s1.and(s2);
s1_intersect_s2.size(); //1
s1_intersect_s2.each(console.log);
// hello
// we can chain several operators.
// the following prints out each string that is in the union of s1 and s2
s1.or(s2).suchthat(function(e) {return e.constructor === String}).each(console.log);
// hello
// there
// this
// is
// a
// set
// again
// one
api
naif()
The naif(array*)
function takes zero or more arrays and returns a set object;
set objects
Given a set called s
, the following functions are supported:
-
s.size()
: returns the number of elements in the set -
s.each(fn)
: the basic set iterator, wherefn
is a function of a single argument. -
s.or(s2)
: returns the union ofs
ands2
, where both are sets returned bynaif
-
s.and(s2)
: returns the intersection ofs
ands2
-
s.not(s2)
: returns the set difference,s
lesss2
-
s.xor(s2)
: returns those elements ins
ors2
but not in both -
s.empty()
: returnstrue
ifs
is the empty set -
s.all(p)
: returnstrue
ifp(e)
is true for eache
ins
-
s.exists(p)
: returnstrue
ifp(e)
is true for at least onee
ins
-
s.subset(s2)
: returnstrue
ifs
is a subset ofs2
-
s.superset(s2)
: returnstrue
ifs
is a superset ofs2
-
s.equal(s2)
: returnstrue
ifs
ands2
contain the same elements, up to the equality of their serialized contents (see aspic for details about serialization) -
s.toArray()
: returns an array of the set's contents. -
s.mutable()
: this extendss
with new methods for mutability, returning a new mutable set that shares memory withs
.
mutable set objects
Mutable sets have all of the functions above, plus two more:
-
ms.insert(x)
: insertsx
into the the set -
ms.remove(x)
: removesx
from the set
oh goodie.