It Seemed Like a Good Idea at the Time Coding, Mostly

24Aug/096

The Magic Dot

So, reading about functors the other day, I noticed something interesting. Specifically, I noticed that in the case of functions,

fmap

is equivalent to the composition operation. I think

fmap

is possibly the coolest function in all of Haskell, so it annoys me when it requires six characters just to use it as an infix operator.

So I present what is, character-for-character, the coolest Haskell trick I have seen so far:

import Prelude hiding ((.))
import Control.Monad.Instances
a . b = a `fmap` b
infixr 9 .

And now, all sorts of fun little tricks are possible, like replacing

"map succ $ [0..9]"

with

"succ . [0..9]"

, and replacing

"getLine >>= return . (+1) . read"

with

"(+1) . read . getLine"

And of course, the same function composition magic still works like always.  Maybe it's just me, but something this simple, elegant, and fun to use just makes me happy inside.

But finally, I have to ask: is there any way of specifically declaring that a module *replaces* the standard Prelude (or even better, individual elements of it)? It would be really nice to just type 

import Prelude.MagicDot

and not have to also bother with adding 

import Prelude hiding ((.))

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
6Aug/090

Useful Shell Trick

Sometimes, when doing a bunch of data munging in the shell, I want to pass the contents of a file through a shell pipeline, and then directly out into the file again. Unfortunately, doing this the naive way will obliterate the file's contents.

So a while back, I saw a neat little utility called "sponge" which simply buffered data until it ended, and then output it. While useful, this wasn't a standard Unix tool, so I couldn't very well rely on having it available.

Well today, after literally minutes of searching, I have found a command that seems to work just as well:

tail -n +0

It outputs all lines in the input, beginning with line zero. Due, I suppose, to the way tail is implemented, it buffers all input before outputting anything.

Not exactly groundbreaking or anything, but a useful little trick to know when playing around in the shell.

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Filed under: Uncategorized No Comments