Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow extractors to return any foldable structure #256

Merged
merged 1 commit into from
Apr 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 35 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,8 @@
// init :: Array a -> Array a
function init(xs) { return xs.slice (0, -1); }

// isEmpty :: Array a -> Boolean
function isEmpty(xs) { return xs.length === 0; }
// isEmpty :: Foldable f => f a -> Boolean
function isEmpty(xs) { return Z.size (xs) === 0; }

// isPrefix :: Array a -> Array a -> Boolean
function isPrefix(candidate) {
Expand Down Expand Up @@ -327,6 +327,15 @@
return s.slice ('('.length, -')'.length);
}

// toArray :: Foldable f => f a -> Array a
function toArray(foldable) {
return Array.isArray (foldable) ?
foldable :
Z.reduce (function(xs, x) { xs.push (x); return xs; },
[],
foldable);
}

// toMarkdownList :: (String, String, a -> String, Array a) -> String
function toMarkdownList(empty, s, f, xs) {
return isEmpty (xs) ?
Expand Down Expand Up @@ -426,10 +435,11 @@
) {
var t = Object.create (Type$prototype);
t._test = test;
t.extractors = tuples.reduce (function(extractors, tuple) {
extractors[tuple[0]] = tuple[1];
return extractors;
t._extractors = tuples.reduce (function(_extractors, tuple) {
_extractors[tuple[0]] = tuple[1];
return _extractors;
}, {});
t.extractors = Z.map (B (toArray), t._extractors);
t.format = format;
t.keys = tuples.map (function(tuple) { return tuple[0]; });
t.name = name;
Expand Down Expand Up @@ -685,7 +695,7 @@
('Maybe')
([])
(typeEq ('sanctuary-maybe/Maybe@1'))
(function(maybe) { return maybe.isJust ? [maybe.value] : []; });
(I);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉


//# NonEmpty :: Type -> Type
//.
Expand Down Expand Up @@ -909,11 +919,7 @@
('StrMap')
([Object_])
(K (true))
(function(strMap) {
return Z.reduce (function(xs, x) { xs.push (x); return xs; },
[],
strMap);
});
(I);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉


//# String :: Type
//.
Expand Down Expand Up @@ -1493,7 +1499,7 @@
};
}

//# UnaryType :: String -> String -> Array Type -> (Any -> Boolean) -> (t a -> Array a) -> Type -> Type
//# UnaryType :: Foldable f => String -> String -> Array Type -> (Any -> Boolean) -> (t a -> f a) -> Type -> Type
//.
//. Type constructor for types with one type variable (such as [`Array`][]).
//.
Expand All @@ -1509,8 +1515,8 @@
//. the given supertypes, and returns `true` if (and only if) the value
//. is a member of `t x` for some type `x`;
//.
//. - a function that takes any value of type `t a` and returns an array
//. of the values of type `a` contained in the `t`; and
//. - a function that takes any value of type `t a` and returns the values
//. of type `a` contained in the `t`; and
//.
//. - the type of `a`.
//.
Expand Down Expand Up @@ -1605,10 +1611,10 @@
(t.url)
(t.supertypes)
(t._test ([]))
(t.extractors.$1);
(t._extractors.$1);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We provide the user-provided extractor function here to avoid B (toArray) (B (toArray) (_1)).

}

//# BinaryType :: String -> String -> Array Type -> (Any -> Boolean) -> (t a b -> Array a) -> (t a b -> Array b) -> Type -> Type -> Type
//# BinaryType :: Foldable f => String -> String -> Array Type -> (Any -> Boolean) -> (t a b -> f a) -> (t a b -> f b) -> Type -> Type -> Type
//.
//. Type constructor for types with two type variables (such as
//. [`Array2`][]).
Expand All @@ -1625,11 +1631,11 @@
//. the given supertypes, and returns `true` if (and only if) the value
//. is a member of `t x y` for some types `x` and `y`;
//.
//. - a function that takes any value of type `t a b` and returns an array
//. of the values of type `a` contained in the `t`;
//. - a function that takes any value of type `t a b` and returns the
//. values of type `a` contained in the `t`;
//.
//. - a function that takes any value of type `t a b` and returns an array
//. of the values of type `b` contained in the `t`;
//. - a function that takes any value of type `t a b` and returns the
//. values of type `b` contained in the `t`;
//.
//. - the type of `a`; and
//.
Expand Down Expand Up @@ -1742,8 +1748,8 @@
(t.url)
(t.supertypes)
(t._test ([]))
(t.extractors.$1)
(t.extractors.$2);
(t._extractors.$1)
(t._extractors.$2);
}

//# EnumType :: String -> String -> Array Any -> Type
Expand Down Expand Up @@ -1848,7 +1854,7 @@
var missing = {};
keys.forEach (function(k) { missing[k] = k; });
for (var k in x) delete missing[k];
return isEmpty (Object.keys (missing));
return isEmpty (missing);
};
}

Expand Down Expand Up @@ -1932,7 +1938,7 @@
var missing = {};
keys.forEach (function(k) { missing[k] = k; });
for (var k in x) delete missing[k];
return isEmpty (Object.keys (missing)) &&
return isEmpty (missing) &&
keys.every (function(k) {
return test2 (x[k]) (fields[k]);
});
Expand Down Expand Up @@ -2841,26 +2847,26 @@
(NullaryType),
UnaryType:
def ('UnaryType')
({})
({f: [Z.Foldable]})
([String_,
String_,
Array_ (Type),
Unchecked ('(Any -> Boolean)'),
Unchecked ('(t a -> Array a)'),
Unchecked ('(t a -> f a)'),
Unchecked ('Type -> Type')])
(function(name) {
return B (B (B (B (def (name) ({}) ([Type, Type])))))
(UnaryType (name));
}),
BinaryType:
def ('BinaryType')
({})
({f: [Z.Foldable]})
([String_,
String_,
Array_ (Type),
Unchecked ('(Any -> Boolean)'),
Unchecked ('(t a b -> Array a)'),
Unchecked ('(t a b -> Array b)'),
Unchecked ('(t a b -> f a)'),
Unchecked ('(t a b -> f b)'),
Unchecked ('Type -> Type -> Type')])
(function(name) {
return B (B (B (B (B (def (name) ({}) ([Type, Type, Type]))))))
Expand Down
4 changes: 2 additions & 2 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3344,7 +3344,7 @@ suite ('UnaryType', () => {
test ('is a quaternary function', () => {
eq (typeof $.UnaryType) ('function');
eq ($.UnaryType.length) (1);
eq (show ($.UnaryType)) ('UnaryType :: String -> String -> Array Type -> (Any -> Boolean) -> (t a -> Array a) -> Type -> Type');
eq (show ($.UnaryType)) ('UnaryType :: Foldable f => String -> String -> Array Type -> (Any -> Boolean) -> (t a -> f a) -> Type -> Type');
});

test ('returns a type constructor which type checks its arguments', () => {
Expand Down Expand Up @@ -3395,7 +3395,7 @@ suite ('BinaryType', () => {
test ('is a quinary function', () => {
eq (typeof $.BinaryType) ('function');
eq ($.BinaryType.length) (1);
eq (show ($.BinaryType)) ('BinaryType :: String -> String -> Array Type -> (Any -> Boolean) -> (t a b -> Array a) -> (t a b -> Array b) -> Type -> Type -> Type');
eq (show ($.BinaryType)) ('BinaryType :: Foldable f => String -> String -> Array Type -> (Any -> Boolean) -> (t a b -> f a) -> (t a b -> f b) -> Type -> Type -> Type');
});

test ('returns a type constructor which type checks its arguments', () => {
Expand Down