You cannot view this unit as you're not logged in yet. Go to Account to login.
8 Comments
romenigldon December 14, 2020 at 10:58 am
I like the exercises.
I just notice the exact number for run the tests.
They aren’t running in the beginning of each tests.
Exercise #1: mix test test/lists_test.exs:22
Exercise #2: mix test test/lists_test.exs:32
and the so on….
Right! You just pick a line that’s inside the test. I like to pick something around the middle so if I add or delete lines, the correct test is still run.
Ha! Yes, that works. This is more about practicing applying pattern matching in function headers. In case it wasn’t already known, you could use Enum.empty?(something) instead of writing a custom function at all! So, it’s really just about practice. 🙂
—
def sum_first_2([first, second | tl]), do: [first + second | tl]
IO.inspect(PatternMatching.Lists.sum_first_2([41,5,119,118]))
—> ~c”.wv”
For some reason, it just won’t behave on this. And here is my setup
—
try:
IO.inspect(PatternMatching.Lists.sum_first_2([41,5,119,118]), charlists: :as_lists)
I like the exercises.
I just notice the exact number for run the tests.
They aren’t running in the beginning of each tests.
Exercise #1: mix test test/lists_test.exs:22
Exercise #2: mix test test/lists_test.exs:32
and the so on….
Right! You just pick a line that’s inside the test. I like to pick something around the middle so if I add or delete lines, the correct test is still run.
Solution:
def sum_pair([first, second]), do: first + second
My solution:
def sum_pair([first, second | []]), do: first + second
Some overthinking went in there 🙂
But hey! It works! 🙂
For exercise 1 this only one function covers all tests:
I don’t know if it fits the philosophy of Elixir
Ha! Yes, that works. This is more about practicing applying pattern matching in function headers. In case it wasn’t already known, you could use
Enum.empty?(something)
instead of writing a custom function at all! So, it’s really just about practice. 🙂def sum_first_2([first, second | tl]), do: [first + second | tl]
IO.inspect(PatternMatching.Lists.sum_first_2([41,5,119,118]))
—> ~c”.wv”
For some reason, it just won’t behave on this. And here is my setup
pattern_matching/lib/pattern_matching on 🐢 main [!?] ❄️ 🌴🌴 elixir -v
Erlang/OTP 27 [erts-15.0] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit:ns]
Elixir 1.17.1 (compiled with Erlang/OTP 27)
—
def sum_first_2([first, second | tl]), do: [first + second | tl]
IO.inspect(PatternMatching.Lists.sum_first_2([41,5,119,118]))
—> ~c”.wv”
For some reason, it just won’t behave on this. And here is my setup
—
try:
IO.inspect(PatternMatching.Lists.sum_first_2([41,5,119,118]), charlists: :as_lists)