You cannot view this unit as you're not logged in yet. Go to Account to login.
4 Comments
Ali ELBaitamon November 23, 2020 at 6:38 am
I got stumped when I attempted the first exercise comparing against the value of “age”. I thought the case expression should be the “age” value which I get from the function parameter: def classify_user(%User{age: age})… I didn’t know how it will work:
case age do
age when age >=18 -> {…}
age when age {…}
_ ->
end
I found it weird and different from other languages. I looked back at the examples in the lesson and they all looked unusual. The case expression usually is not a constant in other languages but in the lesson examples it is: case nil do, case true do, case 10 do!
Eventually, I had to look at the solution and realized that the case expression is the entire User structure not the age value only.
I understand the solution but I just wanted to share what someone new to Elixir struggles with.
Thanks for sharing your experience. I’ll give it some more thought about how it could be improved. Feel free to email me directly with any suggestions! 🙂
I think I was overthinking what the exercises are asking 🙂 I have gone through many exercism.io and codewars exercises and so far pattern matching using function is what I used and never needed case..do. Case..do looks like a secondary construct in Elixir (pattern matching on functions which looks similar is used more often). One important thing I learned from this lesson though is the case..do as an expression which will be useful as I am progressing with Elixir.
Yes, the same pattern matching that applies in a function header applies in a case statement. However a case statement is often needed because you need to call a function to get the data you need to analyze.
I got stumped when I attempted the first exercise comparing against the value of “age”. I thought the case expression should be the “age” value which I get from the function parameter: def classify_user(%User{age: age})… I didn’t know how it will work:
case age do
age when age >=18 -> {…}
age when age {…}
_ ->
end
I found it weird and different from other languages. I looked back at the examples in the lesson and they all looked unusual. The case expression usually is not a constant in other languages but in the lesson examples it is: case nil do, case true do, case 10 do!
Eventually, I had to look at the solution and realized that the case expression is the entire User structure not the age value only.
I understand the solution but I just wanted to share what someone new to Elixir struggles with.
Thanks for sharing your experience. I’ll give it some more thought about how it could be improved. Feel free to email me directly with any suggestions! 🙂
I think I was overthinking what the exercises are asking 🙂 I have gone through many exercism.io and codewars exercises and so far pattern matching using function is what I used and never needed case..do. Case..do looks like a secondary construct in Elixir (pattern matching on functions which looks similar is used more often). One important thing I learned from this lesson though is the case..do as an expression which will be useful as I am progressing with Elixir.
Yes, the same pattern matching that applies in a function header applies in a
case
statement. However a case statement is often needed because you need to call a function to get the data you need to analyze.