GHCi runtime linker found a duplicate definition error

Today I started to play with attoparsec package and tried to write simple parser. I wrote really toy data type like this:

I tried to type:
*SimpleParser> End
in ghci and will get End as I expected. Instead End I got following error:

As we can see in error output GHCi runtime linker found duplicate definition of _hs_bytestring_long_long_uint_hex. Let's see what's wrong bytestring package with:
cabal info bytestring
It will give us output like this:

We can note that there two installed versions of bytestring package. Let's remove old version with:
sudo ghc-pkg unregister --force bytestring-
That's all. After this error with GHCi runtime linker found a duplicate definition error...... will disappear. Hope this blog post will be useful for all who will meet error like this. Happy coding!

Binary tree and some generic tricks with golang

It is quite easy to create binary tree implementation with Go programming language, but it's not clearly for the first view how to make it generic. I think most of us know what is it Binary tree, so I will no explain it here, you can find in the Internet. Let's try to implement binary tree. As you can read about I want to implement generic binary try, so I don't want to make binary tree for some concrete type like int, string and etc... it must generic and be able to work with any data types. It is not a problem to make it generic, because we have {}interface type in Golang. interface{} type means something like any type. That's way you free to do something like this:

Ok, we have interface{} type for any type. Let's create binary tree structure now:

We can see here that binary tree consist from node and pointers to left and right nodes:
Now need to create initialization helper for our binary tree which will return empty binary tree to user:

Ok, now we have binary tree data structure and some code for it's initialization. Let's go to the most interesting part. Now we create Insert function. It will get 1 parameter, some data with interface{} type and insert new node to the our binary tree. How insert works for binary tree: It get new node and tree and check if this tree is empty it creates new first node with this data. If tree is not empty, it compares new node data with current node value and if it is greater it goes recursively to right branch, and if it is smaller it goes to left node in the same way. Implementation of this is following:

Very simple implementation of inserting but it will not work :) If we try to compile our code, we'll get error that: < operator not defined for interface. From the point of logic it is properly behaviour. Really, interface{} means any type, so golang doesn't know what's type will be there and how to compare two values of this type. Actually we can pass and int and string and MyCustomType instead interface{}. How to be with this? If we will look how another programming languages solve this problem, we will find something interesting. Let's look to Haskell for example: There is Ord type class which provides behaviour for <,> and other function for comparing data. Binary tree in Haskell looks like:

Practically it looks almost like golang implementation but with another syntax. We have current node and recursively defined left and right trees. We can see a here, it is like interface{} type in golang. We just can make instance of Ord for Tree and tell Haskell compiler how to use <, > and other operators for tree. There is method to do this in golang like in Haskell. Let's update our binary tree structure and add one function there:

You can see that we had add new field: lessFun which has functional type which gets two arguments with interface{} type and returns bool. How it help us? Before initialization of new binary tree, user will create function with two interface{} and implement comparing of this two arguments there. if first argument smaller than second it will returns true, and false in against way. Usually user knows what's type will be in binary tree and user must know how compare his types. After defining this function need to pass it to New function, so it will be like this:

And now we can write insert function:

Insert function compares node and new node with lessFun function, so it already knows how to compare data with certain types. For example we want to create binary tree for int, it will be:

Here is compare function which gets two arguments with interface{} type and compares it resulting to int type. Than we create new binary tree with New function and pass our compare function to it. And tries to insert some integer numbers to binary tree. It works perfectly, because current binary tree already knows how to compare nodes with lessFun.

If anybody knows method to do better something like this, please let me know in comments.

Full source code of binary tree you can find - here.

Reworked netpool/tcp

Some time ago I wrote blog post about tcp socket acceptor pool which i'm developing for XMPP server and today I fully reworked it. Main data structures like a Listener, Connection still as in previous time excluding really little changes in Listener data structure. I added some API for acceptor pool, fixed some little bugs and fully changed internal structure. I think it post will be useful for newbie Golang developers who wants to play with Golang, TCP and some concurrency. So if you're interesting in it follow this post.

My thoughts about why Haskell is not popular

Despite Haskell programming language is not young language, it has a really great and helpful community, it has big amount of libraries, unfortunately Haskell is not popular programming language. I'm not against this programming language, opposite, haskell's unpopularity makes me sad. I will not write about haskell popularity in industry, I'll try to explain my thoughts about "Why Haskell is not popular" not as Haskell expert, not as professional Haskell developer (i don't get payment for Haskell programming), but from position of usual developer who started to learn/experiment with Haskell some months ago and already don't afraid monads :). I can't So I will try to explain my opinion about Why Haskell is not popular in this post.