PurelyFunctional.tv Newsletter 385: two map patterns: entity vs index
Issue 385 - July 06, 2020 · Archives · Subscribe
Clojure Tip 💡
two map patterns: entity vs index
In Clojure, we tend to use hash maps a lot. But we use them in two very distinct patterns---as an entity and as an index.
The entity pattern is where we store information about a thing in a hash map. We tend to use keywords for the keys. The types of the values depend on the meaning we assign the keywords. This pattern lets us easily aggregate related information into one collection and access individual pieces of information later by name.
The index pattern is more like what we think of as the traditional use of hash maps. The keys are some value we have and the values are some value we want. For instance, we could store user records by their user id. We know the user id and we want to look up the user record in the hash map. In this pattern, we see that the keys are all of the same type and the values are all of the same type.
These two patterns are super common and I'm probably not telling you anything you don't know. But I'm not sure if anyone has named the two patterns. Names give us power, so maybe I've just increased our collective power.
In Clojure we use the same data type for both of these patterns. I've thought a lot about whether these two uses are confusing enough to warrant two separate concepts in the language. Sometimes different intentions should be distinguished in the language.
The more I think about it, however, the less I like the idea of having
different language constructs. We would basically have to duplicate any
operation that worked on one but would also be useful on the other. And
what would prevent you from using the wrong one in a given situation?
Clojure doesn't have static types, so that's out. Runtime checks? No
thanks! Although I think there's something there (much like the
distinction between deftype
and defrecord
) I don't think I know how
to slice them up. Any ideas?
Podcast episode🎙
Listen and watch here my l ast episode: My response to Out of the Tar Pit.
The next one is about Why Functional Programming Matters by John Hughes. Read it before next Monday and then listen to the podcast.
Clojure Challenge 🤔
Last week's challenge
The challenge in Issue 384 was to write a function that checks if two lines are parallel. You can find the submissions here.
It was really hard for me! There are many corner cases---possibilities to divide by zero and special cases like horizontal and vertical lines. There is apparently a simple formula, but I didn't know it and I was not on the way to figuring it out.
Please do participate in the discussion on the gist where the submissions are hosted. It's active and it's a great way to get comments on your code.
This week's challenge
roots of a quadratic equation
Quadratic equations can be represented by three numbers, a
, b
, and
c
, which are the coefficient of x^2
, the coefficient of x
, and the
constant term. The roots of a quadratic equation are everywhere where it
touches the x axis, meaning the equation is equal to zero.
You can use the quadratic formula which calculates the roots. In fact, that's your task: write a function that returns the roots of a quadratic equation using the quadratic formula. Here is more information about it.
Note: you don't have to return complex roots if the curve does not cross the x-axis.
Thanks to this site for the challenge idea where it is considered Medium level in Python.
You can also find these same instructions here. I might update them to correct errors and clarify the descriptions. That's also where submissions will be posted. And there's a great discussion!
As usual, please reply to this email and let me know what you tried. I'll collect them up and share them in the next issue. If you don't want me to share your submission, let me know.
Rock on!
Eric Normand