From 2fc09c20e210c935d27befd55a02f31e863ef283 Mon Sep 17 00:00:00 2001 From: David Chambers Date: Mon, 26 Sep 2016 00:48:19 +0200 Subject: [PATCH] sanctuary-def@0.6.x --- index.js | 207 ++++++++++++++++++++++++++++++++++++--------------- package.json | 2 +- 2 files changed, 146 insertions(+), 63 deletions(-) diff --git a/index.js b/index.js index fb07f655..841cca1a 100644 --- a/index.js +++ b/index.js @@ -217,16 +217,89 @@ }; // Accessible :: TypeClass - var Accessible = Z.TypeClass( + var Accessible = $.TypeClass( 'sanctuary/Accessible', - [], function(x) { return x != null; } ); + // Applicative :: TypeClass + var Applicative = $.TypeClass( + 'sanctuary/Applicative', + function(x) { + return _type(x) === 'Array' || + Apply._test(x) && (hasMethod('of')(x) || + hasMethod('of')(x.constructor)); + } + ); + + // Apply :: TypeClass + var Apply = $.TypeClass( + 'sanctuary/Apply', + function(x) { + switch (_type(x)) { + case 'Array': + case 'Function': + return true; + default: + return Functor._test(x) && hasMethod('ap')(x); + } + } + ); + + // Chain :: TypeClass + var Chain = $.TypeClass( + 'sanctuary/Chain', + function(x) { + return _type(x) === 'Array' || hasMethod('chain')(x); + } + ); + + // Extend :: TypeClass + var Extend = $.TypeClass( + 'sanctuary/Extend', + hasMethod('extend') + ); + + // Foldable :: TypeClass + var Foldable = $.TypeClass( + 'sanctuary/Foldable', + function(x) { + switch (_type(x)) { + case 'Array': + case 'Object': + return true; + default: + return hasMethod('reduce')(x); + } + } + ); + + // Functor :: TypeClass + var Functor = $.TypeClass( + 'sanctuary/Functor', + function(x) { + switch (_type(x)) { + case 'Array': + case 'Function': + case 'Object': + return true; + default: + return hasMethod('map')(x); + } + } + ); + + // Monad :: TypeClass + var Monad = $.TypeClass( + 'sanctuary/Monad', + function(x) { + return Applicative._test(x) && Chain._test(x); + } + ); + // Monoid :: TypeClass - var Monoid = Z.TypeClass( + var Monoid = $.TypeClass( 'sanctuary/Monoid', - [], function(x) { switch (_type(x)) { case 'Array': @@ -241,9 +314,8 @@ ); // Ord :: TypeClass - var Ord = Z.TypeClass( + var Ord = $.TypeClass( 'sanctuary/Ord', - [], function(x) { return $.String._test(x) || $.ValidDate._test(x) || @@ -251,6 +323,18 @@ } ); + // Semigroup :: TypeClass + var Semigroup = $.TypeClass( + 'sanctuary/Semigroup', + hasMethod('concat') + ); + + // Traversable :: TypeClass + var Traversable = $.TypeClass( + 'sanctuary/Traversable', + hasMethod('traverse') + ); + var a = $.TypeVariable('a'); var b = $.TypeVariable('b'); var c = $.TypeVariable('c'); @@ -561,7 +645,7 @@ //. ``` var concat = S.concat = def('concat', - {a: [Z.Semigroup]}, + {a: [Semigroup]}, [a, a, a], Z.concat); @@ -610,8 +694,8 @@ //. ``` var map = S.map = def('map', - {a: [Z.Functor], b: [Z.Functor]}, - [$.AnyFunction, a, b], + {a: [Functor], b: [Functor]}, + [$.Function, a, b], Z.map); //# reduce :: Foldable f => (a -> b -> a) -> a -> f b -> a @@ -635,8 +719,8 @@ //. ``` var reduce = S.reduce = def('reduce', - {b: [Z.Foldable]}, - [$.AnyFunction, a, b, a], + {b: [Foldable]}, + [$.Function, a, b, a], function(f_, initial, foldable) { var f = function(a, b) { return f_(a)(b); @@ -649,8 +733,8 @@ //. Version of [`reduce`](#reduce) accepting uncurried functions. var reduce_ = S.reduce_ = def('reduce_', - {b: [Z.Foldable]}, - [$.AnyFunction, a, b, a], + {b: [Foldable]}, + [$.Function, a, b, a], Z.reduce); //# traverse :: (Applicative f, Traversable t) => (a -> f a) -> (a -> f b) -> t a -> f (t b) @@ -666,8 +750,8 @@ //. ``` S.traverse = def('traverse', - {a: [Z.Traversable], b: [Z.Applicative]}, - [$.AnyFunction, $.AnyFunction, a, b], + {a: [Traversable], b: [Applicative]}, + [$.Function, $.Function, a, b], Z.traverse); //# ap :: Apply f => f (a -> b) -> f a -> f b @@ -683,7 +767,7 @@ //. ``` var ap = S.ap = def('ap', - {a: [Z.Apply], b: [Z.Apply], f: [Z.Apply]}, + {a: [Apply], b: [Apply], f: [Apply]}, [a, b, c], Z.ap); @@ -700,8 +784,8 @@ //. ``` var chain = S.chain = def('chain', - {a: [Z.Chain], b: [Z.Chain]}, - [$.AnyFunction, a, b], + {a: [Chain], b: [Chain]}, + [$.Function, a, b], Z.chain); //# extend :: Extend e => (e a -> a) -> e a -> e a @@ -709,7 +793,7 @@ //. TK. S.extend = def('extend', - {a: [Z.Extend], b: [Z.Extend]}, + {a: [Extend], b: [Extend]}, [a, b, b], Z.extend); @@ -718,7 +802,7 @@ //. TK. S.extract = def('extract', - {a: [Z.Extend]}, + {a: [Extend]}, [a, b], Z.extract); @@ -740,8 +824,8 @@ //. ``` S.filter = def('filter', - {m: [Z.Monad, Z.Monoid]}, - [$.AnyFunction, m, m], + {m: [Monad, Monoid]}, + [$.Function, m, m], Z.filter); //# reject :: (Monad m, Monoid m a) => (a -> Boolean) -> m a -> m a @@ -762,8 +846,8 @@ //. ``` S.reject = def('reject', - {m: [Z.Monad, Z.Monoid]}, - [$.AnyFunction, m, m], + {m: [Monad, Monoid]}, + [$.Function, m, m], function(pred, m) { return Z.filter(function(x) { return !pred(x); }, m); }); @@ -819,7 +903,7 @@ S.A = def('A', {}, - [$.AnyFunction, a, b], + [$.Function, a, b], function(f, x) { return f(x); }); //# T :: a -> (a -> b) -> b @@ -838,7 +922,7 @@ S.T = def('T', {}, - [a, $.AnyFunction, b], + [a, $.Function, b], function(x, f) { return f(x); }); //# C :: (a -> b -> c) -> b -> a -> c @@ -861,7 +945,7 @@ S.C = def('C', {}, - [$.AnyFunction, b, a, c], + [$.Function, b, a, c], function(f, x, y) { return f(y)(x); }); //# B :: (b -> c) -> (a -> b) -> a -> c @@ -878,7 +962,7 @@ S.B = def('B', {}, - [$.AnyFunction, $.AnyFunction, a, c], + [$.Function, $.Function, a, c], compose3); //# S :: (a -> b -> c) -> (a -> b) -> a -> c @@ -896,7 +980,7 @@ S.S = def('S', {}, - [$.AnyFunction, $.AnyFunction, a, c], + [$.Function, $.Function, a, c], function(f, g, x) { return f(x)(g(x)); }); //. ### Function @@ -915,7 +999,7 @@ S.flip = def('flip', {}, - [$.AnyFunction, b, a, c], + [$.Function, b, a, c], function(f, x, y) { return f(y, x); }); //# lift :: Functor f => (a -> b) -> f a -> f b @@ -931,8 +1015,8 @@ //. ``` S.lift = def('lift', - {a: [Z.Functor], b: [Z.Functor]}, - [$.AnyFunction, a, b], + {a: [Functor], b: [Functor]}, + [$.Function, a, b], map); //# lift2 :: Apply f => (a -> b -> c) -> f a -> f b -> f c @@ -955,8 +1039,8 @@ //. ``` S.lift2 = def('lift2', - {a: [Z.Apply], b: [Z.Apply], c: [Z.Apply]}, - [$.AnyFunction, a, b, c], + {a: [Apply], b: [Apply], c: [Apply]}, + [$.Function, a, b, c], function(f, x, y) { return ap(map(f, x), y); }); //# lift3 :: Apply f => (a -> b -> c -> d) -> f a -> f b -> f c -> f d @@ -973,8 +1057,8 @@ //. ``` S.lift3 = def('lift3', - {a: [Z.Apply], b: [Z.Apply], c: [Z.Apply], d: [Z.Apply]}, - [$.AnyFunction, a, b, c, d], + {a: [Apply], b: [Apply], c: [Apply], d: [Apply]}, + [$.Function, a, b, c, d], function(f, x, y, z) { return ap(ap(map(f, x), y), z); }); //. ### Composition @@ -997,7 +1081,7 @@ S.compose = def('compose', {}, - [$.AnyFunction, $.AnyFunction, a, c], + [$.Function, $.Function, a, c], compose3); //# pipe :: [(a -> b), (b -> c), ..., (m -> n)] -> a -> n @@ -1018,7 +1102,7 @@ S.pipe = def('pipe', {}, - [$.Array($.AnyFunction), a, b], + [$.Array($.Function), a, b], function(fs, x) { return fs.reduce(function(x, f) { return f(x); }, x); }); @@ -1052,7 +1136,7 @@ S.meld = def('meld', {}, - [$.Array($.AnyFunction), $.AnyFunction], + [$.Array($.Function), $.Function], function(fs) { var types = [$.Any]; // return type fs.forEach(function(f, idx) { @@ -1527,7 +1611,7 @@ var maybe = S.maybe = def('maybe', {}, - [b, $.AnyFunction, $Maybe(a), b], + [b, $.Function, $Maybe(a), b], function(x, f, maybe) { return fromMaybe(x, maybe.map(f)); }); //# justs :: Array (Maybe a) -> Array a @@ -1564,7 +1648,7 @@ S.mapMaybe = def('mapMaybe', {}, - [$.AnyFunction, $.Array(a), $.Array(b)], + [$.Function, $.Array(a), $.Array(b)], function(f, xs) { return justs(map(f, xs)); }); //# encase :: (a -> b) -> a -> Maybe b @@ -1586,7 +1670,7 @@ var encase = S.encase = def('encase', {}, - [$.AnyFunction, a, $Maybe(b)], + [$.Function, a, $Maybe(b)], function(f, x) { try { return Just(f(x)); @@ -1603,7 +1687,7 @@ var encase2 = S.encase2 = def('encase2', {}, - [$.AnyFunction, a, b, $Maybe(c)], + [$.Function, a, b, $Maybe(c)], function(f, x, y) { try { return Just(f(x)(y)); @@ -1618,7 +1702,7 @@ S.encase2_ = def('encase2_', {}, - [$.AnyFunction, a, b, $Maybe(c)], + [$.Function, a, b, $Maybe(c)], function(f_, x, y) { var f = function(x) { return function(y) { @@ -1636,7 +1720,7 @@ var encase3 = S.encase3 = def('encase3', {}, - [$.AnyFunction, a, b, c, $Maybe(d)], + [$.Function, a, b, c, $Maybe(d)], function(f, x, y, z) { try { return Just(f(x)(y)(z)); @@ -1651,7 +1735,7 @@ S.encase3_ = def('encase3_', {}, - [$.AnyFunction, a, b, c, $Maybe(d)], + [$.Function, a, b, c, $Maybe(d)], function(f_, x, y, z) { var f = function(x) { return function(y) { @@ -2110,7 +2194,7 @@ S.either = def('either', {}, - [$.AnyFunction, $.AnyFunction, $Either(a, b), c], + [$.Function, $.Function, $Either(a, b), c], function(l, r, either) { return either.isLeft ? l(either.value) : r(either.value); }); @@ -2176,7 +2260,7 @@ S.encaseEither = def('encaseEither', {}, - [$.AnyFunction, $.AnyFunction, a, $Either(l, r)], + [$.Function, $.Function, a, $Either(l, r)], function(f, g, x) { try { return Right(g(x)); @@ -2193,7 +2277,7 @@ var encaseEither2 = S.encaseEither2 = def('encaseEither2', {}, - [$.AnyFunction, $.AnyFunction, a, b, $Either(l, r)], + [$.Function, $.Function, a, b, $Either(l, r)], function(f, g, x, y) { try { return Right(g(x)(y)); @@ -2209,7 +2293,7 @@ S.encaseEither2_ = def('encaseEither2_', {}, - [$.AnyFunction, $.AnyFunction, a, b, $Either(l, r)], + [$.Function, $.Function, a, b, $Either(l, r)], function(f, g_, x, y) { var g = function(x) { return function(y) { @@ -2227,7 +2311,7 @@ var encaseEither3 = S.encaseEither3 = def('encaseEither3', {}, - [$.AnyFunction, $.AnyFunction, a, b, c, $Either(l, r)], + [$.Function, $.Function, a, b, c, $Either(l, r)], function(f, g, x, y, z) { try { return Right(g(x)(y)(z)); @@ -2243,7 +2327,7 @@ S.encaseEither3_ = def('encaseEither3', {}, - [$.AnyFunction, $.AnyFunction, a, b, c, $Either(l, r)], + [$.Function, $.Function, a, b, c, $Either(l, r)], function(f, g_, x, y, z) { var g = function(x) { return function(y) { @@ -2280,9 +2364,8 @@ //. ### Alternative // Alternative :: TypeClass - var Alternative = Z.TypeClass( + var Alternative = $.TypeClass( 'Alternative', - [], function(x) { switch (_type(x)) { case 'Array': @@ -2408,7 +2491,7 @@ S.ifElse = def('ifElse', {}, - [$.AnyFunction, $.AnyFunction, $.AnyFunction, a, b], + [$.Function, $.Function, $.Function, a, b], function(pred, f, g, x) { return pred(x) ? f(x) : g(x); }); //# allPass :: Array (a -> Boolean) -> a -> Boolean @@ -2428,7 +2511,7 @@ S.allPass = def('allPass', {}, - [$.Array($.AnyFunction), a, $.Boolean], + [$.Array($.Function), a, $.Boolean], function(preds, x) { for (var idx = 0; idx < preds.length; idx += 1) { if (!preds[idx](x)) return false; @@ -2453,7 +2536,7 @@ S.anyPass = def('anyPass', {}, - [$.Array($.AnyFunction), a, $.Boolean], + [$.Array($.Function), a, $.Boolean], function(preds, x) { for (var idx = 0; idx < preds.length; idx += 1) { if (preds[idx](x)) return true; @@ -2848,7 +2931,7 @@ S.find = def('find', {}, - [$.AnyFunction, $.Array(a), $Maybe(a)], + [$.Function, $.Array(a), $Maybe(a)], function(pred, xs) { for (var idx = 0, len = xs.length; idx < len; idx += 1) { if (pred(xs[idx])) { @@ -2897,7 +2980,7 @@ S.unfoldr = def('unfoldr', {}, - [$.AnyFunction, b, $.Array(a)], + [$.Function, b, $.Array(a)], function(f, x) { var result = []; var m = f(x); @@ -3108,7 +3191,7 @@ //. ``` S.sum = def('sum', - {f: [Z.Foldable]}, + {f: [Foldable]}, [f, $.FiniteNumber], reduce(function(a) { return function(b) { return a + b; }; }, 0)); @@ -3187,7 +3270,7 @@ //. ``` S.product = def('product', - {f: [Z.Foldable]}, + {f: [Foldable]}, [f, $.FiniteNumber], reduce(function(a) { return function(b) { return a * b; }; }, 1)); @@ -3225,7 +3308,7 @@ //. ``` S.mean = def('mean', - {f: [Z.Foldable]}, + {f: [Foldable]}, [f, $Maybe($.FiniteNumber)], function(foldable) { var result = reduce_( diff --git a/package.json b/package.json index 37a930f1..88a41bcf 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "test": "make lint test" }, "dependencies": { - "sanctuary-def": "sanctuary-js/sanctuary-def#dc-sanctuary-type-classes", + "sanctuary-def": "0.6.x", "sanctuary-type-classes": "sanctuary-js/sanctuary-type-classes#dc-everything" }, "devDependencies": {