title: Iteration with purrr::reduce()
body: <p>Given a function <code>f(a, x) = a*x</code> with an initial <code>a = 3</code>, suppose to have an iteration where <code>a</code> is assigned with <code>f(a, x)</code> in the next step. </p>
  
  <ul>
  <li>For <code>x = 2</code>, <code>a</code> is assigned with <code>f(3, x = 2) = 6</code>, then;  </li>
  <li>For <code>x = 3</code>, <code>a</code> is assigned with <code>f(6, x = 3) = 18</code>, then;  </li>
  <li>For <code>x = 4</code>, <code>a</code> is assigned with <code>f(18, x = 4) = 72</code>;</li>
  </ul>
  
  <p>How to use <code>purrr</code> to realize the iteration? The following is not quite right.</p>
  
  <pre class="lang-r prettyprint-override"><code>library(purrr)
  a &lt;- 3
  f &lt;- function(a, x) a*x
  
  2:4 %&gt;% reduce(~f(a, .))
  #&gt; [1] 18
  
  2:4 %&gt;% accumulate(~f(a, .))
  #&gt; [1]  2  6 18
  </code></pre>
  
  <p><sup>Created on 2020-04-24 by the <a href="https://reprex.tidyverse.org" rel="nofollow noreferrer">reprex package</a> (v0.3.0)</sup></p>