Thoughts Heap

A Blog by Roman Gonzalez.-

RSS
Jun
22nd
Wed
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)

Oct
27th
Wed
permalink

We are unable to upload this Certificate file because it is invalid. Please check the file and try again.

At this moment, you are probably being victim of Apple’s Provisioning Portal mysterious error messages.

I know.

  • you are frustrated
  • you are following each single step in a really detailed fashion
  • you have repeated the whole thing at least 4 times and still getting this stupid cryptic error message

Time for google to solve it right? Well, fear no more, I have the one and true answer for you, it’s only 3 simple steps.

  1. Quit Google Chrome or Firefox (whatever you are using)
  2. Open Safari
  3. Try again

You are welcome.

By now you are thinking:

“Gee, developing for Apple was not as a good idea as I thought it was”

isn’t it?

Thanks to @mhorbul and his tweet

Mar
31st
Wed
permalink

Default mutable values in Python functions are WACK!

If you use mutable default values on functions, this will keep it state in recurrent calls:


def myAppend(x, L=[]):
    L.append(x)
    return L

#=============

>>> myAppend(1)
=> [1]
>>> myAppend(2)
=> [1, 2]

That’s seriously messed up o.O

Mar
29th
Mon
permalink

Use form_for from Rails’ console

As a curious developer, I always like to inspect an objects guts on the console, that way I can now as much as I can from the class without reading the documentation. It was the time for the rails form_for view helper. I had no idea how to access that from the terminal whatsoever, so I googled a bit and I found nice tidbits that got me going pretty quickly.

However when you try to use a method like form_for, a NoMethodError is being thrown because you are trying to call url_for for on a nil instance. This is because the default console helper doesn’t have any controller assigned. This can be easily fixed:


helper.controller = ActionController::Integration::Session.new

However, when we try again the form_for method we get a new error.


NoMethodError: undefined method `protect_against_forgery?' for #<ActionController::Integration::Session:0x10608a460>

It seems that for some reason, the ActionController integration session doesn’t support the forgery protection, because this is not a biggie for what we need we are just going to stub the method, and go our own way.


s = ActionController::Integration::Session.new
class << s; def protect_against_forgery?; false; end; end
helper.controller = ActionController::Integration::Session.new

At this point we thought we were going get away with it… well no, it seems it never stops.


NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.<<
    from /Users/roman/Sites/git/work/noomii/vendor/rails/actionpack/lib/action_view/helpers/text_helper.rb:32:in `concat'
    from /Users/roman/Sites/git/work/noomii/vendor/rails/actionpack/lib/action_view/helpers/form_helper.rb:281:in `form_for_without_haml'
    from /Library/Ruby/Gems/1.8/gems/haml-2.2.9/lib/haml/helpers/action_view_mods.rb:170:in `form_for'
    from (irb):16

The default helper object doesn’t have an initialized output buffer either!… luckily is easy to skip this one as well.


s = ActionController::Integration::Session.new
class << s; def protect_against_forgery?; false; end; end
helper.controller = ActionController::Integration::Session.new
helper.output_buffer = ''
f = nil
u = User.new
helper.form_for { |_f| f = _f }
f
>> #<Forms::ApplicationFormBuilder:0x105d7d330 @proc=#<Proc:0x0000000105d80648@(irb):16gt;

Oh the nice smell of victory!

Nov
20th
Fri
permalink
Now we are talking&#8230;

Now we are talking…

Nov
18th
Wed
permalink

guillee:

I see your puny garbage plate and I raise you a typical venezuelan street vendor’s burger. This will either kill you or make you immortal. Ingredients (in order):

  • Mayo, ketchup & mustard
  • Onions
  • Cabbage
  • Potato chips
  • More mayo, ketchup & mustard
  • Avocado
  • Tomato
  • Hamburger patty
  • Chorizo
  • Chicken
  • Bacon & eggs
  • Cheese

(Warning: DO NOT eat this.)

Now that IS a HAMBURGER :-D