<?xml version="1.0" encoding="utf-8"?><!DOCTYPE article  PUBLIC '-//OASIS//DTD DocBook XML V4.4//EN'  'http://www.docbook.org/xml/4.4/docbookx.dtd'><article><articleinfo><title>CreateFunction</title><revhistory><revision><revnumber>2</revnumber><date>2015-09-08 12:12:11</date><authorinitials>LKD70</authorinitials></revision><revision><revnumber>1</revnumber><date>2015-09-08 12:10:23</date><authorinitials>LKD70</authorinitials></revision></revhistory></articleinfo><para>The following functions can be used to manipulate and retrieve details from objects and arrays. </para><para>These should be written in the following formats: </para><itemizedlist><listitem override="none"><para><emphasis>array.function(CreateFunction(&quot;input&quot;,&quot;handling&quot;))</emphasis>, example - <emphasis>myArray.forEach(CreateFunction(&quot;m,i,a&quot;,&quot;m='lkd' &amp;&amp; array2.push(m)&quot;))</emphasis> if you had an array and wished to push all elements matching 'lkd' to another array called &quot;array2&quot; this would allow you to do so. Confused? Same, We'll do some examples soon. </para></listitem></itemizedlist><para>Click each function name in the table below for more details of it, with examples. </para><section><title>Functions</title><informaltable><tgroup cols="5"><colspec colname="col_0"/><colspec colname="col_1"/><colspec colname="col_2"/><colspec colname="col_3"/><colspec colname="col_4"/><tbody><row rowsep="1"><entry colsep="1" rowsep="1"><para><emphasis role="strong">Function </emphasis> </para></entry><entry colsep="1" rowsep="1"><para><emphasis role="strong">Description </emphasis> </para></entry><entry align="center" colsep="1" nameend="col_3" namest="col_2" rowsep="1"/><entry colsep="1" rowsep="1"/></row><row rowsep="1"><entry colsep="1" rowsep="1"><para><link linkend="some">.some()</link> </para></entry><entry colsep="1" rowsep="1"><para>Returns true if &quot;some&quot; of the values are true, else returns false. </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para><link linkend="filter">.filter()</link> </para></entry><entry colsep="1" rowsep="1"><para>Returns the array with values filtered out that match the filter input. </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para><link linkend="forEach">.forEach()</link> </para></entry><entry colsep="1" rowsep="1"><para>Returns a value for each separate element in an array after executing the provided function once per array element. </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para><link linkend="map">.map()</link> </para></entry><entry colsep="1" rowsep="1"><para>Returns a new array with the results of calling a provided function on every element within this array. </para></entry></row><row rowsep="1"><entry colsep="1" rowsep="1"><para><link linkend="every">.every()</link> </para></entry><entry colsep="1" rowsep="1"><para>Returns true if all elements in the array passed the input parameters (all where true). </para></entry></row></tbody></tgroup></informaltable></section><section><title>CreateFunction</title><para>Understanding CreateFunction() might seem hard at first, but once you play with some examples it can be easy to understand. CreateFunction allows the user to define and create there own functions which can be used on the Functions seen above. The CreateFunction() requires an arguments string, and a formula string. These are formatted like so: CreateFunction(&quot;Arguments&quot;,&quot;Formula&quot;) The arguments is what to pass into the function, when using functions like forEach and map, you may be required to pass in an &quot;array&quot; and an &quot;index&quot; argument, this is for the internal processing used in the forEach and map functions. All the functions do however require one argument. This argument is referred too as the &quot;currentValue&quot;. This is because this indicates the current value we're handling from the array the function is being used on. Imagine we have an array of 4 numbers: a=[1,2,3,4] to use the functions above on this array it will need to process each number (1,2,3 and 4). While doing so, this number is referred too as the currentValue. The example below shows how the currentValue is set. </para><itemizedlist><listitem override="none"><para>MyFunction = CreateFunction(&quot;currentValue,Index,Array&quot;,&quot;Arr2.push(currentValue)&quot;) </para></listitem></itemizedlist><para>As you can see in this example, the currentValue is an argument, it is also used in the formula to complete a process. Expanding on this function, we could use forEach on an array Arr=[1,2,3,4] to add the elements the existing array to a new array called Arr2. </para><itemizedlist><listitem override="none"><para>Arr = [1,2,3,4] //Existing array. </para></listitem><listitem override="none"><para>Arr2 = []       //New empty array. </para></listitem><listitem override="none"><para>MyFunction = CreateFunction(&quot;currentValue,Index,Array&quot;,&quot;Arr2.push(currentValue)&quot;) //Function to use in .forEach() function. </para></listitem><listitem override="none"><para>Arr.forEach(MyFunction)  //using the MyFunction CreateFunction on the array 'Arr'. </para></listitem><listitem override="none"><para>echo &quot;the old array is {Arr}, the new array after processing is: {Arr2}&quot; //Echoing the output. </para></listitem></itemizedlist><para>The output of this would be: </para><itemizedlist><listitem override="none"><para>the old array is 1,2,3,4, the new array after processing is: 1,2,3,4 </para></listitem></itemizedlist><para>In the formula section of this CreateFunction we've put: &quot;Arr2.push(currentValue)&quot;. This is a simple command telling it to <ulink url="http://guide.neatportal.com/wiki/CreateFunction/wiki/Arrays#push">push</ulink> the currentValue to the array Arr2. This now means we have two totally identical arrays that are totally separate from one another. </para></section><section><title>some</title><para><emphasis role="strong">Usage:</emphasis> .some(CreateFunction(&quot;input&quot;,&quot;handling&quot;)) </para><para>the .some() function requires at least 1 input. This input is how you'll reference the current value (elements of the array you're running this function against). .some() is a boolean check to see if at least one or more elements in an array return true. If all are false this will return false, but if at least one is true, the result will be true. </para><para><emphasis role="strong">Example:</emphasis> </para><itemizedlist><listitem override="none"><para>lessthan10 = CreateFunction(&quot;x,ind,arr&quot;, &quot;x &lt; 10&quot;) //Create a function to check if 'x' (current value) is '&lt;' (less than) 10. 'ind,arr' allows us to create an index and an array. this is just for internal function handling. </para></listitem><listitem override="none"><para>arr = [ 20, 5, 10, 11, 7, 3.4, 21, 1, 7, 9 ] //Create an array of numbers. </para></listitem><listitem override="none"><para>if arr.some(lessthan10) echo &quot;At least one value is less than 10&quot; //If at least one of the &quot;arr&quot; elements is 'lessthan10', echo &quot;At least one value is less than 10&quot;. </para></listitem></itemizedlist><para><emphasis role="strong">Result:</emphasis> </para><itemizedlist><listitem override="none"><para>At least one value is less than 10 </para></listitem></itemizedlist></section><section><title>filter</title><para><emphasis role="strong">Usage:</emphasis> .filter(CreateFunction(&quot;input&quot;,&quot;handling&quot;)) </para><para>the .filter() function requires at least 1 input. This input is how you'll reference the current value (elements of the array you're running this function against). .filter() returns an array with the values from the input array. But only if these values passed the filter function entered. If one does not pass, that one will not exist in the new array. </para><para><emphasis role="strong">Example:</emphasis> </para><itemizedlist><listitem override="none"><para>lessthan10 = CreateFunction(&quot;x,ind,arr&quot;, &quot;x &lt; 10&quot;) //Create a function to check if 'x' (current value) is '&lt;' (less than) 10. 'ind,arr' allows us to create an index and an array. this is just for internal function handling. </para></listitem><listitem override="none"><para>arr = [ 20, 5, 10, 11, 7, 3.4, 21, 1, 7, 9 ] //Create an array of numbers. </para></listitem><listitem override="none"><para>result = arr.filter(lessthan10) </para></listitem><listitem override="none"><para>echo result </para></listitem></itemizedlist><para><emphasis role="strong">Result:</emphasis> </para><itemizedlist><listitem override="none"><para>5, 7, 3.4, 1, 7, 9 </para></listitem></itemizedlist></section><section><title>forEach</title><para><emphasis role="strong">Usage:</emphasis> .forEach(CreateFunction(&quot;input,Index,Array&quot;,&quot;handling&quot;)) </para><para>the .forEach() function requires at least 3 inputs. This consists of an Array, and Index and the input current value. The index and array are needed for the function handling.  .forEach() returns an array with the values from the input array. But only if these values passed the filter function entered. If one does not pass, that one will not exist in the new array. .forEach Returns a value for each separate element in the array that you've passed in after executing the provided function (CreateFunction) once per array element. </para><para><emphasis role="strong">Example:</emphasis> </para><itemizedlist><listitem override="none"><para>a = [ ] //Creates an empty array that we can use to push values to. </para></listitem><listitem override="none"><para>pushname = CreateFunction(&quot;obj,ind,arr&quot;, &quot;a.push(obj.name)&quot;) //Creates a function that will push the obj.name to the &quot;a&quot; array. </para></listitem><listitem override="none"><para>city.heroes.toArray().forEach(pushname) //.toArray is used to convert the city.heroes object to an array. Each value of this array is the passed though the 'pushname' function. </para></listitem><listitem override="none"><para>echo &quot;Heroes are: &quot; + a //Echo the &quot;a&quot; array. </para></listitem></itemizedlist><para><emphasis role="strong">Result:</emphasis> </para><itemizedlist><listitem override="none"><para>Heroes are: LKD70,BROX,LKD,SPOCK,SPAM000,SPAM001,SPAM002,SPAM003,SPAM004,SPAM005 </para></listitem></itemizedlist></section><section><title>map</title><para><emphasis role="strong">Usage:</emphasis> .map(CreateFunction(&quot;input,Index,Array&quot;,&quot;handling&quot;)) </para><para>the .map() function requires at least 3 inputs. This consists of an Array, and Index and the input current value. The index and array are needed for the function handling.  The .map() function will create a new array consisting of the results of running the CreateFunction on each element within the previous array. Every value in the previous array will run the CreateFunction. There output is then appended to the new array at the same index it was at in the original array. </para><para><emphasis role="strong">Example:</emphasis> </para><itemizedlist><listitem override="none"><para>addTotal = CreateFunction(&quot;x,ind,arr&quot;, &quot;total+=x&quot;) //This function allows us to add all inputted values to the &quot;total&quot; variable (when using it with .forEach() ). </para></listitem><listitem override="none"><para>getArchers = CreateFunction(&quot;city,ind,arr&quot;, &quot;city.cityManager.troop.archer&quot;) //This grabs the archer count from a city. </para></listitem><listitem override="none"/><listitem override="none"><para>total = 0 </para></listitem><listitem override="none"><para>cities.map(getArchers).forEach(addTotal) //This runs the &quot;getArchers&quot; function in all &quot;cities&quot;. This means the output will consist of the archer count for all cities in the account. It then uses forEach on this new array to run the &quot;addTotal&quot; function on each archer count. This will add each of the archer counts to the &quot;total&quot; variable. </para></listitem><listitem override="none"/><listitem override="none"><para>echo &quot;Total (idle) archers in all cities is: {total}.&quot; //this simply echo's the 'total' variable. </para></listitem></itemizedlist><para><emphasis role="strong">Result:</emphasis> </para><itemizedlist><listitem override="none"><para>Total (idle) archers in all cities is: 320963530. </para></listitem></itemizedlist></section><section><title>every</title><para><emphasis role="strong">Usage:</emphasis> .map(CreateFunction(&quot;input,Index,Array&quot;,&quot;handling&quot;)) </para><para>the .every() function requires at least 1 input. This is the current value within the array.  the .every() function will return true or false depending on the output of every array element after processing through the function. If just one of the elements returns false, this will return false, but if all returned true, then this will be true. </para><para><emphasis role="strong">Example:</emphasis> </para><itemizedlist><listitem override="none"><para>limit = GetResources(&quot;f:990b,w:100m,i:10m:10m,s:10m,g:100m&quot;) //Sets the variable &quot;limit&quot; to a resources object. </para></listitem><listitem override="none"><para>hasResource = CreateFunction(&quot;city,index,arr&quot;, &quot;city.cityManager.hasResource(limit)&quot;) //hasResource function to check if the city hasResource() &quot;limit&quot;. </para></listitem><listitem override="none"><para>if !cities.every(hasResource) echo &quot;We are low on resource in at least one city!&quot; //Perform &quot;every&quot; on the cities array to check if &quot;every&quot; city returns true on hasResources (This would mean all cities have the specified resources.) if it does not it will echo &quot;We are low on resource in at least one city!&quot;. </para></listitem></itemizedlist><para><emphasis role="strong">Result:</emphasis> </para><itemizedlist><listitem override="none"><para>We are low on resource in at least one city! </para></listitem></itemizedlist><!--rule (<hr>) is not applicable to DocBook--><para> <ulink url="http://guide.neatportal.com/wiki/CreateFunction/wiki/CategoryFunctions#">CategoryFunctions</ulink> </para></section></article>