Thoughts Heap

A Blog by Roman Gonzalez.-

RSS
Aug
23rd
Tue
permalink

Haskell’s Tuple like structures in Clojure

As you may know by now, there is no equivalent for Haskell’s tuples in Clojure, in order to replicate this behavior, you will need to use vectors of two items. Clojure provides the functions first and second to get the elements out of the tuple vector

Tuples in Haskell are very useful when using the zip function, one way to replicate this behavior in Clojure is using the map function with the vector function and the lists you want to join as parameters.

This can be compared to Haskell code:

The cool thing about the map function in Clojure, is that given its polymorphic nature, it can do the same things that the zipWithN, mapM and map functions do in Haskell.

Pretty neat though confusing at the start.

Aug
15th
Mon
permalink
If you want to do something, you’ll find a way to do it. Otherwise, you’ll find an excuse.
— Jim Rohn
Aug
14th
Sun
permalink
It is better to have 100 functions operate on one data structure than 10 functions on 10 data structures.
— Alan Perlis (read from The Joy Of Clojure)
Jun
27th
Mon
permalink

Puppet - Could not retrieve catalog from remote server

Today I was having issues running puppet (for the first time), I bought an Amazon EC2 micro instance to run the master, and I was working with a vagrant lucid64 box to check that the initial setup for agent nodes was working correctly; sadly I got stuck with an error for a few hours, after doing the SSL certification on the agent. The error was the following:


err: Could not retrieve catalog from remote server: Error 403 on SERVER: Forbidden request: lucid64.hsd1.ca.comcast.net.(XX.XX.XX.XX) access to /catalog/lucid64.hsd1.ca.comcast.net. [find] authenticated  at line 52
notice: Using cached catalog
err: Could not retrieve catalog; skipping run

After a long googling session, I came up with this link that pointed me to the right direction. In order to fix this problem, in the puppet agent (the vagrant instance in this case) you have to set the certname attribute on the puppet.conf file like so:


[main]
certname=lucid64

This way you won’t have any leading dot (“.”) on the hostname, and puppet won’t bite you in the ass, Hope this help you and future me.

Jun
22nd
Wed
permalink

Erlang and Haskell sitting on a Tree

Last night I was reading the paper Haskell for the cloud [pdf], written by the super kick-ass Haskell guru Simon Peyton-Jones.

Two paragraphs got my eye, and I wanted to share them with you

We use the term “cloud” to mean a large number of processors with separate memories that are connected by a network and have independent failure modes. We don’t believe that shared-memory concurrency is appropriate for programming the cloud. This is because an effective programming model must be accompanied by a cost model. In a distributed memory system, the most significant cost, in both energy and time, is data movement. A programmer trying to reduce these costs needs a model in which they are explicit, not one that denies that data movement is even taking place— which is exactly the premise of a simulated shared memory.

In many ways, failure is the defining issue of distributed computation. In a network of hundreds of computers, some of them are likely to fail during the course of an extended computation; if our only recourse were to restart the computation from the beginning, the likelihood of it ever completing would become ever smaller as the system scales up. A programming system for the cloud must therefore be able to tolerate partial failure. Here again, Erlang has a solution that has stood the test of time; we don’t innovate in this area, but adopt Erlang’s solution

This a really interesting paper if you are a functional programming lover.

permalink

vim-ruby slowness fix

Lately whenever I was working on a rails project, I was having such a pain editing the routes file of this specific project, it went super slow on me on editing time. My “patch” fix was to always disable the syntax or set the filetype to something different than ruby to make it go normal speed again.

This came even more unbearable when it was happening on other files, and my crappy “patch” solution came back and bite me in the ass; I started to have bugs because I was not having the syntax coloring on the buffer.

After a lot of STFW I came up with this issue on the ruby-vim project, that was suggesting to use a set foldmethod=manual rather than a set foldmethod=syntax.

That did the work!

Jun
4th
Sat
permalink

QuickFix errrorformat for Haskell

If you are wondering how to get the whole potential of QuickFix with Haskell, here you have a snippet of my vim config.

Have to thank to Martin Norbäck for his post in the GHC mailing list for the errorformat value.

Jan
14th
Fri
permalink

On how Haskell’s Arrows might just be function compositions with a fancier name

Update: The post you are about to read explains just a tiny portion of what Arrows are, pure functions are just one instance of this quite complex classtype. I’m not trying to say that Arrows are just pure functions, I’m saying that in your program, they might be just that. Just to clarify, this post simplify the concept of Arrows — for you the novice reader — to digest it easily. I would recommend reading the comments just to know where to go next.


Of course, I’m not saying there is no logical reason to call function composition “Arrows”, it is just an abstraction and as we should know by now, this abstractions serve as the foundations of high level (sometimes useful) theory, but even so, it is not easy for the average Joe to understand all this mombo-jombo of Category Theory.

I really got the idea not so long ago, and not by studying Category Theory papers1, but by reading this webpost about prime number checking, I saw that the (really smart) author was using the Arrow operators (»>), (&&&), (***) as composition of functions and suddenly it snapped my head around.

