def to_rna(dna) do
Enum.each(dna, &convert(&1))
end
defp convert('G') do
'C'
end
defp convert('C') do
'G'
end
defp convert('T') do
'A'
end
defp convert('A') do
'U'
end
defp convert(letter) do
IO.puts("Unknown letter")
end
OK, it is getting clearer now. Thank you very much for such a detailed explanation, really useful
Taking into account that Enum.each returns just :ok and map returns a list, I think a better solution would be to return a corresponding matching letter, so in pseudo language it would read:
Also remember that everything is an expression (there are no statements) so everything evaluates to some kind of value.
This is why there is no return statement - functions have to evaluate to a value just like expressions, so they automatically “return” the last value they evaluated.
As I’m getting a list of characters (either of just one, A, or a sequence like AZERTY), I think I have to
make them match one by one to make a translation to other characters according to the translation table (like A should be replaced with T, Z-> with U, etc.)
build another chain of translated characters and return it.
Is it one of the solutions would as follows or there are other Enum, String, Enumerable, etc. hiddent functions ?
def to_rna(dna) do
Enum.map(dna, &convert(&1))
end
defp convert(?G) do
?C
end
test "transcribes guanine to cytosine" do
assert RNATranscription.to_rna('G') == 'C'
end
By the way, where does it come from, this ? sign, - I find it nowhere in official guides.
@spec to_rna([char]) :: [char]
def to_rna(dna) do
Enum.map(dna, &convert(&1))
end
defp convert(?G) do
?C
end
defp convert(?C) do
?G
end
defp convert(?T) do
?A
end
defp convert(?A) do
?U
end
defp convert(letter) do
IO.puts(:stderr, "Couldn't find matching for #{letter}")
letter
end
Ahr, I found it, - some words about the use of ? sign:
UTF-8 requires one byte to represent the characters h , e , and o , but two bytes to represent ł . In Elixir, you can get a character’s code point by using ?
I asked because it looked a little bit weird for me to compare two characters by using ? sign instead of === or == or just equals that exist in other languages. More of that, I also have to return the character to replace with using the same ? sign.
? simply generates the integer (codepoints) that is equivalent to the character, which in the case of ASCII will fit into 8 bits. What you call a comparison is actually a pattern match.
A pattern match is a conditional construct.
When a pattern match in a function head succeeds that function clause is evaluated
When the pattern match fails the next function head is attempted.
When you run out of function heads (i.e. each head failed the match) the program crashes.
defp convert(letter) do
case letter do
?G ->
?C
?C ->
?U
?T ->
?A
?A ->
?U
_ ->
IO.puts(:stderr, "Couldn't find matching for #{letter}")
letter
end
end