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

replaced compose, curryN with own implementations #63

Closed
wants to merge 1 commit into from
Closed
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
277 changes: 62 additions & 215 deletions dist/union-type.js
Original file line number Diff line number Diff line change
@@ -1,203 +1,4 @@
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.unionType = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var _curry2 = require('./internal/_curry2');


/**
* Wraps a function of any arity (including nullary) in a function that accepts exactly `n`
* parameters. Unlike `nAry`, which passes only `n` arguments to the wrapped function,
* functions produced by `arity` will pass all provided arguments to the wrapped function.
*
* @func
* @memberOf R
* @sig (Number, (* -> *)) -> (* -> *)
* @category Function
* @param {Number} n The desired arity of the returned function.
* @param {Function} fn The function to wrap.
* @return {Function} A new function wrapping `fn`. The new function is
* guaranteed to be of arity `n`.
* @deprecated since v0.15.0
* @example
*
* var takesTwoArgs = function(a, b) {
* return [a, b];
* };
* takesTwoArgs.length; //=> 2
* takesTwoArgs(1, 2); //=> [1, 2]
*
* var takesOneArg = R.arity(1, takesTwoArgs);
* takesOneArg.length; //=> 1
* // All arguments are passed through to the wrapped function
* takesOneArg(1, 2); //=> [1, 2]
*/
module.exports = _curry2(function(n, fn) {
// jshint unused:vars
switch (n) {
case 0: return function() {return fn.apply(this, arguments);};
case 1: return function(a0) {return fn.apply(this, arguments);};
case 2: return function(a0, a1) {return fn.apply(this, arguments);};
case 3: return function(a0, a1, a2) {return fn.apply(this, arguments);};
case 4: return function(a0, a1, a2, a3) {return fn.apply(this, arguments);};
case 5: return function(a0, a1, a2, a3, a4) {return fn.apply(this, arguments);};
case 6: return function(a0, a1, a2, a3, a4, a5) {return fn.apply(this, arguments);};
case 7: return function(a0, a1, a2, a3, a4, a5, a6) {return fn.apply(this, arguments);};
case 8: return function(a0, a1, a2, a3, a4, a5, a6, a7) {return fn.apply(this, arguments);};
case 9: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8) {return fn.apply(this, arguments);};
case 10: return function(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9) {return fn.apply(this, arguments);};
default: throw new Error('First argument to arity must be a non-negative integer no greater than ten');
}
});

},{"./internal/_curry2":4}],2:[function(require,module,exports){
var _curry2 = require('./internal/_curry2');
var _curryN = require('./internal/_curryN');
var arity = require('./arity');


/**
* Returns a curried equivalent of the provided function, with the
* specified arity. The curried function has two unusual capabilities.
* First, its arguments needn't be provided one at a time. If `g` is
* `R.curryN(3, f)`, the following are equivalent:
*
* - `g(1)(2)(3)`
* - `g(1)(2, 3)`
* - `g(1, 2)(3)`
* - `g(1, 2, 3)`
*
* Secondly, the special placeholder value `R.__` may be used to specify
* "gaps", allowing partial application of any combination of arguments,
* regardless of their positions. If `g` is as above and `_` is `R.__`,
* the following are equivalent:
*
* - `g(1, 2, 3)`
* - `g(_, 2, 3)(1)`
* - `g(_, _, 3)(1)(2)`
* - `g(_, _, 3)(1, 2)`
* - `g(_, 2)(1)(3)`
* - `g(_, 2)(1, 3)`
* - `g(_, 2)(_, 3)(1)`
*
* @func
* @memberOf R
* @category Function
* @sig Number -> (* -> a) -> (* -> a)
* @param {Number} length The arity for the returned function.
* @param {Function} fn The function to curry.
* @return {Function} A new, curried function.
* @see R.curry
* @example
*
* var addFourNumbers = function() {
* return R.sum([].slice.call(arguments, 0, 4));
* };
*
* var curriedAddFourNumbers = R.curryN(4, addFourNumbers);
* var f = curriedAddFourNumbers(1, 2);
* var g = f(3);
* g(4); //=> 10
*/
module.exports = _curry2(function curryN(length, fn) {
return arity(length, _curryN(length, [], fn));
});

},{"./arity":1,"./internal/_curry2":4,"./internal/_curryN":5}],3:[function(require,module,exports){
/**
* Optimized internal two-arity curry function.
*
* @private
* @category Function
* @param {Function} fn The function to curry.
* @return {Function} The curried function.
*/
module.exports = function _curry1(fn) {
return function f1(a) {
if (arguments.length === 0) {
return f1;
} else if (a != null && a['@@functional/placeholder'] === true) {
return f1;
} else {
return fn(a);
}
};
};

},{}],4:[function(require,module,exports){
var _curry1 = require('./_curry1');


/**
* Optimized internal two-arity curry function.
*
* @private
* @category Function
* @param {Function} fn The function to curry.
* @return {Function} The curried function.
*/
module.exports = function _curry2(fn) {
return function f2(a, b) {
var n = arguments.length;
if (n === 0) {
return f2;
} else if (n === 1 && a != null && a['@@functional/placeholder'] === true) {
return f2;
} else if (n === 1) {
return _curry1(function(b) { return fn(a, b); });
} else if (n === 2 && a != null && a['@@functional/placeholder'] === true &&
b != null && b['@@functional/placeholder'] === true) {
return f2;
} else if (n === 2 && a != null && a['@@functional/placeholder'] === true) {
return _curry1(function(a) { return fn(a, b); });
} else if (n === 2 && b != null && b['@@functional/placeholder'] === true) {
return _curry1(function(b) { return fn(a, b); });
} else {
return fn(a, b);
}
};
};

},{"./_curry1":3}],5:[function(require,module,exports){
var arity = require('../arity');


/**
* Internal curryN function.
*
* @private
* @category Function
* @param {Number} length The arity of the curried function.
* @return {array} An array of arguments received thus far.
* @param {Function} fn The function to curry.
*/
module.exports = function _curryN(length, received, fn) {
return function() {
var combined = [];
var argsIdx = 0;
var left = length;
var combinedIdx = 0;
while (combinedIdx < received.length || argsIdx < arguments.length) {
var result;
if (combinedIdx < received.length &&
(received[combinedIdx] == null ||
received[combinedIdx]['@@functional/placeholder'] !== true ||
argsIdx >= arguments.length)) {
result = received[combinedIdx];
} else {
result = arguments[argsIdx];
argsIdx += 1;
}
combined[combinedIdx] = result;
if (result == null || result['@@functional/placeholder'] !== true) {
left -= 1;
}
combinedIdx += 1;
}
return left <= 0 ? fn.apply(this, combined) : arity(left, _curryN(length, combined, fn));
};
};

},{"../arity":1}],6:[function(require,module,exports){
var curryN = require('ramda/src/curryN');

var isString = function(s) { return typeof s === 'string'; };
var isNumber = function(n) { return typeof n === 'number'; };
var isBoolean = function(b) { return typeof b === 'boolean'; };
Expand All @@ -208,6 +9,23 @@ var isObject = function(value) {
var isFunction = function(f) { return typeof f === 'function'; };
var isArray = Array.isArray || function(a) { return 'length' in a; };

var curryN = function(length, fn) {
function _curry(args, received) {
var accumulated = args
for (var i = 0; i < received.length; i++) {
accumulated = accumulated.concat([received[i]]);
}

if (accumulated.length >= length) {
return fn.apply(this, accumulated);
} else {
return function() { return _curry(accumulated, arguments) };
}
}

return function() { return _curry([], arguments) };
}

var mapConstrToFn = function(group, constr) {
return constr === String ? isString
: constr === Number ? isNumber
Expand All @@ -223,21 +41,26 @@ var numToStr = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh

var validate = function(group, validators, name, args) {
var validator, v, i;
if (args.length > validators.length) {
throw new TypeError('too many arguments supplied to constructor ' + name
+ ' (expected ' + validators.length + ' but got ' + args.length + ')');
}
for (i = 0; i < args.length; ++i) {
v = args[i];
validator = mapConstrToFn(group, validators[i]);
if (Type.check === true &&
(validator.prototype === undefined || !validator.prototype.isPrototypeOf(v)) &&
(typeof validator !== 'function' || !validator(v))) {
throw new TypeError('wrong value ' + v + ' passed to location ' + numToStr[i] + ' in ' + name);
var strVal = typeof v === 'string' ? "'" + v + "'" : v; // put the value in quotes if it's a string
throw new TypeError('wrong value ' + strVal + ' passed as ' + numToStr[i] + ' argument to constructor ' + name);
}
}
};

function valueToArray(value) {
var i, arr = [];
for (i = 0; i < value.keys.length; ++i) {
arr.push(value[value.keys[i]]);
for (i = 0; i < value._keys.length; ++i) {
arr.push(value[value._keys[i]]);
}
return arr;
}
Expand All @@ -257,8 +80,8 @@ function constructor(group, name, fields) {
}
function construct() {
var val = Object.create(group.prototype), i;
val.keys = keys;
val.name = name;
val._keys = keys;
val._name = name;
if (Type.check === true) {
validate(group, validators, name, arguments);
}
Expand All @@ -267,7 +90,7 @@ function constructor(group, name, fields) {
}
return val;
}
group[name] = curryN(keys.length, construct);
group[name] = keys.length === 0 ? construct() : curryN(keys.length, construct);
if (keys !== undefined) {
group[name+'Of'] = function(obj) {
return construct.apply(undefined, extractValues(keys, obj));
Expand All @@ -277,7 +100,7 @@ function constructor(group, name, fields) {

function rawCase(type, cases, value, arg) {
var wildcard = false;
var handler = cases[value.name];
var handler = cases[value._name];
if (handler === undefined) {
handler = cases['_'];
wildcard = true;
Expand All @@ -289,10 +112,12 @@ function rawCase(type, cases, value, arg) {
throw new Error('non-exhaustive patterns in a function');
}
}
var args = wildcard === true ? [arg]
: arg !== undefined ? valueToArray(value).concat([arg])
: valueToArray(value);
return handler.apply(undefined, args);
if (handler !== undefined) {
var args = wildcard === true ? [arg]
: arg !== undefined ? valueToArray(value).concat([arg])
: valueToArray(value);
return handler.apply(undefined, args);
}
}

var typeCase = curryN(3, rawCase);
Expand All @@ -303,7 +128,7 @@ function createIterator() {
idx: 0,
val: this,
next: function() {
var keys = this.val.keys;
var keys = this.val._keys;
return this.idx === keys.length
? {done: true}
: {value: this.val[keys[this.idx++]]};
Expand All @@ -313,10 +138,14 @@ function createIterator() {

function Type(desc) {
var key, res, obj = {};
obj.prototype = {};
obj.prototype[Symbol ? Symbol.iterator : '@@iterator'] = createIterator;
obj.case = typeCase(obj);
obj.caseOn = caseOn(obj);

obj.prototype = {};
obj.prototype[Symbol ? Symbol.iterator : '@@iterator'] = createIterator;
obj.prototype.case = function (cases) { return obj.case(cases, this); };
obj.prototype.caseOn = function (cases) { return obj.caseOn(cases, this); };

for (key in desc) {
res = constructor(obj, key, desc[key]);
}
Expand All @@ -325,7 +154,25 @@ function Type(desc) {

Type.check = true;

Type.ListOf = function (T) {
var List = Type({List:[Array]});
var innerType = Type({T: [T]}).T;
var validate = List.case({
List: function (array) {
try {
for(var n = 0; n < array.length; n++) {
innerType(array[n]);
}
} catch (e) {
throw new TypeError('wrong value '+ array[n] + ' passed to location ' + numToStr[n] + ' in List');
}
return true;
}
});
return function() { return validate(List.List.apply(this, arguments)) };
};

module.exports = Type;

},{"ramda/src/curryN":2}]},{},[6])(6)
});
},{}]},{},[1])(1)
});
4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"directories": {
"test": "test"
},
"dependencies": {
"ramda": "^0.24.0"
},
"dependencies": {},
"devDependencies": {
"babel": "^5.8.21",
"browserify": "^10.2.4",
Expand Down
Loading