Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
e4550f5
Adds "args", a function which returns the arguments its called with a…
trxcllnt Mar 10, 2013
46ab042
Adds "distribute", a function which wraps a polyadic function and rep…
trxcllnt Mar 10, 2013
231573d
Adds definition to the _ variable such that the AVM won't cast it to …
trxcllnt Mar 10, 2013
b530ea9
Adds "newInstance_", a version of newInstance that can be partially a…
trxcllnt Mar 10, 2013
5b8f078
adds "guard", a function that guards a function from being invoked wi…
trxcllnt Mar 10, 2013
08f9783
Updates partial to work with the new _ definition. Passing "undefined…
trxcllnt Mar 10, 2013
b97db91
Adds "permutate", a function which returns an Array of all the possib…
trxcllnt Mar 10, 2013
cefdbba
Updates "sequence" to allow a return value of _, indicating the retur…
trxcllnt Mar 10, 2013
4f9c526
Updates "inject" to allow for null seeds, indicating the seed should …
trxcllnt Mar 10, 2013
05968f5
Updates "pluck" to allow for arguments to be passed to the plucked Fu…
trxcllnt Mar 10, 2013
9c5448c
Adds "areEqual", a function which equates two or more values via stri…
trxcllnt Mar 10, 2013
6f1b59d
Adds "ifElse", a function which when invoked, invokes one of two func…
trxcllnt Mar 10, 2013
4f56b67
Adds "tap", a function that accepts a function to run and a return va…
trxcllnt Mar 10, 2013
877caba
Fixes the "permutate" documentation to reflect the values in the exam…
trxcllnt Mar 10, 2013
c73c9dd
Initial commit of callXMLProperty -- expand this method.
trxcllnt Mar 24, 2013
61842fd
Alias of "isA"
trxcllnt Mar 24, 2013
4716605
Adds property chaining to getProperty. Fixes args to merge argument s…
trxcllnt Mar 24, 2013
b693441
Fixes inGroupsOf to not return an empty array if the source array has…
trxcllnt Apr 15, 2013
1a1c489
Fixes min/max to use the field param correctly.
trxcllnt Apr 15, 2013
03a2920
Introduces a new method, "distribute" which accepts list of Functions…
trxcllnt Apr 30, 2013
cff1218
Introduces "pull", a function that calls a getter or function with ar…
trxcllnt Apr 30, 2013
5250c25
Introduces "bindSetProp", a function that binds an instance and a set…
trxcllnt Apr 30, 2013
e1877e2
Updates toDictionary to include a value selector in addition to its k…
trxcllnt May 6, 2013
8d844f8
Introduces memoize, because everybody needs to memoize.
trxcllnt May 6, 2013
b82fe65
Adds more functions to the callXMLProperty function.
trxcllnt May 6, 2013
f5491b5
Changes how memoize checks whether memoized hash results are in the d…
trxcllnt Jun 14, 2013
1e21302
Allows apply to accept a null array without issue.
trxcllnt Jun 21, 2013
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
35 changes: 19 additions & 16 deletions asx/src/asx/array/inGroupsOf.as
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
package asx.array
{
import asx.fn.aritize;
import asx.fn.partial;
import asx.object.newInstance;

/**
*
*
*/
public function inGroupsOf(iterable:Object, size:int):Array
{
var i:int = 0;
var group:Array = [];
var groups:Array = [group];

for each (var item:Object in iterable)
{
group[group.length] = item;
i++;

if (i == size)
{
i = 0;
group = [];
groups[groups.length] = group;
if(size < 1) size = 1;

const l:int = len(iterable);
const groups:Array = map(range(Math.ceil(l / size)), aritize(partial(newInstance, Array), 0));

var group:Array;
var i:int = -1;

for each(var item:Object in iterable) {
if(++i % size == 0) {
group = groups[i / size];
}
}

group[group.length] = item;
}

return groups;
}
}
8 changes: 6 additions & 2 deletions asx/src/asx/array/inject.as
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package asx.array {
import asx.fn._;

/**
* Injects the memo value into an iterator function that is applied to every
Expand All @@ -21,11 +22,14 @@ package asx.array {
* });
* </listing>
*/
public function inject(memo:Object, iterable:Object, iterator:Function):Object {
public function inject(seed:Object, iterable:Object, iterator:Function):Object {
var i:int = 0;
var n:int = iterator.length;
var memo:Object = seed;
for each (var value:Object in iterable) {
memo = iterator.apply(null, [ memo, value, i ].slice(0, Math.max(2, n)));
memo = seed == _ && i == 0 ?
value :
iterator.apply(null, [ memo, value, i ].slice(0, Math.max(2, n)));
i++;
}
return memo;
Expand Down
14 changes: 8 additions & 6 deletions asx/src/asx/array/max.as
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
package asx.array
{
import asx.fn._;

/**
* Find the max item, or item with the max field value.
*/
public function max(iterable:Object, field:String=null):Object
{
var result:Object;
var result:Object = _;
var item:Object;

if (field)
{
for each (item in iterable)
{
if (!result)
if (result === _)
{
result = item;
result = item[field];
}
else if (item && item[field] > result[field])
else if (item && item[field] > result)
{
result = item;
result = item[field];
}
}
}
else
{
for each (item in iterable)
{
if (!result)
if (result === _)
{
result = item;
}
Expand Down
14 changes: 8 additions & 6 deletions asx/src/asx/array/min.as
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
package asx.array
{
import asx.fn._;

/**
* Find the min item, or item with the min field value.
*/
public function min(iterable:Object, field:String=null):Object
{
var result:Object;
var result:Object = _;
var item:Object;

if (field)
{
for each (item in iterable)
{
if (!result)
if (result === _)
{
result = item;
result = item[field];
}
else if (item && item[field] < result[field])
else if (item && item[field] < result)
{
result = item;
result = item[field];
}
}
}
else
{
for each (item in iterable)
{
if (!result)
if (result === _)
{
result = item;
}
Expand Down
65 changes: 65 additions & 0 deletions asx/src/asx/array/permutate.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package asx.array
{
import flash.utils.Dictionary;

import asx.fn._;
import asx.fn.partial;

/**
* Returns a multi-dimensional Array of all combinations of values in the Array,
*
* <p>Example:</p>
* <blockquote>
* trace(permutate([1, 2, 3], 2)); //[ [1, 1], [1, 2], [1, 3], [2, 1], [2, 2], [2, 3], [3, 1], [3, 2], [3, 3] ]
* trace(permutate([1, 2, 3], 3));
* // yields:
* //[
* // [1, 1, 1], [1, 1, 2], [1, 1, 3],
* // [1, 2, 1], [1, 2, 2], [1, 2, 3],
* // [1, 3, 1], [1, 3, 2], [1, 3, 3],
* // [2, 1, 1], [2, 1, 2], [2, 1, 3],
* // [2, 2, 1], [2, 2, 2], [2, 2, 3],
* // [2, 3, 1], [2, 3, 2], [2, 3, 3],
* // [3, 1, 1], [3, 1, 2], [3, 1, 3],
* // [3, 2, 1], [3, 2, 2], [3, 2, 3],
* // [3, 3, 1], [3, 3, 2], [3, 3, 3]
* //]
* </blockquote>
*/
public function permutate(array:Array, combinations:int = 2):Array {

const size:int = length(array);

combinations = (combinations < 2) ? 2 : combinations;

if(size < 2)
return array;

if(size < combinations)
return [array];

// Initialize a cache to store the permutations for each unique value.
// If we encounter that value again, we won't have to iterate, reducing
// the runtime from O(n^combinations) to O((n - duplicates)^combinations)
const cache:Dictionary = new Dictionary();

const permutations:Array = [];
const merge:Function = [].concat;

const recurse:Function = function(array:Array, togo:int, values:Array):Array {
return togo < 1 ?
[values] :
merge.apply(null, map(array, function(x:Object):Array {
return recurse(array, togo - 1, values.concat(x));
}));
};

// Do a merge/map instead of calling recurse so that we can cache
// the results of unique value permutations.
return merge.apply(null, map(array, function(e:*):Array {
return e in cache ?
cache[e] :
cache[e] = recurse(array, combinations - 1, [e]);
}));
}
}
15 changes: 11 additions & 4 deletions asx/src/asx/array/pluck.as
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package asx.array
{
import asx.fn._;
import asx.fn.partial;

/**
* Extracts the value of a field from every item in an Array.
Expand All @@ -14,21 +16,26 @@ package asx.array
* assertThat(plucked, equalTo(['waffles', 'crumpets', 'toast']));
* </listing>
*/
public function pluck(iterable:Object, field:Object):Array
public function pluck(iterable:Object, field:Object, ...args):Array
{
var chain:Array = field is Array ? field as Array : String(field).split('.');
return inject(iterable, chain, $pluckIt) as Array;
return inject(iterable, chain, partial($pluckIt, _, _, args)) as Array;
}
}

import asx.array.map;
import asx.fn.callFunction;
import asx.fn.callProperty;
import asx.fn.getProperty;

internal function $pluckIt(iterable:Object, field:String):Array
internal function $pluckIt(iterable:Object, field:String, args:Array = null):Array
{
var isMethod:Boolean = !!field.match(/^.+\(\)$/);
field = isMethod ? field.slice(0, -2) : field;
return map(iterable, isMethod ? callProperty(field) : getProperty(field));
return map(iterable,
isMethod ?
callProperty.apply(null, [field].concat(args)) :
getProperty(field)
);
}

7 changes: 5 additions & 2 deletions asx/src/asx/array/toDictionary.as
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package asx.array
{
import asx.fn.I;
import asx.fn.getProperty;

import flash.utils.Dictionary;
Expand All @@ -20,7 +21,7 @@ package asx.array
* trace(dict[3].value); // 5
* </listing>
*/
public function toDictionary(iterable:Object, key:Object):Dictionary
public function toDictionary(iterable:Object, key:Object, value:Object = null):Dictionary
{
var dict:Dictionary = new Dictionary();

Expand All @@ -29,8 +30,10 @@ package asx.array
? key as Function
: getProperty(String(key));

var valFn:Function = value is Function ? value as Function : I;

for each (var item:Object in iterable)
dict[ keyFn(item) ] = item;
dict[ keyFn(item) ] = valFn(item);

return dict;
}
Expand Down
4 changes: 3 additions & 1 deletion asx/src/asx/fn/_.as
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ package asx.fn {
*
* @see asx.fn.partial
*/
public var _:* = undefined;
// don't use undefined because the AVM automatically casts undefined to
// false-y values and we lose the uniqueness of the constant;
public var _:Object = {};
}
11 changes: 11 additions & 0 deletions asx/src/asx/fn/apply.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package asx.fn
{
/**
* @author ptaylor
*/
public function apply(fn:Function):Function {
return function(array:Array):* {
return fn.apply(null, array || []);
}
}
}
20 changes: 20 additions & 0 deletions asx/src/asx/fn/areEqual.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package asx.fn
{
import asx.array.allOf;
import asx.array.head;
import asx.array.last;
import asx.array.permutate;

/**
* @author ptaylor
*/
public function areEqual(...items):Boolean {

if(items.length < 2) return head(items);
if(items.length == 2) return head(items) === last(items);

const permutations:Array = permutate(items, 2);

return allOf(permutations, apply(areEqual));
}
}
11 changes: 11 additions & 0 deletions asx/src/asx/fn/args.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package asx.fn
{
/**
* @author ptaylor
*/
public function args(...args):Array {
return merge.apply(null, args);
}
}

internal const merge:Function = [].concat;
11 changes: 11 additions & 0 deletions asx/src/asx/fn/bindSetProp.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package asx.fn
{
/**
* @author ptaylor
*/
public function bindSetProp(instance:Object, field:String):Function {
return function(val:*):void {
instance[field] = val;
};
}
}
22 changes: 22 additions & 0 deletions asx/src/asx/fn/callXMLProperty.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package asx.fn
{
/**
* @author ptaylor
*/
public function callXMLProperty(method:String, ...args):* {
return function(node:XML):* {
switch(method) {
case 'children':
return node.children();
case 'childIndex':
return node.childIndex();
case 'elements':
return node.elements();
case 'localName':
return node.localName();
case 'toString':
return node.toString();
}
};
}
}
13 changes: 13 additions & 0 deletions asx/src/asx/fn/distribute.as
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package asx.fn
{
import asx.array.map;

/**
* @author ptaylor
*/
public function distribute(...fns):Function {
return function(...args):Array {
return map(fns, callFunction.apply(null, args));
}
}
}
Loading