Counting characters occurrences in String

2,358 views
Skip to first unread message

Jean Duarte

unread,
Mar 2, 2015, 1:46:26 PM3/2/15
to elixir-l...@googlegroups.com
Hi folks.

Is there any simpler way to count characters occurences other than this?

Enum.group_by(~w{A G C T T T T C A}, &Base.encode16/1) |> Dict.values |> Enum.map(&Enum.count/1)

I saw similar code in Python which looks like this:

s.count("A"), s.count("G"), s.count("C"), s.count("T")


Cheers

r...@dimpixel.com

unread,
Mar 2, 2015, 1:55:57 PM3/2/15
to elixir-l...@googlegroups.com
I did this in a test script a few weeks ago using Enum.reduce. This is what I came up with using Map, you could convert to Dict:

Enum.reduce ~w{A G C T T T T C A}, %{}, fn(letter, acc) ->
  count = Map.get(acc, letter) || 0
  Map.put(acc, letter, count+1)
end

- Rob

José Valim

unread,
Mar 2, 2015, 1:57:32 PM3/2/15
to elixir-l...@googlegroups.com
Using Map API is better if you know it is a map (which you do in your case). The next step is to change it to use Map.update/4 instead of doing 2 operations. :)
--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/f9fafb82-9c3a-4995-a8f4-292189265492%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--


José Valim
Skype: jv.ptec
Founder and Lead Developer

r...@dimpixel.com

unread,
Mar 2, 2015, 2:14:37 PM3/2/15
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
That's great, I didn't know that Map.update/4 was a thing! :)

Enum.reduce ~w{A G C T T T T C A}, %{}, fn(letter, acc) ->
  Map.update(acc, letter, 1, &(&1 + 1))
end


On Monday, March 2, 2015 at 11:57:32 AM UTC-7, José Valim wrote:
Using Map API is better if you know it is a map (which you do in your case). The next step is to change it to use Map.update/4 instead of doing 2 operations. :)

On Monday, March 2, 2015, <r...@dimpixel.com> wrote:
I did this in a test script a few weeks ago using Enum.reduce. This is what I came up with using Map, you could convert to Dict:

Enum.reduce ~w{A G C T T T T C A}, %{}, fn(letter, acc) ->
  count = Map.get(acc, letter) || 0
  Map.put(acc, letter, count+1)
end

- Rob


On Monday, March 2, 2015 at 11:46:26 AM UTC-7, Jean Duarte wrote:
Hi folks.

Is there any simpler way to count characters occurences other than this?

Enum.group_by(~w{A G C T T T T C A}, &Base.encode16/1) |> Dict.values |> Enum.map(&Enum.count/1)

I saw similar code in Python which looks like this:

s.count("A"), s.count("G"), s.count("C"), s.count("T")


Cheers

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-talk+unsubscribe@googlegroups.com.

Jean Duarte

unread,
Mar 2, 2015, 2:31:57 PM3/2/15
to elixir-l...@googlegroups.com, jose....@plataformatec.com.br
Thanks guys.

Far simpler and clearer than expected ;)
Reply all
Reply to author
Forward
0 new messages