Skip to content

Commit

Permalink
Merge pull request #142 from sanctuary-js/davidchambers/function-types
Browse files Browse the repository at this point in the history
accurately represent types of partially applied functions
  • Loading branch information
davidchambers authored Apr 28, 2017
2 parents 2189b6f + b16e6e9 commit 2775f34
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
36 changes: 29 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,6 @@
// K :: a -> b -> a
function K(x) { return function(y) { return x; }; }

// always :: a -> (-> a)
function always(x) { return function() { return x; }; }

// always2 :: a -> (b, c) -> a
function always2(x) { return function(y, z) { return x; }; }

Expand Down Expand Up @@ -327,6 +324,9 @@
};
}

// parenthesize :: String -> String
var parenthesize = wrap('(')(')');

// q :: String -> String
var q = wrap('\u2018')('\u2019');

Expand Down Expand Up @@ -2076,9 +2076,7 @@
var arity = reprs.length - 1;
return typeInfo.name + ' :: ' +
constraintsRepr(typeInfo.constraints, id, K(K(id))) +
when(arity === 0,
wrap('(')(')'),
init(reprs).join(' -> ')) +
when(arity === 0, parenthesize, init(reprs).join(' -> ')) +
' -> ' + last(reprs);
}

Expand Down Expand Up @@ -2313,6 +2311,7 @@
impl // :: Function
) {
var n = typeInfo.types.length - 1;

var curried = arity(_indexes.length, function() {
if (opts.checkTypes) {
var delta = _indexes.length - arguments.length;
Expand Down Expand Up @@ -2367,7 +2366,30 @@
return curry(opts, typeInfo, typeVarMap, values, indexes, impl);
}
});
curried.inspect = curried.toString = always(typeSignature(typeInfo));

curried.inspect = curried.toString = function() {
var vReprs = [];
var tReprs = [];
for (var idx = 0, placeholders = 0; idx < n; idx += 1) {
if (_indexes.indexOf(idx) >= 0) {
placeholders += 1;
tReprs.push(showType(typeInfo.types[idx]));
} else {
while (placeholders > 0) {
vReprs.push('__');
placeholders -= 1;
}
vReprs.push(Z.toString(_values[idx]));
}
}
return typeInfo.name +
when(vReprs.length > 0, parenthesize, vReprs.join(', ')) +
' :: ' +
constraintsRepr(typeInfo.constraints, id, K(K(id))) +
when(n === 0, parenthesize, tReprs.join(' -> ')) +
' -> ' + showType(typeInfo.types[n]);
};

return curried;
}

Expand Down
13 changes: 13 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,19 @@ describe('def', function() {
eq($7.toString(), '$7 :: a -> a -> a -> a -> a -> a -> a -> Array a');
eq($8.toString(), '$8 :: a -> a -> a -> a -> a -> a -> a -> a -> Array a');
eq($9.toString(), '$9 :: a -> a -> a -> a -> a -> a -> a -> a -> a -> Array a');

var __ = $.__;

eq($3(10).toString(), '$3(10) :: a -> a -> Array a');
eq($3(10, __).toString(), '$3(10) :: a -> a -> Array a');
eq($3(__, 20).toString(), '$3(__, 20) :: a -> a -> Array a');
eq($3(10, 20).toString(), '$3(10, 20) :: a -> Array a');
eq($3(10, __, __).toString(), '$3(10) :: a -> a -> Array a');
eq($3(__, 20, __).toString(), '$3(__, 20) :: a -> a -> Array a');
eq($3(__, __, 30).toString(), '$3(__, __, 30) :: a -> a -> Array a');
eq($3(10, 20, __).toString(), '$3(10, 20) :: a -> Array a');
eq($3(10, __, 30).toString(), '$3(10, __, 30) :: a -> Array a');
eq($3(__, 20, 30).toString(), '$3(__, 20, 30) :: a -> Array a');
});

it('returns a curried function', function() {
Expand Down

0 comments on commit 2775f34

Please sign in to comment.