Skip to content

Commit 97b7e60

Browse files
committed
Merge pull request #53 from plaid/dc-tuples
permit the use of arrays as tuples
2 parents ee916c0 + c5cca38 commit 97b7e60

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

index.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,27 +1009,30 @@
10091009
});
10101010

10111011
// _determineActualTypes :: (Boolean, [Object], [Any]) -> [Type]
1012-
var _determineActualTypes = function recur(loose, $seen, values) {
1012+
var _determineActualTypes = function recur(loose, seen, values) {
10131013
if (isEmpty(values)) return [Unknown];
10141014
// typeses :: [[Type]]
10151015
var typeses = map(values, function(value) {
1016+
var seen$;
10161017
if (typeof value === 'object' && value != null ||
10171018
typeof value === 'function') {
10181019
// Abort if a circular reference is encountered; add the current
10191020
// object to the list of seen objects otherwise.
1020-
if ($seen.indexOf(value) >= 0) return [];
1021-
$seen.push(value);
1021+
if (seen.indexOf(value) >= 0) return [];
1022+
seen$ = seen.concat([value]);
1023+
} else {
1024+
seen$ = seen;
10221025
}
10231026
return chain(env, function(t) {
10241027
return (
10251028
t.name === 'sanctuary-def/Nullable' || !test(t, value).valid ?
10261029
[] :
10271030
t.type === 'UNARY' ?
1028-
map(recur(loose, $seen, t._1(value)), UnaryType.from(t)) :
1031+
map(recur(loose, seen$, t._1(value)), UnaryType.from(t)) :
10291032
t.type === 'BINARY' ?
10301033
BinaryType.xprod(t,
1031-
recur(loose, $seen, t._1(value)),
1032-
recur(loose, $seen, t._2(value))) :
1034+
recur(loose, seen$, t._1(value)),
1035+
recur(loose, seen$, t._2(value))) :
10331036
// else
10341037
[t]
10351038
);

test/index.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,6 +1743,39 @@ describe('def', function() {
17431743
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
17441744
});
17451745

1746+
it('permits the use of arrays as tuples', function() {
1747+
// Pair :: Type
1748+
var Pair = $.BinaryType(
1749+
'my-package/Pair',
1750+
R.both(R.is(Array), R.propEq('length', 2)),
1751+
R.compose(R.of, R.nth(0)),
1752+
R.compose(R.of, R.nth(1))
1753+
);
1754+
1755+
var env = $.env.concat([Either, Pair]);
1756+
var def = $.create(true, env);
1757+
1758+
// id :: a -> a
1759+
var id = def('id', {}, [a, a], R.identity);
1760+
1761+
eq(id(['abc', 123]), ['abc', 123]);
1762+
eq(id([Left('abc'), 123]), [Left('abc'), 123]);
1763+
eq(id(['abc', Right(123)]), ['abc', Right(123)]);
1764+
eq(id([Left('abc'), Right(123)]), [Left('abc'), Right(123)]);
1765+
1766+
throws(function() { id([Left('abc'), 123, 456]); },
1767+
errorEq(TypeError,
1768+
'Type-variable constraint violation\n' +
1769+
'\n' +
1770+
'id :: a -> a\n' +
1771+
' ^\n' +
1772+
' 1\n' +
1773+
'\n' +
1774+
'1) [Left("abc"), 123, 456] :: Array ???\n' +
1775+
'\n' +
1776+
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
1777+
});
1778+
17461779
it('supports values of "foreign" types', function() {
17471780
// id :: a -> a
17481781
var id = def('id', {}, [a, a], R.identity);

0 commit comments

Comments
 (0)