Last active
October 10, 2016 09:45
-
-
Save petsel/2a3561617a03e6e0e6c6 to your computer and use it in GitHub Desktop.
"Math.average", "Math.sum", "Array.flatten", "Array.shuffle", "Enumerable.shuffle", "Array.unique", "Enumerable.unique"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composable("composites.Array_flatten", function (require, global) { | |
"use strict"; | |
var | |
environment = require("environment_extended_introspective_core"), | |
environment_introspective = environment.introspective, | |
Array = global.Array, | |
isFunction = environment_introspective.isFunction, | |
isArray = environment_introspective.isArray, | |
isArguments = environment_introspective.isArguments, | |
array_from = (isFunction(Array.from) && Array.from) || environment.helpers.makeArray, | |
array_flatten = function flatten (list) { | |
list = (isArguments(list) && array_from(list)) || list; | |
if (isArray(list)) { | |
list = list.reduce(function (collector, elm) { | |
return collector.concat(flatten(elm)); | |
}, []); | |
} | |
return list; | |
} | |
// stay conservative as above - "smart" shortcut code from beneath will break: | |
// | |
// array_flatten = function flatten(list) { | |
// return ((isArray(list) && list) || (isArguments(list) && array_from(list))).reduce(function (collector, elm) { | |
// | |
// return collector.concat(flatten(elm)); | |
// | |
// }, []) || list; | |
// } | |
; | |
Array.prototype.flatten = function () { | |
return array_flatten(this); | |
}; | |
//return Array; | |
}); | |
(function (Array, Object) { | |
//"use strict"; | |
var | |
array_prototype = Array.prototype, | |
array_prototype_slice = array_prototype.slice, | |
expose_internal_class = Object.prototype.toString, | |
isArguments = function (type) { | |
return !!type && (/^\[object\s+Arguments\]$/).test(expose_internal_class.call(type)); | |
}, | |
isArray = function (type) { | |
return !!type && (/^\[object\s+Array\]$/).test(expose_internal_class.call(type)); | |
}, | |
array_from = ((typeof Array.from == "function") && Array.from) || function (listAlike) { | |
return array_prototype_slice.call(listAlike); | |
}, | |
array_flatten = function flatten (list) { | |
list = (isArguments(list) && array_from(list)) || list; | |
if (isArray(list)) { | |
list = list.reduce(function (collector, elm) { | |
return collector.concat(flatten(elm)); | |
}, []); | |
} | |
return list; | |
} | |
; | |
array_prototype.flatten = function () { | |
return array_flatten(this); | |
}; | |
}(Array, Object)); | |
console.log([ | |
[[[0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2]]], | |
[[[0, 1, 2], [0, 1, 2]], [[0, 1, 2], [0, 1, 2]]] | |
].flatten()); | |
//[0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2] | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composable(/*"composites.Array_shuffle"*/"", function (require, global) { | |
"use strict"; | |
require("components.Enumerable_shuffle").call(global.Array.prototype); | |
//return global.Array; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composable(/*"composites.Array_unique"*/"", function (require, global) { | |
"use strict"; | |
require("components.Enumerable_unique").call(global.Array.prototype); | |
//return global.Array; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composable("components.Enumerable_shuffle", function (require, global) { | |
"use strict"; | |
require("environment_extended_introspective_core"); | |
var | |
environment = require("environment"), | |
environment_introspective = environment.introspective, | |
environment_helpers = environment.helpers, | |
Mixin, | |
Array = global.Array, | |
Math = global.Math, | |
isFunction = environment_introspective.isFunction, | |
isArray = environment_introspective.isArray, | |
array_from = (isFunction(Array.from) && Array.from) || environment_helpers.makeArray, | |
//array_from = environment_helpers.array_from, | |
math_floor = Math.floor, | |
math_random = Math.random, | |
shuffle = function () { | |
var list = this; | |
(!isArray(list) && (list = array_from(list))); | |
if (list) { | |
var | |
idx, | |
item, | |
count = list.length | |
; | |
while (count) { | |
idx = math_floor(math_random() * count); | |
item = list[--count]; | |
list[count] = list[idx]; | |
list[idx] = item; | |
} | |
} | |
return list; | |
} | |
; | |
Mixin = function () { | |
this.shuffle = shuffle; | |
}; | |
return Mixin; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composable("components.Enumerable_unique", function (require, global) { | |
"use strict"; | |
require("environment_extended_introspective_core"); | |
var | |
environment = require("environment"), | |
environment_introspective = environment.introspective, | |
environment_helpers = environment.helpers, | |
Mixin, // the "Enumerable_unique" Mixin Module. | |
isEqualType = environment_introspective.isEqualType, | |
isArray = environment_introspective.isArray, | |
array_from = (isFunction(Array.from) && Array.from) || environment_helpers.makeArray, | |
//array_from = environment_helpers.array_from, | |
//parseFloat = global.parseFloat, | |
//math_floor = global.Math.floor, | |
unique = function () { | |
var | |
list = this, | |
i = -1, | |
k, | |
len, | |
type | |
; | |
!isArray(list) && (list = array_from(list)); | |
len = list.length; | |
while (++i < len) { | |
type = list[i]; | |
k = i; | |
//while (++k < len) { // this is the [deduplicate] or [uniq]e part. | |
while ((i in list) && (++k < len)) { // this is the [deduplicate] or [uniq]e part without [compact]ing or [defragment]ation. | |
//if (list[k] === type) { // strict equality : type and reference based | |
//if (isEqualType(list[k], type)) { // "equals" implementation: structure comparing equality | |
//if ((k in list) && (list[k] === type)) { | |
if ((k in list) && isEqualType(list[k], type)) { | |
list.splice(k, 1); | |
--len; | |
--k; | |
} | |
} | |
// if (!type && ((typeof type == "undefined") || (typeof type == "object"))) { // this is the [compact] or [defragment]ation part - it will be executed at most twice - once for each of the values [null] and [undefined] in case both are present. | |
// list.splice(i, 1); | |
// --len; | |
// --i; | |
// } | |
} | |
return list; | |
} | |
; | |
Mixin = function () { | |
/** | |
* implementing the "Enumerable_unique" Mixin Module. | |
*/ | |
this.unique = unique; | |
}; | |
return Mixin; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composable("composites.Math_average", function (require, global) { | |
"use strict"; | |
require("composites.Array_flatten"); | |
var | |
environment = require("environment_extended_introspective_core"), | |
environment_introspective = environment.introspective, | |
Math = global.Math, | |
parseFloat = global.parseFloat, | |
isFunction = environment_introspective.isFunction, | |
isArray = environment_introspective.isArray, | |
math_average = function (list) { | |
list = (list && isFunction(list.flatten) && list.flatten()) || list; | |
var total; | |
if (isArray(list)) { | |
total = list.reduce(function (sum, summand) { | |
return (sum + parseFloat(summand, 10)) | |
}, 0); | |
} | |
return parseFloat((total / list.length), 10); | |
} | |
; | |
Math.average = math_average; | |
//return Math; | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composable("composites.Math_sum", function (require, global) { | |
"use strict"; | |
require("composites.Array_flatten"); | |
var | |
environment = require("environment_extended_introspective_core"), | |
environment_introspective = environment.introspective, | |
Math = global.Math, | |
parseFloat = global.parseFloat, | |
isFunction = environment_introspective.isFunction, | |
isArray = environment_introspective.isArray, | |
math_sum = function (list) { | |
list = (list && isFunction(list.flatten) && list.flatten()) || list; | |
var total; | |
if (isArray(list)) { | |
total = list.reduce(function (sum, summand) { | |
return (sum + parseFloat(summand, 10)) | |
}, 0); | |
} | |
return parseFloat(total, 10); | |
} | |
; | |
Math.sum = math_sum; | |
//return Math; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment