while true live

Musings on the fatality of infinite loops and other stuff.

Rolling the Dice Using Random Enum Values

Every wanted to roll the dice in Haskell? Let’s say you have a data type like

data Number = One | Two | Three | Four | Five | Six
   deriving (Enum, Bounded, Eq, Show)

How do you generate a random Number value? There are several ways to do it, usually in an IO context, obviously. In order to use it in a common monad transformer stack, the primitives of the MonadRandom type class come to my mind. So it would be most convenient to get a random value using getRandom. Anyway, as much fun as rolling the dice is, let’s derive a solution working for all bounded Enums.

Toying With Racket

2 hours to go… I’ve been waiting for the men’s tennis US Open final to start. What should I do? In the end, I’ve been killing time listening to that Functional Geekery podcast episode which features Matthew Flatt talking about the Racket programming language. Realizing the irony, my thoughts went something like: “Racket, tennis?! Can’t be a coincidence! Still some time to burn, so I will programm something tennis-like in Racket!” So let’s open the Racket quickstart to somehow learn Racket and get an idea what the tennis-like thing should be.

Historical Elo Tennis Rating

Recently I’ve come across that fivethirtyeight article comparing the female tennis all-time greats by means of an Elo rating variant (originally known from chess). It concludes that Serena Williams does not have the highest rating in history, although she is frequently considered as the greatest player of all time nowadays. Obviously, I wondered what’s the situation like in the men’s tennis world. Have Roger Federer’s record 17 Grand Slam wins led to the highest Elo rating as well? What about players like Borg and McEnroe? Anyway, I have found surprising results.

Approximating PI With PureScript

Oh, it is Pi day! To be honest, I had not known about it until today but it somehow made it into my Twitter stream. Well, after some relaxed Saturday morning browsing I stumbled across the Wolfram Pi approximation demonstration. It looks like this:

Pi is approximated by computing the area of the inscribed polygon and by dividing that area by the square of the circle radius – after all, the area of a circle is r²π. Obviously, that idea is that beautiful and so simple that Pi has thus been approximated thousands of years ago (of course, there are many better and faster ways!), so I decided to implement it myself.

Partial Matching in R

I’ve not been too exposed to statistics programming in the last few year, however, sometimes it couldn’t be avoided – and R couldn’t be avoided! (I still have too much self-esteem to think about falling back to Excel, even for the simplest things.)

I don’t hate R and I don’t like R; it just strikes me as very strange. Often, the syntax strikes me as odd and above all, the multitude of ways to express the same thing reminds of my little Perl experience. Anyway, I have subscribed to the Coursera R course for fun and in hope to somehow “get the better of R”, to grok in on a deep level or to at least get a more profound idea as to why I don’t like it ;)

Anyway, here is a language construct which is very strange and a little bit absurd, no whatever how often I think about it. It’s called partial matching. To put it bluntly, partial matching makes it possible to avoid spelling out the whole name of an element of a list in oder to access it. Let’s have a look.

Preview Your Github Markdown Before Committing!

Writing software documentation can be a pretty dull task and is neglected too often (at least by me). But what purpose does the best program serve if nobody knows how to use it? None.

Looking at Github, the Markdown formatted README.md is the front page of every repository and the first (and mostly only) source of documentation. Until recently, I have treated my README.md files rather shabbily; I would have thought that this lack of motivation originates from a general dislike of writing documentation, however, I might have changed my opinion. This blog post has opened my eyes to an alternative explanation: writing Github Markdown documentation is not trivial when you don’t have a chance to preview the resulting markdown before committing! In that case the output will look not as expected more often than not – which makes the committer angry as s/he needs to push another commit just for improving the Markdown. Anyway, I remember feeling annoyed quite frequently by that procedure in retrospective. It does not need a stretch of imagination to foresee the negative influence that annoyance has on my willingness to provide README.md documentation… ;)

