title: How to write reduce function with memo of arbitrary type
body: <p>I am trying to write a simple histogram program that takes a stream of unknown (very large) length, integers only, and bins them up. My problem doesn't relate to binning so we can assume that the integers can be in range (0, 1000] and the histogram bins are of size 1 (each possible number gets its own bin).</p>
  <p>Normally I would do this with <code>reduce</code> and its variants in whatever programming language, and I write something like that in Julia (using an array in place of the actual file-based iterator i am using as input):</p>
  <pre><code>function ct_a(arr, i)
         arr[i] += 1
  end
  
  reduce(ct_a, [100, 140, 471, 900, 999], init=zeros(1000))
  </code></pre>
  <p>Now i've tried variants such as changing the order of arguments and so on but get a variety of seeming type or signature related issues:</p>
  <pre><code>ERROR: MethodError: no method matching reduce(::typeof(ct_b), ::Array{Int64,1}, ::Array{Int64,1})
  ERROR: MethodError: no method matching reduce(::typeof(ct_a), ::Array{Int64,1}, ::Array{Int64,1})
  ERROR: MethodError: no method matching setindex!(::Int64, ::Int64, ::Int64)
  </code></pre>
  <p>This seems like it should work because AFAIK there is no formal reason why the output of a <code>reduce</code> operation needs to resemble the type of the individual elements being iterated over. Every example I can find online however about the use of <code>reduce</code> in julia supposes that the memo (output) is of like type (i.e., the <code>op</code> operator being passed in is of type <code>F: T,T -&gt; T</code>.) This seems like an arbitrary restriction and unlikely to be the case, but i can't figure out how to do this differently.</p>
  <p>I have been able to circumvent this issue somehow by using a function like <code>F: T,T-&gt;null</code> and storing the memo globally:</p>
  <pre><code>memo=zeros(1000)
  
  function ct_x(_, i)
          memo[i] += 1
  end
  
  reduce(ct_x, [1,2,3,100,200,900], init=&quot;whatever&quot;)
  </code></pre>
  <p>Is there an intended way to express reduce functions of this nature?</p>