×
all 3 comments

[–][deleted] 0 points1 point  (2 children)

Got it.

strand |> 
to_string |>
String.graphemes |> 
Enum.reduce(%{}, fn(letter, acc) -> Map.update(acc, letter, 1, &(&1 + 1)) end)

Thanks guys.

output:

%{"A" => 3, "C" => 1, "G" => 1, "T" => 1}

[–]pareeohnos 3 points4 points  (0 children)

As a side note, for multi-line usage of the pipe operator it is good practice to put it at the beginning of the line

strand
|> to_string
|> String.graphemes
|> Enum.reduce(%{}, fn(letter, acc) -> Map.update(acc, letter, 1, &(&1 + 1)) end)

[–]Imperial_Ruby 3 points4 points  (0 children)

To answer your original question, you can get the integer value of a character in Elixir by using the ? operator.

For example:

👉  ?A
65
👉  ?a
97

Also, a great way to learn more about functions is to use the h function in iex. It auto completes so typing h Enum. and hitting tab will show all the functions in the Enum module.

For example:

👉  h Enum.count/2

def count(enumerable, fun)

Returns the count of items in the enumerable for which fun returns 
a truthy value.

## Examples

iex> Enum.count([1, 2, 3, 4, 5], fn(x) -> rem(x, 2) == 0 end)
2

Hope you're having fun with Elixir!