The solution is obvious but not that trivial. Obviously, there surely are plugins for various IDEs which can parse Markdown and output HTML markup. I remember having once installed such an Eclipse plugin. But things get trickier as Github does not use vanilla Markdown but its own brand of Markdown – it is not that easy to find a way to display Github Markdown with good confidence in its conformity; in addition it shouldn’t force me to use any special IDE as I do most stuff in Emacs.

The script that I use now was mentioned in the referenced blog post above: flavor.rb. It is a ruby script which actually asks the Github API (!) to do the hard work, namely providing the HTML output! That looks stupid at first glance but it is actually the only way to be really sure of the preview’s congruence with the actual display on the Github page…

Else Return Null

I guess everybody has a story or two in store about Nullpointer exceptions in Java (or in any language with nullable values). Sooner or later you will make the acquaintance of the dreaded stacktrace line denoting that you did something terribly wrong, demanding a value where none exists, and you just wonder why, how the hell can that object be null?! during an hour of painful debugging… you know that? Good. Well, I won’t ponder over how to avoid it, how to apply proper error handling etc. (that’s way too complicated, I’m tired) but I’d just like to point out my personal favourite, the famous else return null idiom.

Using XMonad With a Projector

I’ve been using XMonad for a few years now (lately in combination with GNOME) but just recently I have found a way to make it work in a useful way in the context of a presentation with a projector as a second screen.

In general, XMonad does have decent multi-screen support. Building on Xinerama, the default configuration provides key-binding support for three screens; how workspaces are mapped to the screens and how this mapping can change, respectively, can be pretty confusing at first, though – it is best explained in here. Whenever I want to use a projector things get pretty tricky, however.

Using Google Universal Analytics With Octopress

When I first set up my Github pages Octopress blog I followed those instructions to set Google Analytics tracking up – and it worked. However, in May Google Analytics somehow decided to move my account to Universal Analytics – maybe I had approved it without really knowing what it meant…

Well, what is Universal Analytics? I have no idea! The important thing to notice is, though, that you need to change your JavaScript tracking code if you make that transition; obviously, I didn’t know that. Otherwise tracking wouldn’t work any longer; would it? As a matter of fact, it somehow did.

It is not only not right, it is not even wrong.

Wolfgang Pauli

Actually, my Analytics dashboard did show some visits and hits, that’s why I didn’t notice the problem for a long time, however, I guess it only mentioned 10% of them! Anyway, after a long debugging session with Google Tag Assistant and Google Analytics Debugger I figured out the necessity to change the code, finally.

Shortly, you just need to change your source/_includes/google_analytics.html to this code:

{% if site.google_analytics_tracking_id %}
  <script type="text/javascript">
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),

ga('create', '{{ site.google_analytics_tracking_id }}', 'auto');
ga('send', 'pageview');
{% endif %}

Concerning Github pages, it’s not necessary to include a call to set your domain to github.io any longer. It just works out of the box – what a progress! It is not only right, it is not even wrong now!

The Notoriously Point-free “((X.).)” Trick

Point-free code is ubiquitous in every well-polished Hackage library. It’s more concise than its pointed counterparts and feels more “hygienic” as function composition immediately translates to its mathematical background.

let pointFree = (+1) . read
let notPointFree x = (+1) (read x)
-- notPointFree 'mentions' its argument "x"

So far, so good. However, you get a problem in your chain of function composition when your right-most function “takes two parameters”.

let plusOne = (+1) :: Int -> Int
let readTwoArgs = (\x y -> read x + read y) :: String -> String -> Int

let coolSolution = plusOne . readTwoArgs :: String -> String -> Int
-- Type checker doesn't like coolSolution

let boringSolution x y = plusOne (readTwoArgs x y)
-- it's boring since it's not point-free

Unfortunately, coolSolution is not well-typed; this might drive you nuts for some time: It’s obvious that composing those functions must somehow work. You’d think: “Even GHCI must have understood what I mean!” However, Haskell doesn’t care for what you mean as much as it cares for type-safety ;) ! So you might fall back on boringSolution which is only half the fun as it’s not point-free… anyway, there IS a way to compose those functions!