Exercises in Programming Style–Code Golf

NOTE : read the rest of the series, or check out the source code.

If you enjoy read­ing these exer­cises then please buy Crista’s book to sup­port her work.

exercises-prog-styles-cover

Fol­low­ing on from the last post, we will look at the Code Golf style today.

 

Style 6 – Code Golf

This style came from Code Golf, which is also often associated APL, another language that I’ve had a lot of fun with in recent months!

image

Constraints

  • As few lines of code as possible

 

Well, cover your eyes folks, this one ain’t pretty…

The lines are so long that you have to click on the image to view it in full just to be able to read the original source code properly…

image

But, but, but, it does solve the term frequency problem in only 3 lines of code! 3 lines!

3 unbelievably hard to read lines of code 

And that’s where this style falls down, the pursuit for brevity often comes at the expense of readability.

It can also be argued that what we have done here is rearranging the steps in fewer lines without necessarily reducing the number of steps. That is, we haven’t reduced the number of things that needs to be kept in our head in order to understand what this piece of code is doing to solve the problem.

On this note, APL’s syntax is often described as unreadable, but I think the problem is not readability but rather familiarity. Its syntax and built-in abstractions are both alien to most developers, and very different to what they’re used to.

When I was learning API, there were a few mental hurdles that I needed to jump through:

  • glyph-based function names
  • functions have monadic (i.e. prefix) and dyadic (i.e. infix) forms
  • everything works on matrices as well as scalars
  • precedence always flow from right-to-left, except when braces are used

once I built up some familiarity with the language I actually found APL pretty easy to read.

 

You can find all the source code for this exercise here.

3 Comments

  1. Sehnsucht   •  

    In the pur­suit for brevity at the expense of readability I managed to shorten even more the code (there will be a warning for using ^ to concat string but that saves me four space).
    The space are most for fun ; where I really gain is

    binding both File.ReadAllText and __SOURCE_DIRECTORY__ to short values using let in syntax in first line
    using the set function instead of Set.ofArray
    using a sequence comprehension for w (no need for Seq.cast because [if I made no mistake] comprehension work on IEnumerable rather that it’s generic counterpart)

    replacing your GroupBy then Select with Seq.countBy with the identity function (id)
    and also on the last part inside iter where I partially apply (||>) with printfn to avoid writing a full lambda

    I also thought about dropping the ToLower part and putting the case insentive inline option inside the pattern “(?-i)[a-z]{2,}” but that will probably change the overall result because the collected match value won’t be in lowercase anymore

  2. Pingback: F# Weekly #44, 2015 | Sergey Tihon's Blog

  3. Pingback: Exercises in Programming Style–Infinite Mirror | theburningmonk.com

Leave a Reply

Your email address will not be published.