<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->

<refentry xml:id="ds-vector.reduce" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
 <refnamediv>
  <refname>Ds\Vector::reduce</refname>
  <refpurpose>Reduces the vector to a single value using a callback function</refpurpose>
 </refnamediv>

 <refsect1 role="description">
  &reftitle.description;
  <methodsynopsis>
   <modifier>public</modifier> <type>mixed</type><methodname>Ds\Vector::reduce</methodname>
   <methodparam><type>callable</type><parameter>callback</parameter></methodparam>
   <methodparam choice="opt"><type>mixed</type><parameter>initial</parameter></methodparam>
  </methodsynopsis>
  <para>
    Reduces the vector to a single value using a callback function.
  </para>

 </refsect1>

 <refsect1 role="parameters">
  &reftitle.parameters;
  <variablelist>
   <varlistentry>
    <term><parameter>callback</parameter></term>

    <listitem>
      <methodsynopsis>
       <type>mixed</type><methodname><replaceable>callback</replaceable></methodname>
       <methodparam><type>mixed</type><parameter>carry</parameter></methodparam>
       <methodparam><type>mixed</type><parameter>value</parameter></methodparam>
      </methodsynopsis>
      <variablelist>
       <varlistentry>
        <term><parameter>carry</parameter></term>
        <listitem>
         <para>
          The return value of the previous callback, or <parameter>initial</parameter> if
          it's the first iteration.
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term><parameter>value</parameter></term>
        <listitem>
         <para>
          The value of the current iteration.
         </para>
        </listitem>
       </varlistentry>
      </variablelist>
     </listitem>

   </varlistentry>
   <varlistentry>
    <term><parameter>initial</parameter></term>
    <listitem>
     <para>
        The initial value of the carry value. Can be &null;.
     </para>
    </listitem>
   </varlistentry>
  </variablelist>
 </refsect1>

 <refsect1 role="returnvalues">
  &reftitle.returnvalues;
  <para>
    The return value of the final callback.
  </para>
 </refsect1>

 <refsect1 role="examples">
  &reftitle.examples;
  <example>
   <title><function>Ds\Vector::reduce</function> with initial value example</title>
   <programlisting role="php">
<![CDATA[
<?php
$vector = new \Ds\Vector([1, 2, 3]);

$callback = function($carry, $value) {
    return $carry * $value;
};

var_dump($vector->reduce($callback, 5));

// Iterations:
//
// $carry = $initial = 5
//
// $carry = $carry * 1 =  5
// $carry = $carry * 2 = 10
// $carry = $carry * 3 = 30
?>
]]>
   </programlisting>
   &example.outputs.similar;
   <screen>
<![CDATA[
int(30)
]]>
   </screen>
  </example>
  <example>
   <title><function>Ds\Vector::reduce</function> without an initial value example</title>
   <programlisting role="php">
<![CDATA[
<?php
$vector = new \Ds\Vector([1, 2, 3]);

var_dump($vector->reduce(function($carry, $value) {
    return $carry + $value + 5;
}));

// Iterations:
//
// $carry = $initial = null
//
// $carry = $carry + 1 + 5 =  6
// $carry = $carry + 2 + 5 = 13
// $carry = $carry + 3 + 5 = 21
?>
]]>
   </programlisting>
   &example.outputs.similar;
   <screen>
<![CDATA[
int(21)
]]>
   </screen>
  </example>
 </refsect1>


</refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->
