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
[Exercise #07 + #08] Discussion and Help. #7
Comments
Thanks for sharing these exercises, super cool 👍. With #8 I came up with a little bit diff. model so it made insert part simpler:
but on the other hand, retrieving shopping cart items is cumbersome now because of these mappings:
|
Hey @robertlyson! I like where you're going with this -- My previous reply (not particularly beginner friendly 💩)As you noted, it's still a bit unergonomic. This is where lenses would come quite useful. With the `generic-lens` package we could write something like this:-- Provided that we revert the ShoppingCart definition.
data ShoppingCart
= ShoppingCart (Map.Map String Item)
deriving (Eq, Show, Generic, Aeson.FromJSON, Aeson.ToJSON)
appendToCart item
= ShoppingCart
$ Map.insertWith
(\existing new -> existing & field @"quantity" ~+ (quantity new))
(model item)
item
cart There's a lot to unpack here.
The I appreciate it looks scary so don't worry too much about it. I think your solution is absolutely fine! I generally don't reach for lenses and I hate it when people go crazy with them because I can't understand what's going on (ie. the classic Haskell bullshit that I want to avoid here). I find it quite ironic that my first comment on ZBH is about lenses :) Good job getting to Exercise 08, I hope to add more soon! |
This issue is specific for discussion on Exercise #07 and #08.
You can ask anything, there are no stupid questions!
Did you find the exercise difficult?
Did the tests make sense?
Was the goal clear?
The text was updated successfully, but these errors were encountered: