code formating

Friday, July 18, 2014

Erlang Maps

I am on the process of programming the bitcoin protocol on Erlang. Erlang is great for pattern matching binary messages and the OTP gen_server offers an incredibly powerful way to generate state machines that can talk to sockets.

When decoding different protocol messages, there are a number of variable data attributes that come out. So programming the protocol, I chose tuples as I was starting to decode the first type of message. Soon I realised when coding the second message type, that the decoded values where entirely different and pattern matching a variable number of parameter tuple, would be difficult.

So I thought of proplists. Now this does gives us more flexibility on the list of parameters but we lose the one line pattern matching ability. I regard this last point as important because when we are deep into the state machine, the current state and the message type are key to decide what to do. Pattern matching these it's very interesting from my of view.
It was then when I stumbled upon Maps. Variable length flexibility and pattern matching ability. Just what I needed for this particular purpose.

An example of a function that matches first the decoded message and then the state of the gen_server is as follows:

f(DecodedMessage = #{command := version}, State = #{state := <<"init">>},Socket)->
    ...

Naturally, the decoded message and also the state have more keys than the ones that I am matching in the example, but Erlang allows me to match the ones that I am interested on, retaining all the rest.

Simple and beautiful!

Programatically walk a CATIA tree in .NET

One of the most basic tasks when automating anything in CATIA, it's usually to walk the whole tree and apply some changes to objects in ...