Skip to content

Instantly share code, notes, and snippets.

@andrejewski
Last active August 29, 2015 14:03
Show Gist options
  • Save andrejewski/e67b0b192a2acf1301f2 to your computer and use it in GitHub Desktop.
Save andrejewski/e67b0b192a2acf1301f2 to your computer and use it in GitHub Desktop.
a collection of Seth.js examples
/*
This gist aims to demonstrate how sets can
be compared in Seth.
All set-to-set comparison methods begin with
`is` such as `#isSuperset()`. And these methods
return a Proof class instance with useful
assertion methods.
Comparisons must be proved through assertions
as Seth is functional and the functions are
not evaluated until called with test data.
*/
var Seth = require('seth'),
Set = Seth.Set;
// comparison
var p1 = Seth.Everything.isSupersetOf(Seth.Numbers);
// assertion test
p1.confirms(1890); // true
p1.confirms("Kevin Spacey"); // true
// comparison
var p2 = Seth.Everything.isInverseOf(Seth.Nothing);
// assertion test
// we can really assert with any data, but..
p2.confirmsAll([1,23,4,5,6,7,8,9,10]); // true
// comparison
var p3 = Seth.Integers.isSubsetOf(Seth.Numbers);
// assertion test
p3.confirms(1890); // true
p3.confirms(Math.PI); // true
var p4 = Set([1,2,3]).isSupersetOf(Set([1,2,3,4,5]));
p4.confirmsAll([1,2,3,4,5]); // false
p4.confirmsAllExcept([1,2,3,4,5]) // [4,5]
/*
This gist aims to demonstrate the composable nature
of Seth's functional set theory.
Here, we define a universe of all strings called
`StringSet` using a simple function.
We then create two subsets of `StringSet` called
`SingleLineComment` and `MultiLineComment`.
To create a set of all possible JavaScript comments,
a union the two sets is created, named `CommentStringSet`.
All set-element assertions are done with
`Set#contains(element)`.
*/
var Seth = require('seth'),
Set = Seth.Set;
var StringSet = Set(function(x) {
return typeof Set === 'string';
});
// true
StringSet.contains('string');
StringSet.contains('Kevin Spacey');
// false
StringSet.contains(String);
StringSet.contains(1890);
var SingleLineComment = StringSet.subset(function(string) {
return string.trim().slice(0,2) === '//';
});
SingleLineComment.contains('// comment'); // true
SingleLineComment.contains('/* comment */'); // false
var MultiLineComment = StringSet.subset(function(string) {
var trimmed = string.trim(),
head = trimmed.slice(0,2),
tail = trimmed.slice(trimmed.length - 2);
return head === '/*' && tail === '*/';
});
MultiLineComment.contains('// comment'); // false
MultiLineComment.contains('/* comment */'); // true
var CommentStringSet = SingleLineComment.union(MultiLineComment);
// true
CommentStringSet.contains('// comment');
CommentStringSet.contains('/* comment */');
// false
CommentStringSet.contains('string');
CommentStringSet.contains('Kevin Spacey');
CommentStringSet.contains(String);
CommentStringSet.contains(Number);
var Seth = require('seth'),
Set = Seth.Set;
var A = Set([1,2,3]),
B = Set([1,2,4]),
C = Set([1,4]),
D = Set([]), // or Seth.Empty
E = Set([2,3]);
[A,B,C,D,E].filter(function(s) {
return s.contains(2) || s.contains(4);
}).length; // 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment