Thoughts Heap

A Blog by Roman Gonzalez.-

RSS
Feb
1st
Wed
permalink
Dec
2nd
Fri
permalink
Most of the biggest problems in software are problems of misconception.
— Rich Hickey.
Nov
30th
Wed
permalink

Haskell’s Show and pretty printing bad practice

Most of times when I was coding Haskell code, I always implemented the Show classtype whenever I wanted to print things on the screen in a nice and fashionable way.

It seems however this is a really bad practice that hasn’t been taught enough in the Haskell community. I discovered this in a StackOverflow question that had nothing to do with pretty printing, but the discussion led to that1.

A user named Matt Fenwick suggested that for whatever reason the guy who asked the question shouldn’t implement the Show classtype, later on I started to comment:

Me: Why not declare Show classtype for the Expr? just curious?

hammar: Show is meant as a form of lightweight serialization. It’s not meant for pretty-printing, although people often abuse it that way. In particular, if a Read instance is also defined, one should be able to expect that read . show is the identity function.

After that realization, I went out and looked over for different pretty printing solutions in Haskell. This was quite frustrating, for some reason I didn’t find any library that would define pretty printing functions for basic containers like List, Set, Map.

The closest I’ve found to an out of the box pretty printing solution was the pretty-show package. However this doesn’t create a new classtype for pretty printing, making it really limiting IMO.

There is also the well established pretty package, This one provides combinators to create pretty printing functions easily, however, it doesn’t provide a classtype or a default implementation for the containers mentioned above.

Lastly the most promising package I’ve found is called GenericPretty. This one provides a classtype for pretty printing and implements some Prelude ADT, however no containers whatsoever and no combinators for generating pretty printing.

In conclusion, the pretty printing solutions are so, so… yet there is potential for something new raising up in the future.


1 | Because of this the title was later changed to Show and Pretty-Printing

Nov
28th
Mon
permalink

Clojure’s repeatedly gotcha

When using Clojure’s repeatedly function to read lines from a terminal, or when trying to do IO of some sort, the behavior won’t be as expected

This is because repeatedly instead of calling the action n times, it generates a lazy seq for each time the action function gets called. Given that it is a lazy seq, if you don’t force the evaluation of the list, all but the first element will not be executed.

In order to avoid this, you have to use the dorun function, this is going to force evaluation for each element on a lazy seq, and it will discard the result of the seq altogether, this is because this function is expecting the action body to be for side-effects only

if you come from Haskell land, the idiom (dorun (repeatedly n body-fn)) would be the equivalent to call the function repeatM_.

An example:

Nov
8th
Tue
permalink

Vim’s Fugitive’s “Error detected while processing function <SNR>51_Commit”

At a random time on my development cycle, I was using the awesome Fugitive plugin developed by Tim Pope, and I got the following strange error:



Error detected while processing function 51_Commit:
line   52:
E480: No match: `=msgfile`
Press ENTER or type command to continue

After some googling, I found out the reason was that I installed just a few days ago the also awesome ctrlp plugin. When you were setting up ctrlp, you did something like this:



