Reducing the conceptual load of reduce
Abstract: calls to reduce
are difficult to understand. An example reduce
in a code sample is analyzed and discussed to explore the reasons. An alternative version without the reduce
is offered and compared.
I'm really keen on legible code. Apart from correctness, I think legibility is the most important thing for software engineering, where the main cost is in communicating things between people. The trick is that "legible" is neither objective nor quantifiable, but that's actually how it should be: we're dealing with humans, here.
In my quest for legibility, I do code reviews. I recently saw the following code (in Clojure). I think it's not quite as legible as it could be.
Continue reading →Mapping will to keystrokes: an experiment in the Colemak keyboard layout
About 3 weeks ago, I switched to the Colemak keyboard layout. It's much more efficient than the standard Qwerty layout but not as alien as the more well-known alternative, Dvorak.
It's been challenging. I was typing around 115 words per minute using Qwerty, and I had to toss that out and start over from zero. Ouch. The first week was especially frustrating. Things took much longer to type than my mind was expecting, and I often lost my train of thought in the process of getting it out of my head.
At this point, I've recovered about a third to a half of my speed. It's no longer very painful. I'm getting there.
However, there's one area in my muscle memory that is resisting the advances of the new Colemak regime: vim. Vim is frustrating all over again, much like when I was first learning it in college.
Continue reading →Lazy reduce in Clojure
We’re big fans of Clojure at Altometrics. It really gets out of the way and lets us get stuff done without a lot of fuss. Like many functional programming languages, Clojure features lazy sequences, which are especially useful if a given sequence is too big to fit into memory. (I’ll call such sequences “large” for the purposes of this post.)
I recently wrote the following Clojure function to lazily reduce
a large sequence. I couldn’t find a Clojure function that does this, so I figured it would be worth sharing. The discussion follows, but first, here’s the code:
(defn sidelong-reduce
"Do a lazy reduce, returning the original (lazy) sequence,
and passing the final accumulator value to `store-result!`
when the sequence is fully realized."
[seq f accum store-result!]
(lazy-seq
(if (empty? seq)
(do (store-result! accum) nil)
(let [[fst & rst] seq]
(cons fst
(sidelong-reduce rst f (f accum fst) store-result!))))))
Continue reading →
Analyzing Reddit Submission Times
I enjoy reading the Data is Beautiful sub-Reddit, which often has interesting and useful visualizations. The official guide has some tips for making a great post, including this one:
The best time to post is generally between 12pm and 5pm EST (UTC–5). Other times also work well, but most of the successful posts on /r/dataisbeautiful were posted in that time range.
I thought I'd test that assertion. So I wrote a simple tool in Clojure and gnuplot that queries the Reddit API for a particular subreddit, groups recent submissions by the submission hour (UTC), and creates a chart displaying the percentage of high-scoring submissions per hour. Here's the current chart for DataIsBeautiful:
Continue reading →