Skip to content

Commit

Permalink
Merge pull request #101 from sanctuary-js/davidchambers/show
Browse files Browse the repository at this point in the history
remove ‘toString’
  • Loading branch information
davidchambers authored May 10, 2018
2 parents 6508fb9 + 43b6a99 commit cd0d111
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 207 deletions.
4 changes: 2 additions & 2 deletions bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* Install node modules required for the benchmark you wish to run
* Use `npm run bench -- --help` for options

For example, let's say you like to know how the performance of `toString`
For example, let's say you like to know how the performance of `equals`
compares to what it was at version 2.x.x:

```console
$ (cd bench && npm install [email protected])
$ npm run bench -- --benchmark old-vs-new --match methods.toString.*
$ npm run bench -- --benchmark old-vs-new --match methods.equals.*
```
3 changes: 0 additions & 3 deletions bench/old-vs-new.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ module.exports = benchmark(oldZ, newZ, {leftHeader: 'old', rightHeader: 'new'},
'methods.map.Identity': function(Z) { Z.map(inc, Identity(1)); },
'methods.sort.Array': function(Z) { Z.sort(shuffledArray); },
'methods.sort.List': function(Z) { Z.sort(shuffledList); },
'methods.toString.Identity': function(Z) { Z.toString(Identity(0)); },
'methods.toString.Object': function(Z) { Z.toString({x: 0, y: 0}); },
'methods.toString.String': function(Z) { Z.toString('hello'); },
'test.Comonad.Identity': function(Z) { Z.Comonad.test(Identity(0)); },
'test.Contravariant.Function': function(Z) { Z.Contravariant.test(Math.abs); }
}));
131 changes: 0 additions & 131 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -606,11 +606,6 @@
//. ```
var Contravariant = $('Contravariant', [], {contramap: Value});

// Null$prototype$toString :: Null ~> () -> String
function Null$prototype$toString() {
return 'null';
}

// Null$prototype$equals :: Null ~> Null -> Boolean
function Null$prototype$equals(other) {
return true;
Expand All @@ -621,11 +616,6 @@
return true;
}

// Undefined$prototype$toString :: Undefined ~> () -> String
function Undefined$prototype$toString() {
return 'undefined';
}

// Undefined$prototype$equals :: Undefined ~> Undefined -> Boolean
function Undefined$prototype$equals(other) {
return true;
Expand All @@ -636,13 +626,6 @@
return true;
}

// Boolean$prototype$toString :: Boolean ~> () -> String
function Boolean$prototype$toString() {
return typeof this === 'object' ?
'new Boolean(' + toString(this.valueOf()) + ')' :
this.toString();
}

// Boolean$prototype$equals :: Boolean ~> Boolean -> Boolean
function Boolean$prototype$equals(other) {
return typeof this === 'object' ?
Expand All @@ -657,13 +640,6 @@
this === false || other === true;
}

// Number$prototype$toString :: Number ~> () -> String
function Number$prototype$toString() {
return typeof this === 'object' ?
'new Number(' + toString(this.valueOf()) + ')' :
1 / this === -Infinity ? '-0' : this.toString(10);
}

// Number$prototype$equals :: Number ~> Number -> Boolean
function Number$prototype$equals(other) {
return typeof this === 'object' ?
Expand All @@ -678,12 +654,6 @@
isNaN(this) || this <= other;
}

// Date$prototype$toString :: Date ~> () -> String
function Date$prototype$toString() {
var x = isNaN(this.valueOf()) ? NaN : this.toISOString();
return 'new Date(' + toString(x) + ')';
}

// Date$prototype$equals :: Date ~> Date -> Boolean
function Date$prototype$equals(other) {
return equals(this.valueOf(), other.valueOf());
Expand All @@ -709,13 +679,6 @@
return '';
}

// String$prototype$toString :: String ~> () -> String
function String$prototype$toString() {
return typeof this === 'object' ?
'new String(' + toString(this.valueOf()) + ')' :
JSON.stringify(this);
}

// String$prototype$equals :: String ~> String -> Boolean
function String$prototype$equals(other) {
return typeof this === 'object' ?
Expand Down Expand Up @@ -775,19 +738,6 @@
return [];
}

// Array$prototype$toString :: Array a ~> () -> String
function Array$prototype$toString() {
var reprs = this.map(toString);
var keys = Object.keys(this).sort();
for (var idx = 0; idx < keys.length; idx += 1) {
var k = keys[idx];
if (!/^\d+$/.test(k)) {
reprs.push(toString(k) + ': ' + toString(this[k]));
}
}
return '[' + reprs.join(', ') + ']';
}

