one-go
Deal with a set of objects in one go!
Goal
The module is implemented as a way of prototyping faster by abstracting away looping over collections. But this is done at the expence of execution speed, as objects are looped over again and again.
OneGo is a factory that given a class Class returns a function List whose prototype shares the same API as Class and does the same object by object in the collection.
Usage
Better to understand with an example:
; {thisname = name;} {return thisname;} {thisname = name;} {thisname = firstName + lastName;} {console;} const List = ;const list = 'Aaa' 'Bbb' 'Ccc'; list; // Returns ['Aaa', 'Bbb', 'Ccc']list; // Prints 'Aaa\nBbb\nCcc\n'list; // Sets every name to 'Ddd'list; // Sets names respectively to 'Eee', 'Fff', 'Ggg'list; // Sets names respectively to 'EeeEe', 'FffFf', 'GggGg'
const List = OneGo(Class)
Creating a List constructor: List and Class have the same API. Each method of List wraps a loop over the same method in Class. Their results (if any) are returned as an array of all results.
const list = new List(...args)
Instantiating a list: Arguments passed to the constructor can be as many as necessary. Their initial number determines once and for all the size of each generated loops.
If the original class constructor takes a single argument, then []
can be omitted around it when passed to the List constructor. But if the original class takes more arguments, then []
are necessary to distinguish which object should be initialized with what.
Some valid constructions:
const list1 = 'Aaa' 'Bbb' 'Ccc';const list2 = 'Aaa' 'Bbb' 'Ccc';const list3 = 'Aaa' 1 id: 'id1' 'Bbb' 21 id: 'id2' 'Ccc' 12 id: 'id3';
list.methodName(...args)
Simple looping: By default, a call to a method with some arguments passes those arguments to each and every wrapped object associated method.
If you have a method adding an argument to a property, then that argument will be added to each and every same property of the underlying objects.
If you desire a more vector like behavior, for a one-to-one addition for example, you need a special construct with ogArgs
function as a helper. See below.
list.methodName(ogArgs(...args))
Advanced looping: When you want to pass distinct arguments to every underlying object methods, you need to use the wrapping function ogArgs
. It will take care of distributing your arguments to their respective targets.
; {thisvalue = number;} {thisvalue += number;} {return thisvalue;} const List = ;const list = 1 34 MathPI 999; list
Options
Option 'arrayInit'
Sometimes it's just easier to pass an array of arguments rather than arguments directly. By setting this option to true
, the generated function List will expect an array instead of a sequence of arguments.
; {thisvalue = number;} {thisvalue += number;} {return thisvalue;} const List1 = ; // option 'arrayInit' not setconst list1 = 1 34 MathPI 999; // sequence of arguments const List2 = ; // option 'arrayInit' setconst list2 = 1 34 MathPI 999; // array of arguments
Option 'override'
By default, the generated List shares its API with the underlying Type, simply looping over the list elements and applying the method of the same name. If you want a different behavior for a specific method, override it using the 'override' option.
; {thisvalue = number;} {thisvalue += number;} {return thisvalue;} const List = ;const list = 1 34 999;list; // Returns 134.9... and not [1, 34, 99.9]
Option 'parallel'
You may not want to override a generated List method, but the underlying Type original method. Use the 'parallel' option for that.
; {thisvalue = number;} {thisvalue += number;} {return thisvalue;} const List = ;const list= 1 34 999;list; // Returns ['value: 1', 'value: 34', 'value: 99.9']// and not [1, 34, 99.9] ## License one-go is MIT licensed/LICENSE © 2016 Jason Lenoblemailto:jasonlenoble@gmailcom