So… if you are familiar with how function composition works, this should be of no surprise to you:

>> let plus5times3 = (*3) . (+5)

>> :t plus5time3
>> plus5time3 :: Integer -> Integer

But what happens if we rename the (.) high-order function with (»>) and instead of applying from right to left, we do it (the more natural way) from left to right:

>> :m + Control.Arrow 
>> :m + Data.Char

>> let plus5times3A = (+5) >>> (*3)

>> :t plus5times3A
>> plus5times3A :: Integer -> Integer

I have declared the exact same thing, I just changed the order of the partially applied functions, and renamed the (.) function to something more fancier (»>).

So what? Is this what Arrows is all about? I would say almost, you see you can do more powerful things with the Arrow abstraction.

First, let’s imagine we have a split function, that converts an input value into a tuple with the value repeated twice

>> let split x = (x, x)

Now, we could use some methods of the Control.Arrow package, to start doing alterations on just a position of a tuple

>> first length ([1,2,3], "3 numbers")
>> (3, "3 numbers")

>> second (map toUpper) ([1,2,3], "3 numbers")
>> ([1,2,3], "3 NUMBERS")

>> let plus5AndOriginal  = split >>> first (+5)
>> let originalAndTimes3 = split >>> second (*3)

>> :t plus5AndOriginal
>> plus5AndOriginal :: Integer -> (Integer, Integer)

>> :t originalAndTimes3
>> originalAndTimes2 :: Integer -> (Integer, Integer)

>> plus5AndOriginal 5
>> (10, 5)

>> originalAndTimes3 5
>> (5, 15)

Ok, that looks cool and all, but what is this useful for? Let’s create some more functions and show you.

>> let plus5AndTimes3 = split >>> first (+5) >>> second (*3)
>> plus5AndTimes3 7
>> (12, 21)

With this notation, we just don’t have function composition, but we have functions that can get one value, and return multiple applications of functions to the same value we passed as a parameter, e.g plus5AndTimes3 is returning the application of two functions (+5) and (*3) to the input 7. We go with the famous drawing to give more insights on what is going on:

       (first)  ---- (+5) --> 12
       |
7 ---(split)
       |
       (second) ---- (*2) --> 21

This pattern is so common, we have a function (&&&) that does the split, first and second for us

>> let plus5AndTimes3' = (+5) &&& (*3)

This is translated to “we apply the function (+5) AND the function (*3) to the given input”, is named (&&&) after this AND statement.

           |---- 12
           |
7 ---(+5) &&& (*3)
           |
           |---- 21

If we want to apply functions to the first value, we use the first function

>> plus5AndTimes3' >>> first (`mod` 2) $ 7
>> (0, 21)


           |----> 12 --> (first (`mod` 2)) --> 0
           |
7 ---(+5) &&& (*2)
           |
           |---- 21

If we want to apply functions to the second value, we use the second function

>> plus5AndTimes3 >>> second (`div` 2) $ 7
>> (12, 10)


            |----> 12 --> 
            |
7 --- (+5) &&& (*2)
            |
            |---- 14  --> (second (`div` 2)) --> 10

What if we want to apply functions to both values at the same time? We could use the first and second functions consecutively, however there is a better function for that called (***), and this is how it would be used:

>> plus5AndTimes3 >>> first (`mod` 2) >>> second (`div` 2) $ 7
>> (0, 10)
>> plus5AndTimes3 >>> (`mod` 2) *** (`div` 2) $ 7
>> (0, 10)

At this point, you should see that there is no dark magic in the Arrow interface, once you realize is just function compositions, this operators looses the weird property and become something more natural.

One last bit, this works pretty good when you want to have multiple values at the same time from the same source, but how you go and use those values together in a function?, What if we use a “joinA” function?

>> let joinA fn (a, b) = fn a b
>> plus5AndTimes3 >>> joinA (+) $ 7
>> 33

             |------ 12 -----|
             |               |
7 --- (plus5AndTimes2)   joinA (+) -> 33
             |               |
             |------ 21 -----|

The real world use cases of this are beyond my knowledge, but I think it is a matter of time before I can start thinking on ways to solve problems using this approach, at least I have the foundations with all the “Category Theory” stripped out, although I’m pretty sure reading the theory would get me pretty far in the future.

This works because functions are an instance of the Arrow typeclass, I can imagine there could be other implementations using the same typeclass that would do distributed programming on each arrow, or fancier stuff like that, and all this on the background because we are using an interface. Neat stuff.


1 OK! I studied a little bit of it, but didn’t understand squat on the time of reading, and it didn’t get me that far by itself

Dec
14th
Tue
permalink
2nd Vancouver’s Haskell Hack Night… Just awesome.

2nd Vancouver’s Haskell Hack Night… Just awesome.

Dec
9th
Thu
permalink
elchacoveloz:


Obi-Wan never told you about your father…




It would had been so awesome if that would actually happened in the movie

elchacoveloz:

Obi-Wan never told you about your father…

It would had been so awesome if that would actually happened in the movie

(Source: elchacoveloz, via darkana)