// Array$prototype$equals :: Array a ~> Array a -> Boolean
function Array$prototype$equals(other) {
if (other.length !== this.length) return false;
Expand Down Expand Up @@ -875,12 +825,6 @@
return this.map(function(_, idx, xs) { return f(xs.slice(idx)); });
}

// Arguments$prototype$toString :: Arguments ~> String
function Arguments$prototype$toString() {
var args = Array.prototype.map.call(this, toString).join(', ');
return '(function () { return arguments; }(' + args + '))';
}

// Arguments$prototype$equals :: Arguments ~> Arguments -> Boolean
function Arguments$prototype$equals(other) {
return Array$prototype$equals.call(this, other);
Expand All @@ -891,11 +835,6 @@
return Array$prototype$lte.call(this, other);
}

// Error$prototype$toString :: Error ~> () -> String
function Error$prototype$toString() {
return 'new ' + this.name + '(' + toString(this.message) + ')';
}

// Error$prototype$equals :: Error ~> Error -> Boolean
function Error$prototype$equals(other) {
return equals(this.name, other.name) &&
Expand All @@ -912,17 +851,6 @@
return {};
}

// Object$prototype$toString :: StrMap a ~> () -> String
function Object$prototype$toString() {
var reprs = [];
var keys = Object.keys(this).sort();
for (var idx = 0; idx < keys.length; idx += 1) {
var k = keys[idx];
reprs.push(toString(k) + ': ' + toString(this[k]));
}
return '{' + reprs.join(', ') + '}';
}

