Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyword lists / assert_raise ArgumentError #222

Open
robbyrussell opened this issue May 21, 2018 · 13 comments
Open

Keyword lists / assert_raise ArgumentError #222

robbyrussell opened this issue May 21, 2018 · 13 comments

Comments

@robbyrussell
Copy link

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.

koan "But unlike maps, the keys in keyword lists must be atoms" do

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?)

@iamvery
Copy link
Collaborator

iamvery commented May 21, 2018

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 ❤️

@felipesere
Copy link
Collaborator

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?

@robbyrussell
Copy link
Author

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
Copy link
Author

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

@s-oram
Copy link
Contributor

s-oram commented Jun 16, 2018

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

@iamvery
Copy link
Collaborator

iamvery commented Jun 25, 2018

👋 @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.

@s-oram
Copy link
Contributor

s-oram commented Jun 26, 2018

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

@felipesere
Copy link
Collaborator

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

@s-oram
Copy link
Contributor

s-oram commented Jul 4, 2018

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

@felipesere
Copy link
Collaborator

felipesere commented Jul 4, 2018 via email

@iamvery
Copy link
Collaborator

iamvery commented Jul 5, 2018

teamwork!

@s-oram
Copy link
Contributor

s-oram commented Jul 6, 2018

@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 added a commit to s-oram/elixir-koans that referenced this issue Jul 11, 2018
Koan added following comment on Github.
elixirkoans#222 (comment)

[quote]
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"
```

[/quote]
@tolbier
Copy link

tolbier commented Jan 2, 2019

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"] ;-)

NateBarnes pushed a commit to NateBarnes/elixir-koans that referenced this issue Dec 31, 2019
Koan added following comment on Github.
elixirkoans/elixir-koans#222 (comment)

[quote]
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"
```

[/quote]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants