<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="170680" data-post-id="170680">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="NobbZ" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/NobbZ/120/27235_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  NobbZ
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Macro/Metaprogramming approaches are possible, though my approach usually just takes an ordered list of conversions and iterates over them.</p>
<details>
<summary>My take inside, it might spoil you!`</summary>
<pre data-code-wrap="ex"><code class="lang-ex">defmodule Roman do
  @arab_to_roman [
    {1000, "M"}, {900, "CM"}, {500, "D"}, {400, "CD"}, {100, "C"}, {90, "XC"},
    {50, "L"}, {40, "XL"}, {10, "X"}, {9, "IX"}, {5, "V"}, {4, "IV"}, {1, "I"}
  ]

  @doc """
  Convert the number to a roman number.
  """
  @spec numerals(1..3999) :: String.t
  def numerals(number) do
    Enum.reduce(@arab_to_roman, {"", number}, fn ({ar, rm}, {acc, n}) -&gt;
      {acc &lt;&gt; String.duplicate(rm, div(n, ar)), rem(n, ar)}
    end) |&gt; elem(0)
  end
end
</code></pre>
<p>PS: This was created when <code>mix format</code> did not yet exist.</p>
</details> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="170680" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/what-do-you-think-of-my-solution-for-converting-integers-to-roman-numerals/30554/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-170680" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="170680"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="171145" data-post-id="171145">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="saverio-kantox" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/saverio-kantox/120/15972_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  saverio-kantox
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Last idea y <a class="mention" href="/u/nobbz" rel="nofollow">@NobbZ</a> is cool, iteration is 100x better than recursion when you have something to iterate on.</p>
<p>My take is still using recursion, but with only one way of handling all cases, because the additional clauses are only adding noise. By checking case from big to small, you have each clause in the same shape.</p>
<details>
<summary>
Click to expand and see my take</summary>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule Roman do
  def numeral(n) when n &lt; 0 or n &gt; 3_999, do: {:error, {:out_of_bounds, n}}
  def numeral(n), do: {:ok, do_numeral(n)}

  defp do_numeral(n) when n &gt;= 1000, do: "M" &lt;&gt; do_numeral(n - 1000)
  defp do_numeral(n) when n &gt;= 900, do: "DM" &lt;&gt; do_numeral(n - 900)
  defp do_numeral(n) when n &gt;= 500, do: "D" &lt;&gt; do_numeral(n - 500)
  defp do_numeral(n) when n &gt;= 400, do: "CD" &lt;&gt; do_numeral(n - 400)
  defp do_numeral(n) when n &gt;= 100, do: "C" &lt;&gt; do_numeral(n - 100)
  defp do_numeral(n) when n &gt;= 90, do: "XC" &lt;&gt; do_numeral(n - 90)
  defp do_numeral(n) when n &gt;= 50, do: "L" &lt;&gt; do_numeral(n - 50)
  defp do_numeral(n) when n &gt;= 40, do: "XL" &lt;&gt; do_numeral(n - 40)
  defp do_numeral(n) when n &gt;= 10, do: "X" &lt;&gt; do_numeral(n - 10)
  defp do_numeral(n) when n &gt;= 9, do: "IX" &lt;&gt; do_numeral(n - 9)
  defp do_numeral(n) when n &gt;= 5, do: "V" &lt;&gt; do_numeral(n - 5)
  defp do_numeral(n) when n &gt;= 4, do: "IV" &lt;&gt; do_numeral(n - 4)
  defp do_numeral(n) when n &gt;= 1, do: "I" &lt;&gt; do_numeral(n - 1)
  defp do_numeral(0), do: ""
end
</code></pre>
</details> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="171145" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/what-do-you-think-of-my-solution-for-converting-integers-to-roman-numerals/30554/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-171145" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="171145"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="171156" data-post-id="171156">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="kip" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/kip/120/1440_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  kip
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>ex_cldr Core Team</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p><a class="mention" href="/u/nobbz" rel="nofollow">@nobbz</a>’s solution is also using <a href="https://en.wikipedia.org/wiki/Fold_(higher-order_function)" rel="nofollow">recursion</a>. Everything is recursion, <code>Enum.reduce/3</code> just makes it look a little like iteration.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="171156" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/what-do-you-think-of-my-solution-for-converting-integers-to-roman-numerals/30554/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-171156" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="171156"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #13"></div>
  </section>
</div>
    <div class="postbit" id="172197" data-post-id="172197">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="mudasobwa" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/mudasobwa/120/5298_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  mudasobwa
                  </h3>
		          </div>
						
			          <div class="user-title">
									<span>Creator of Cure</span>
			          </div>
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>FWIW, here is a generic solution that takes a <code>charlist</code> as a parameter (might be longer than <code>'IVXLCDM'</code>, it only needs to follow the Roman numerals convention) and converts numbers <em>without intermediate checks</em> for “non-round” numerals.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule RomanNumerals do
  require Integer
  @romans 'IVXLCDM'

  @spec numeral(pos_integer()) :: String.t()
  def numeral(number, romans \\ @romans) do
    fours = fn
      &lt;&lt;c, c, c, c&gt;&gt;, [last], _f -&gt; &lt;&lt;c, last&gt;&gt;
      &lt;&lt;c, c, c, c&gt;&gt;, [c, next | _], _f -&gt; &lt;&lt;c, next&gt;&gt;
      &lt;&lt;next, c, c, c, c&gt;&gt;, [c, next, result | _], _f -&gt; &lt;&lt;c, result&gt;&gt;
      &lt;&lt;some, c, c, c, c&gt;&gt;, [c, next | _], _f -&gt; &lt;&lt;some, c, next&gt;&gt;
      input, [_ | next], f -&gt; f.(input, next, f)
    end

    romans
    |&gt; Enum.with_index()
    |&gt; Enum.reverse()
    |&gt; Enum.reduce({number, []}, fn {c, i}, {number, acc} -&gt;
      denominator =
        round(
          if Integer.is_even(i),
            do: :math.pow(10, i / 2),
            else: 5 * :math.pow(10, (i - 1) / 2)
        )

      {
        rem(number, denominator),
        acc ++ List.duplicate(c, div(number, denominator))
      }
    end)
    |&gt; elem(1)
    |&gt; to_string()
    |&gt; String.replace(~r/.?(.)\1\1\1/, fn m -&gt; fours.(m, romans, fours) end)
  end
end
</code></pre> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="172197" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/what-do-you-think-of-my-solution-for-converting-integers-to-roman-numerals/30554/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-172197" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="172197"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>