// Object$prototype$equals :: StrMap a ~> StrMap a -> Boolean
function Object$prototype$equals(other) {
var self = this;
Expand Down Expand Up @@ -1076,35 +1004,30 @@
var implementations = {
Null: {
'prototype': {
'toString': Null$prototype$toString,
'fantasy-land/equals': Null$prototype$equals,
'fantasy-land/lte': Null$prototype$lte
}
},
Undefined: {
'prototype': {
'toString': Undefined$prototype$toString,
'fantasy-land/equals': Undefined$prototype$equals,
'fantasy-land/lte': Undefined$prototype$lte
}
},
Boolean: {
'prototype': {
'toString': Boolean$prototype$toString,
'fantasy-land/equals': Boolean$prototype$equals,
'fantasy-land/lte': Boolean$prototype$lte
}
},
Number: {
'prototype': {
'toString': Number$prototype$toString,
'fantasy-land/equals': Number$prototype$equals,
'fantasy-land/lte': Number$prototype$lte
}
},
Date: {
'prototype': {
'toString': Date$prototype$toString,
'fantasy-land/equals': Date$prototype$equals,
'fantasy-land/lte': Date$prototype$lte
}
Expand All @@ -1117,7 +1040,6 @@
String: {
'fantasy-land/empty': String$empty,
'prototype': {
'toString': String$prototype$toString,
'fantasy-land/equals': String$prototype$equals,
'fantasy-land/lte': String$prototype$lte,
'fantasy-land/concat': String$prototype$concat
Expand All @@ -1129,7 +1051,6 @@
'fantasy-land/chainRec': Array$chainRec,
'fantasy-land/zero': Array$zero,
'prototype': {
'toString': Array$prototype$toString,
'fantasy-land/equals': Array$prototype$equals,
'fantasy-land/lte': Array$prototype$lte,
'fantasy-land/concat': Array$prototype$concat,
Expand All @@ -1145,22 +1066,19 @@
},
Arguments: {
'prototype': {
'toString': Arguments$prototype$toString,
'fantasy-land/equals': Arguments$prototype$equals,
'fantasy-land/lte': Arguments$prototype$lte
}
},
Error: {
'prototype': {
'toString': Error$prototype$toString,
'fantasy-land/equals': Error$prototype$equals
}
},
Object: {
'fantasy-land/empty': Object$empty,
'fantasy-land/zero': Object$zero,
'prototype': {
'toString': Object$prototype$toString,
'fantasy-land/equals': Object$prototype$equals,
'fantasy-land/lte': Object$prototype$lte,
'fantasy-land/concat': Object$prototype$concat,
Expand Down Expand Up @@ -1190,54 +1108,6 @@
};
/* eslint-enable key-spacing */

//# toString :: a -> String
//.
//. Returns a useful string representation of its argument.
//.
//. Dispatches to the argument's `toString` method if appropriate.
//.
//. Where practical, `equals(eval(toString(x)), x) = true`.
//.
//. `toString` implementations are provided for the following built-in types:
//. Null, Undefined, Boolean, Number, Date, String, Array, Arguments, Error,
//. and Object.
//.
//. ```javascript
//. > toString(-0)
//. '-0'
//.
//. > toString(['foo', 'bar', 'baz'])
//. '["foo", "bar", "baz"]'
//.
//. > toString({x: 1, y: 2, z: 3})
//. '{"x": 1, "y": 2, "z": 3}'
//.
//. > toString(Cons(1, Cons(2, Cons(3, Nil))))
//. 'Cons(1, Cons(2, Cons(3, Nil)))'
//. ```
var toString = (function() {
// $seen :: Array Any
var $seen = [];

function call(method, x) {
$seen.push(x);
try { return method.call(x); } finally { $seen.pop(); }
}

return function toString(x) {
if ($seen.indexOf(x) >= 0) return '<Circular>';

var xType = type(x);
if (xType === 'Object') {
var result;
try { result = call(x.toString, x); } catch (err) {}
if (result != null && result !== '[object Object]') return result;
}

return call(implPath([xType, 'prototype', 'toString']) || x.toString, x);
};
}());

//# equals :: (a, b) -> Boolean
//.
//. Returns `true` if its arguments are of the same type and equal according
Expand Down Expand Up @@ -2345,7 +2215,6 @@
Extend: Extend,
Comonad: Comonad,
Contravariant: Contravariant,
toString: toString,
equals: equals,
lt: lt,
lte: lte,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"jsverify": "0.8.x",
"list": "2.0.11",
"sanctuary-benchmark": "1.0.x",
"sanctuary-scripts": "1.5.x"
"sanctuary-scripts": "1.5.x",
"sanctuary-show": "1.0.x"
},
"files": [
"/LICENSE",
Expand Down
5 changes: 3 additions & 2 deletions test/Identity.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var FL = require('fantasy-land');
var show = require('sanctuary-show');

var Z = require('..');

Expand Down Expand Up @@ -56,8 +57,8 @@ Identity.prototype[FL.extract] = function() {
};

Identity.prototype.inspect =
Identity.prototype.toString = function() {
return 'Identity(' + Z.toString(this.value) + ')';
Identity.prototype['@@show'] = function() {
return 'Identity (' + show(this.value) + ')';
};

module.exports = Identity;
5 changes: 3 additions & 2 deletions test/List.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var FL = require('fantasy-land');
var show = require('sanctuary-show');

var Z = require('..');

Expand Down Expand Up @@ -93,10 +94,10 @@ List.prototype[FL.traverse] = function(typeRep, f) {
};

List.prototype.inspect =
List.prototype.toString = function() {
List.prototype['@@show'] = function() {
return this.isNil ?
'Nil' :
'Cons(' + Z.toString(this.head) + ', ' + Z.toString(this.tail) + ')';
'Cons (' + show(this.head) + ', ' + show(this.tail) + ')';
};

module.exports = List;
5 changes: 3 additions & 2 deletions test/Maybe.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var FL = require('fantasy-land');
var show = require('sanctuary-show');

var Z = require('..');

Expand Down Expand Up @@ -79,8 +80,8 @@ Maybe.prototype[FL.reduce] = function(f, x) {
};

Maybe.prototype.inspect =
Maybe.prototype.toString = function() {
return this.isJust ? 'Just(' + Z.toString(this.value) + ')' : 'Nothing';
Maybe.prototype['@@show'] = function() {
return this.isJust ? 'Just (' + show(this.value) + ')' : 'Nothing';
};

module.exports = Maybe;
5 changes: 3 additions & 2 deletions test/Sum.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

var FL = require('fantasy-land');
var show = require('sanctuary-show');

var Z = require('..');

Expand Down Expand Up @@ -28,8 +29,8 @@ Sum.prototype[FL.invert] = function() {
};

Sum.prototype.inspect =
Sum.prototype.toString = function() {
return 'Sum(' + Z.toString(this.value) + ')';
Sum.prototype['@@show'] = function() {
return 'Sum (' + show(this.value) + ')';
};

module.exports = Sum;
Loading

0 comments on commit cd0d111

Please sign in to comment.