title: Fibonacci with memoization elixir
body: <p>I'm studying about functional programming and I made a simple fibonacci in elixir.</p>
  
  <p>I know that isn't possible change values in functional programming and I made a code to make fibonacci with memoization, but the code is bad.
  How can I improve this code?</p>
  
  <pre><code>defmodule Fib do
    def fib_memoized(0, memo) do
      {0, memo}
    end
  
    def fib_memoized(1, memo) do
      {1, memo}
    end
  
    def fib_memoized(n, memo \\ %{}) do
      if Map.has_key?(memo, n) do
        { memo[n], memo }
      else
        {n1, memo1} = fib_memoized(n-1, memo)
        {n2, memo2} = fib_memoized(n-2, memo1)
  
        value = n1+n2
  
        {value, Map.merge(memo2, %{n =&gt; value})}
      end
    end
  
    def fib(n) do
      { value, _ } = fib_memoized(n)
      value 
    end
  end
  
  IO.puts Fib.fib(1000)
  </code></pre>
  
  <p>In javascript is possible to make a High Order Function and save the "map" and update this one.</p>
  
  <p>Ex:</p>
  
  <pre><code>function memoization(fn) {
      let memo = {}
      return function (n) {
        if(!memo[n]) {
          memo[n] = fn(n)
        }
        return memo[n]
      }
    }
  </code></pre>
  
  <p>Is possible to make something like it?</p>