elixir-koans icon indicating copy to clipboard operation
elixir-koans copied to clipboard

Keyword lists / assert_raise ArgumentError

Open robbyrussell opened this issue 7 years ago • 13 comments

As a first timer to Elixir, I found myself scratching my head at the last koan for KeywordLists. It's the first function and dealing with raising an argument.

https://github.com/elixirkoans/elixir-koans/blob/08c90ce0d84120d315a8ee626ff79b5b445ececd/lib/koans/07_keyword_lists.ex#L29

Nor have I been exposed to maps so this felt like I was just trying to get it to pass without really knowing why it is. (perhaps that's part of the point of Koans to get exposed to new things without understanding them yet?)

robbyrussell avatar May 21 '18 00:05 robbyrussell

This is really great feedback, @robbyrussell! Thank you! It's always hard to keep your finger on the pulse of these koans as they grow and change over time. You have to work through them over and over to really feel out the flow (which tends to just not happen in reality). So this kind of feedback from the community is really important ❤️

iamvery avatar May 21 '18 13:05 iamvery

Thanks @robbyrussell for the feedback! From a cursory look, I think we rearranged the koans and didn't pay attention to surroundings: The first koan also mentions maps, while the one you mentioned then goes "But unlike maps...".

Would it make sense to flip the koans, move them to 08_maps and then compare maps to keyword lists?

felipesere avatar May 21 '18 14:05 felipesere

Would it make sense to flip the koans, move them to 08_maps and then compare maps to keyword lists?

Possibly, I was just a bit thrown off by what the example was doing. (especially since it looked like I just needed to write the same thing as a few lines up but don't (yet) know why it'd trigger an ArgumentError

robbyrussell avatar May 21 '18 15:05 robbyrussell

(side-note: was really enjoying my way through this project. Was a fun Sunday afternoon project)

robbyrussell avatar May 21 '18 15:05 robbyrussell

Possibly, I was just a bit thrown off by what the example was doing. (especially since it looked like I just needed to write the same thing as a few lines up but don't (yet) know why it'd trigger an ArgumentError

Kind of similar here. The koan subject "But unlike maps, the keys in keyword lists must be atoms" explains the salient point, but I don't understand the syntax fn -> not_kw_list[___]. What is fn ->? The koan/test passes once the assertion is rewritten to fn -> not_kw_list[{"foo", "bar"}] but I don't know if that is the correct answer or not.

It's no big deal. This is my first exposure to Elixir so having to look things up to understand what's going on isn't an issue.

[Some time later...]

I found "The left to right arrow (->) is used to establish a relationship between left and right." but I'm having trouble figuring out what that actually means. Is this a common concept? My background is 10 years of Delphi dev. https://hexdocs.pm/elixir/master/syntax-reference.html#left-to-right-arrow

s-oram avatar Jun 16 '18 02:06 s-oram

👋 @s-oram

What is fn ->?

Thanks for the link to the docs for ->! Honestly in practice, I don't think that -> is often thought of on its own. So to answer your specific question quoted above, the syntax fn -> ... end is used to define an anonymous function. To illustrate that, give this a try:

one = fn -> 1 end
one.() 
# => 1
add = fn x, y -> x + y end
add.(2,3)
# => 5

You'll sometimes see these written on multiple lines like:

add = fn
  x, y -> x + y
end

And, in fact, you can define multiple cases for a function using this sytnax:

lolwat = fn
  "lol" -> "wat"
  _ -> "haha"
end
lolwat.("lol")
# => "wat"
lolwat.("anything")
# => "haha"
lolwat.("rly")
# => "haha"

There's probably quite a bit more to say about all this, but hopefully it gives you a taste. Something to look out for is the shorthand syntax for anonymous functions: &(&1+&2) is functionally equivalent to fn x,y -> x+y end.

iamvery avatar Jun 25 '18 14:06 iamvery

@iamvery That helps. Thanks for taking the time for the explanation. :)

s-oram avatar Jun 26 '18 00:06 s-oram

@iamvery do you want to turn that into Koan? 🤣

felipesere avatar Jul 04 '18 06:07 felipesere

@felipesere I could try turning it into a koan if you would like?

s-oram avatar Jul 04 '18 07:07 s-oram

Yes, please! I see that 13_functions.ex should already cover most of it, but please have a look and enhance and adjust as necessary ❤️💚💙💛

On Wed, 4 Jul 2018 at 08:44 Shannon Oram [email protected] wrote:

@felipesere https://github.com/felipesere I could try turning it into a koan if you would like?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/elixirkoans/elixir-koans/issues/222#issuecomment-402393118, or mute the thread https://github.com/notifications/unsubscribe-auth/ABw7TFX39wf_6ewvqS0RLvP22dY0Lz6oks5uDHJigaJpZM4UGSKS .

felipesere avatar Jul 04 '18 08:07 felipesere

teamwork!

iamvery avatar Jul 05 '18 12:07 iamvery

@felipesere Great, I've got stuff on for the next couple of days. I'll aim to have something finished by the end of next week.

s-oram avatar Jul 06 '18 01:07 s-oram

Kind of similar here. The koan subject "But unlike maps, the keys in keyword lists must be atoms" explains the salient point, but I don't understand the syntax fn -> not_kw_list[___]. What is fn ->? The koan/test passes once the assertion is rewritten to fn -> not_kw_list[{"foo", "bar"}] but I don't know if that is the correct answer or not. @s-oram The right answer is fn -> not_kw_list["foo"] ;-)

tolbier avatar Jan 02 '19 19:01 tolbier