diff --git a/tutorial/tutorial.wmml b/tutorial/tutorial.wmml index 74548b6230edd920c5aa76091be7a8e371f3469e..6534cd6f8cd593f45c1cf7fd853a333cd5edc111 100644 --- a/tutorial/tutorial.wmml +++ b/tutorial/tutorial.wmml @@ -5960,8 +5960,8 @@ count("....","..") is 2. <section title="Array"> <module name="Array" title="supplemental Array handling functions"> -As with <tt>String</tt> these functions are Pike functions written to -supplement those written in C. +As with <tt>String</tt> these functions are mostly Pike functions +written to supplement those written in C. @@ -6229,7 +6229,7 @@ the size of the given array). </man_description> <man_example> -Array.permute( ({ 1,2,3 }), 0) -> ({1,2,3}) +Array.permute( ({ 1,2,3 }), 0) -> ({1,2,3}) <br> Array.permute( ({ 1,2,3 }), 3) -> ({1,3,2}) </man_example> <man_see> @@ -6237,6 +6237,54 @@ Array.shuffle </man_see> </function> +<function name=Array.reduce title="Iterativily applicate a function on an array"> +<man_syntax> +mixed reduce(function <i>f</i>, array <i>arr</i>, void|mixed <i>zero</i>); +</man_syntax> +<man_description> +<tt>reduce</tt> sends the first two elements in <i>arr</i> to <i>f</i>, +then the result and the next element in <i>arr</i> to <i>f</i> and +so on. Then it returns the result. The function will return +<i>zero</i> if <i>arr</i> is the empty array. If <i>arr</i> has +size 1 it will return the element in <i>arr</i>. + +</man_description> +<man_example> +Array.reduce(aggregate, indices(allocate(4))) -><br> + ({ ({ ({ 0, 1 }), 2 }), 3 })<br> +Array.reduce(`+, ({}), "FOO?") -> <br> + "FOO?"<br> +Array.reduce(lambda(int a, int b) {<br> + while(b) { int t=b; b=a%b; a=t; }<br> + return a;<br> + }, ({7191,21573,64719,33694}))<br> + -> 17 +</man_example> +<man_see> +Array.rreduce +</man_see> +</function> + +<function name=Array.rreduce title="Iterativily applicate a function on an array backwards"> +<man_syntax> +mixed rreduce(function <i>f</i>, array <i>arr</i>, void|mixed <i>zero</i>); +</man_syntax> +<man_description> +<tt>reduce</tt> sends the last two elements in <i>arr</i> to <i>f</i>, +then the third last element in <i>arr</i> and the result to <i>f</i> and +so on. Then it returns the result. The function will return +<i>zero</i> if <i>arr</i> is the empty array. If <i>arr</i> has +size 1 it will return the element in <i>arr</i>. + +</man_description> +<man_example> +Array.rreduce(aggregate, indices(allocate(4))) -><br> + ({ 0, ({ 1, ({ 2, 3 }) }) }) +</man_example> +<man_see> +Array.reduce +</man_see> +</function> <function name=Array.search_array title="search for something in an array">