28 February 2009

Ocaml: using Big_int, operator overloading and functors for dummies

I'm working on a functional programming project. I have encountered a few difficulties, so I will post here a few things that took me quite a while (and a lot of luck) to find. Especially operator overloading, where Google lead me only to endless discussions about wether it should exist or not and why it doesn't, when it actually does.

I will write a code snippet that will allow us to use integers and Big_ints the exact same way, for instance doubling them using one function named "twice". (In other words, putting two and two together with 4 modules and a functor - can something be more fun?)

First, let's get to the Big_ints:


    1 #load "nums.cma";;


    2 open Big_int;;




So far so good. Now, we'll declare two modules that can add numbers, one for integers, one for the Big_ints, but first, they'll need a signature:

    4 module type Add_sig =


    5   sig


    6     type t;;


    7     val (+): t -> t -> t


    8   end;;




Now, the modules. Notice how cool functional programming is, you can assign functions like other values:

   10 module Int_modl=


   11   struct


   12    type t = int


   13    let (+) = (+);;


   14  end;;


   15 


   16 module BigInt_modl =


   17   struct


   18     type t = big_int


   19     let (+) = add_big_int;;


   20  end;;



The "let (+) = (+)" looks stupid, but is required.

Now we'll write a functor to transform these two modules in two new modules that have a richer functionality (okay, "twice" is not "rich", but that's just an example and I hope you can see the big picture), that we will implement only once, though int and Big_int are two completely different data types and have completely different additions ("+" and "add_big_int") - again, notice how cool functional programming is.

   22 module Add =


   23  functor (Modl : Add_sig) ->


   24   struct


   25     type t = Modl.t


   26     let (+) = Modl.(+)


   27     let twice t = t + t;;


   28   end;;




Nice functor. Let's use it to create the previously mentioned modules and we're almost there:

   30 module Ints = Add(Int_modl);;


   31 module Bigs = Add(BigInt_modl);;




Okay, time to see if that works:

33 Ints.twice 2;;


34 int_of_big_int (Bigs.twice (big_int_of_int 2));;



We have a bit of hassle with creating and printing the Big_ints (thus the longer code), but in the end, we defined one function "twice" that uses the same operator "+" to perform technically completely different operations (with an emphasis on technically - you could use this technique to make "+" mean subtraction or multiplication for the integers and that would be nasty).

Finally, the entire code, ready to copy, paste and... run!

#load "nums.cma";;


open Big_int;;


 


module type Add_sig =


  sig


    type t;;


    val (+): t -> t -> t


  end;;


 


module Int_modl=


  struct


   type t = int


   let (+) = (+);;


 end;;


 


module BigInt_modl =


  struct


    type t = big_int


    let (+) = add_big_int;;


 end;;


 


 module Add =


 functor (Modl : Add_sig) ->


  struct


    type t = Modl.t


    let (+) = Modl.(+)


    let twice t = t + t;;


  end;;


 


module Ints = Add(Int_modl);;


module Bigs = Add(BigInt_modl);;


 


Ints.twice 2;;


int_of_big_int (Bigs.twice (big_int_of_int 2));;


18 February 2009

On my wishlist: "Fashion Geek" by Diana Eng

Diana Eng was a Project Runway contestant (that's means she's a fashion designer) famous (and adored by me) for involving geek/tech/etc. stuff in her creations. She won top honors at Yahoo! Hack day for a thingy that takes pictures as you walk and blogs them, and now she wrote a book with DIY projects! (More info here.)

I just *have* to have it.

16 February 2009

I knew I forgot to blog about something

I knew I forgot to blog about something: I passed the70-536 exam!

I got 841 points and 700 was the passing score, so I was pretty well prepared. And they had good coffee at the center.

What's next? The asp.net one, as I have a bit of experience in that. And then maybe WPF, as it's the closest thing to Silverlight - or is there a Silverlight certificate coming up? I don't know about any so far.

15 February 2009

Geek does chick stuff

I'm getting married soon, my exams are over and I should be writing my funk project. There couldn't be a better day for chick stuff.

I started with a bit of cardio. I had Carmen Electra's Striptease Aerobic vol. 2 lying around for months, so I decided to try it.


Well, it wasn't what I expected. It had no vulgarity (no, no disappointment here), but not much fitness either. The girls were walking around and caressing their tights, but I wished for some more dance, more jumping, more challenge. (Though the clothes were great - if only the crew put as much effort to the choreography as to the styling...) Well, judge for yourself:



So I ended up doing what I usually do for cardio: put Bomfunk MC's CD loud and improvised. And that was great.

Another thing lying around (but only for weeks) was Philips' Satinelle Premium:

In one word: ouch. This was my first try to epilation with that kind of hardware and the pain was a bit hard to bear at first, but I got used to it pretty quickly. (I guess I should have RTFMed and kept the cooling thingy for 4 hours in the freezer and not 20 minutes in the fidge. I'll try that next time.) Plus, the powerfulness of that thing made the whole process a bit fun. Still, my skin doesn't look as smooth and shiny as Carmen's. And I'm not as fit. Go figure.

[Picture credit, credit]

14 February 2009

My biggest dream is to marry well (and that's not pathetic)

My biggest dream is to marry well. Is it. And some people find it pathetic. And I'm gonna prove it's not.

Just marry? And that's it?

People ask when I speak my mind. The thing is, it's not just marry and it's never it.

It's never it because marriage is forever, for better or for worse. I don't want to just get into it, but to stay in it. For life.

It's not just marry because it's about love everyday, it's about making a great commitment, it's about a lot more than just going through a certain ceremony. Love is easy on Valentine's day. It's easy when things are good. It's harder when you have to sacrifice a dream for it or the person you loved hurt you. Still, I wanna do it without exceptions: love that man every single day, even if he lies to me, cheats on me, put something above me... Of course, I want my husband to love me that way as well.

In the TV shows I watch, the protagonists keep on changing partners - otherwise, not much would happen. But they have someone predestined for them in a way that the viewers know it from the start. They might end up together after seven seasons of stupidity (or soon but drift apart and back), but they will, and when they do, everyone knows this relationship is not like the others - it's forever. That's what I want - to marry my Mr Right (I'll pass on the seven seasons of stupidity).

Attentive readers will ask:

But you're already engaged!

That's right, I am. Therefore, my biggest dream is to marry my fiance. There - I found a way to say it that sounds dumb even to me! What's a fiance for if not to marry him? But, if you read that post attentively, you should understand how much this means to me.

01 February 2009

Flacs are coming!

I was just browsing 7digital and saw this:


That's an album in flac! Flas as in:

...Free Lossless Audio Codec.

Personally, I'm pretty much deaf and 256k mp3 are perfect for me. But many of my beloved ones have been blessed with a good hearing, some to the point of not tolerating any quality loss. So I'm happy for them. And I'm happy to see the market going towards better quality. And mostly, I'm happy that DRMs got their lower frequencies kicked.

An extensive search showed that this is the only flac album on 7digital so far. So that's just a start, let's see how things go, but I really like the way they're going so far.

A short story of CD cases

This is a short story of CD cases.

First, they looked like that:



Then, came double ones:
Then the singles ones became twice thinner:



And then twice bigger again, but with rounded corners:



As promised, that was short. Here's more if you're really interested.

[Picture credit, credit, credit, credit]