I had written some complicated looking code at work recently (shhh, let's keep that between us ok?). Something about it didn't seem right to me, but I was busy at the time so I wrote a TODO to come back and find a way to make it simpler.
Since the code is so confusing it is virtually impossible to read at a glance and understand its intent, I will explain what it does. It takes in a map where the keys are numbers of days over a given SLA, and the values are the number of shipments that were over the SLA by that amount of days. The map actually can and does have more keys on it than that, but for the purposes of this article we can ignore them. The function takes that map and converts it to a version where the values are not counts of the individual SLA, but instead the current running percentage totals.An example:
What makes this code end up so complicated is that it is complecting three things: generating the series of sums, dividing them, and creating a new map to return.
The solution is to break those 3 parts up.
First we generate the series of sums using reductions.
Second, we divide each by the total to convert them to percentages.
Thirdly, we create a new map with percents as the values instead of counts.
Another thing you might notice in this code is that I purposely used the more verbose `#(get count-based-past-SLA-map %)` when I could have just used `count-based-past-SLA-map`, as it is already a function! I prefer to err on the side of making my code as obvious as possible, so I try to avoid using data structures as functions except under a few special cases. But I guess that would be a different topic for a different blog.