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


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<aside class="quote no-group" data-username="amnu3387" data-post="11" data-topic="39390">
<div class="title">
<div class="quote-controls"></div>
<img alt="" width="24" height="24" src="https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/ed8c4c/48.png" class="avatar"> amnu3387:</div>
<blockquote>
<p><code>def maybe_reshape(&lt;&lt;"int_", _&gt;&gt;, v) when is_binary(v) do</code></p>
</blockquote>
</aside>
<p>you can make it into a <code>defp</code> and name the function differently and you will not need to run the <code>is_binary(v)</code> check since it was checked in the previous call.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="212727" 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/help-to-simplify-optimize-a-function/39390/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-212727" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="212727"
                     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="212728" data-post-id="212728">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="amnu3387" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  amnu3387
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Yeap, but then only works for atom keyed maps - that version was to allow binary keys too - but good pointers</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="212728" 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/help-to-simplify-optimize-a-function/39390/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-212728" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="212728"
                     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="212736" data-post-id="212736">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Here’s my improved version built on top of <a class="mention" href="/u/amnu3387" rel="nofollow">@amnu3387</a> 's version.</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">
defmodule Sanitizer do
  @spec parse_map_int_fields(map) :: map
  def parse_map_int_fields(map) when is_map(map) do
    Enum.reduce(map, [], fn
      {k, v}, acc when is_atom(k) and is_binary(v) -&gt;
        [{k, transform_v(Atom.to_string(k), v)} | acc]

      k_v, acc -&gt;
        [k_v | acc]
    end)
    |&gt; :maps.from_list()
  end

  defp transform_v(&lt;&lt;"int_", _&gt;&gt;, v) do
    case Integer.parse(v) do
      {v_int, ""} -&gt; v_int
      _ -&gt; nil
    end
  end

  defp transform_v(_k_string, v) do
    v
  end
end
</code></pre>
<p>I use a list to build the new map, and then convert it into a map. Could be faster or not depending on how many items get updated from the original map.</p>
<p>Check out this new test_map. If you really want to check on a full representation of an integer, you need to pattern match on the remainder, to be <code>""</code></p>
<pre data-code-wrap="elixir"><code class="lang-elixir">test_map = %{
  int_a: "1",
  int_b: "not_a_int!",
  int_c: "1.2",
  int_d: "1x",
  int_x: 42,
  str_a: "This is a string field",
  str_b: "Another string field",
  fl_a: 0.0,
  
}

Sanitizer.parse_map_int_fields(test_map)
</code></pre>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(5)&gt; Sanitizer.parse_map_int_fields(test_map)
%{
  fl_a: 0.0,
  int_a: 1,
  int_b: nil,
  int_c: nil,
  int_d: nil,
  int_x: 42,
  str_a: "This is a string field",
  str_b: "Another string field"
}
</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="212736" 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/help-to-simplify-optimize-a-function/39390/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-212736" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="212736"
                     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="212781" data-post-id="212781">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="rmoretto" src="/assets/icons/user-9f439610.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  rmoretto
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>I agree with you that to be more precise with the parsing the <code>""</code> remainder should be match, but in my use case is better that any integer is saved than none.</p>
<p>I have run the benchmark with the code that you shared, only changing the line <code>{v_int, ""}</code> to <code>{v_int, _}</code> to match the other functions, here are the results:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">Name                             ips        average  deviation         median         99th %
mnussbaumer_response        960.24 K        1.04 μs  ±2517.65%        0.94 μs        1.80 μs
gregvaughn_response         868.02 K        1.15 μs  ±1919.57%        1.03 μs        2.04 μs
kokolegorille_response      827.66 K        1.21 μs  ±2517.58%        1.09 μs        1.65 μs
ericgray_response           724.15 K        1.38 μs  ±1604.94%        1.26 μs        2.50 μs
chungwong_response          656.80 K        1.52 μs  ±1600.17%        1.38 μs        2.71 μs
parse_map_int_fields        623.51 K        1.60 μs  ±1424.02%        1.45 μs        2.52 μs
eksperimental_response      612.94 K        1.63 μs  ±1509.09%        1.47 μs        2.92 μs

Comparison:
mnussbaumer_response        960.24 K
gregvaughn_response         868.02 K - 1.11x slower +0.111 μs
kokolegorille_response      827.66 K - 1.16x slower +0.167 μs
ericgray_response           724.15 K - 1.33x slower +0.34 μs
chungwong_response          656.80 K - 1.46x slower +0.48 μs
parse_map_int_fields        623.51 K - 1.54x slower +0.56 μs
eksperimental_response      612.94 K - 1.57x slower +0.59 μs
</code></pre>
<p>I guess that the <code>:maps.from_list()</code> is too expensive to be worth in this specific case?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="212781" data-batch-url="/posts/batch_likers">
                        1
                      </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/help-to-simplify-optimize-a-function/39390/15">Post #14</a>
	                </div>
	            </div>
              <div id="likers-container-212781" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="212781"
                     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 #14"></div>
  </section>
</div>
    <div class="postbit" id="212815" data-post-id="212815">
  <section>
    <div class="post-wrap">


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

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>Naive solution on a first try:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule IntMapSanitizer do
  @test_map_1 %{
    greet: "Hirrow",
    int_a: "17",
    int_b: "gibberish",
    int_c: "428",
    farewell: "Aufwiedersehen"
  }

  defp is_int_name?(k) when is_atom(k) do
    k
    |&gt; Atom.to_string()
    |&gt; String.starts_with?("int_")
  end

  defp should_keep?(v) when is_nil(v) or is_integer(v), do: false
  defp should_keep?(_), do: true

  defp parse_to_int_or_nil(v) do
    case Integer.parse(v) do
      :error -&gt;
        nil

      {parsed, _} -&gt;
        parsed
    end
  end

  def sanitize(%{} = map) do
    for {k, v} &lt;- map, is_int_name?(k), should_keep?(v), reduce: map do
      acc -&gt; Map.update(acc, k, nil, &amp;parse_to_int_or_nil(&amp;1))
    end
  end

  def test1() do
    sanitize(@test_map_1)
  end
end
</code></pre>
<p>Try in <code>iex</code>:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">iex(1)&gt; IntMapSanitizer.test1()
%{
  farewell: "Aufwiedersehen",
  greet: "Hirrow",
  int_a: 17,
  int_b: nil,
  int_c: 428
}
</code></pre>
<p>Haven’t measured but <code>for</code> comprehensions with filters and the <code>reduce</code> option have always been fairly fast in my experience.</p>
<p>Maybe if you make a public GitHub repo then we can make a benchmarking arena. <img src="https://forum.elixirforum.com/images/emoji/apple/slight_smile.png?v=15" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="212815" data-batch-url="/posts/batch_likers">
                        1
                      </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/help-to-simplify-optimize-a-function/39390/16">Post #15</a>
	                </div>
	            </div>
              <div id="likers-container-212815" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="212815"
                     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>