Enum.group_by(~w{A G C T T T T C A}, &Base.encode16/1) |> Dict.values |> Enum.map(&Enum.count/1)
s.count("A"), s.count("G"), s.count("C"), s.count("T")
--
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.
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) || 0Map.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.
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.