set wildignore=*/.git/*,*/.hg/*,*/dist/*,*/cabal-dev/*

Well… it seems Fugitive plugin doesn’t like that very much, if you remove the git portion from that setting, Fugitive will work as expected again

Thanks Interwebz

Oct
25th
Tue
permalink
The distinction between mechanism and policy is one of the best ideas behind the Unix design. Most programming problems can indeed be split into two parts: “what capabilities are to be provided” (the mechanism) and “how those capabilities can be used” (the policy). If the two issues are addressed by different parts of the program, or even by different programs altogether, the software package is much easier to develop and to adapt to particular needs.
— Jonathan Corbet, Greg Kroah-Hartman, Alessandro Rubini on Linux Device Drivers, 3rd Edition
Oct
13th
Thu
permalink
chiguire:

tejemaneje:

Dennis Ritchie has just passed away.
Surely you probably don’t know who this guy is, but his contributions to computer science made your life what it is right now.
Among other things, he created C, a programming language which is used to create all sort of OS and aplication software these days (and it will be that way in the future), and was key in the development of other important programming languages that came before. C made possible software as you know it.
Also, C was fundamental in the development of Unix, a OS that (one way or another) led to Linux. If you don’t want to mess with the Open Source / Licensed Software dilemma, keep in mind that Linux (and the Open Source movement in general) is a powerful engine that moves science and research nowadays.
In one week exactly the world has lost two of the greatest contributors in computer science. One dedicated to design, one dedicated to programming.
Maybe, if you allow me to put it that way, it is life saying us that both things are equally important.

Lo recordaremos hasta 2038.



Another Great One has gone&#8230;

chiguire:

tejemaneje:

Dennis Ritchie has just passed away.

Surely you probably don’t know who this guy is, but his contributions to computer science made your life what it is right now.

Among other things, he created C, a programming language which is used to create all sort of OS and aplication software these days (and it will be that way in the future), and was key in the development of other important programming languages that came before. C made possible software as you know it.

Also, C was fundamental in the development of Unix, a OS that (one way or another) led to Linux. If you don’t want to mess with the Open Source / Licensed Software dilemma, keep in mind that Linux (and the Open Source movement in general) is a powerful engine that moves science and research nowadays.

In one week exactly the world has lost two of the greatest contributors in computer science. One dedicated to design, one dedicated to programming.

Maybe, if you allow me to put it that way, it is life saying us that both things are equally important.

Lo recordaremos hasta 2038.

Another Great One has gone…

Oct
5th
Wed
permalink
Rest in peace dude&#8230; Thanks for the ride. Great men leave young.

Rest in peace dude… Thanks for the ride. Great men leave young.

Sep
29th
Thu
permalink

Testing URI canonicalization using QuickCheck

Today I was working on a custom web crawler for the company I’m working in. One of the problems I was having in the first implementations was that I was not canonicalizing the links, and I was visiting the same link more than once in the crawl.

Canonicalization of URI’s basically consist on removing any query string and fragment out of the given URI (at least, that is my understanding of canonicalization).

I needed to test a function that was doing the canonicalization of links, and decided to use a QuickCheck property to check just that. I implemented an Arbitrary instance for a pair of URI Strings, so that I could get the canonicalized version and a version with a query string and/or fragment, and compare the two after the canonicalization algorithm I developed.

The following code shows the Arbitrary instance of a newtype called URIPair



The next one shows how this is being used in a QuickCheck property test



Finally the Main action



By using the Gen monad combinators, and a newtype for URIs, I was able to implement in around 50 LOC, a generator for both canonicalized and normal links.

I’m still believing there must be a more elegant and shorter way to do this, or maybe I could reuse some code on Arbitrary instances for URIs, sadly no luck so far.

If you are Haskell developer yourself and want to provide insights, please fork the gist and show us how this can be done in a better way.

Cheers.

Sep
23rd
Fri
permalink

Puppet: ERROR 400 on SERVER: Must pass ‘param-name’ to Class[‘class-name’]

I’m currently playing with puppet doing some changes to my modules to support Archlinux, at this time I did several changes to my git module, Initially the implementation was something like this:

And then, it became something like this:

In the end I added some new parameters to my git class, and changed my nodes.pp file accordingly:

For some crazy reason, the puppet agent was throwing me the following error:

err: Could not retrieve catalog from remote server: Error 400 on SERVER: Must pass user to Class[Git] at /etc/puppet/modules/git/manifests/init.pp:1 on node vagrantup.dev

Which was pretty annoying, given that I was passing all the parameters that the module was asking for. After two hours of trying in and out, reading documentation, doing magic incantations, etc. I decided to do this to my nodes.pp manifest file:

That miraculously solved the problem, Class inclusion order is important, even if the documentation doesn’t say so. This is as in puppet 2.7.3