Monday, March 16, 2015

Pike, A Hidden Beauty

Last few days I was getting myself familiar with an interesting project named sTeam. One of the things that makes it interesting is that its written in pike. Pike is the programming language that is used to write it.

Don't worry if you haven't heard of pike. Many people have not. Its a relatively undescovered language. Average google search along the lines of "Pike tutorials" would not get you much other than this official beginner tutorial. The stackoverflow tag 'pike' has mere 8 subscribers. There is only the single official intrepretter and not many IDEs except for an emacs mode and an eclipse plugin. Text editor I use, gedit, by default does not know how to highlight syntax of a .pike file.

But installing pike and trying it out makes you wonder.. why? why don't we use this more.

Pike is very attractive just like an unseen hidden forrest flower.

In my short career so far I have been lucky enough to use many languages. Most of my assignments in the university are to be done using C or Java. My final year project is done with Diaspora, which is ruby. I coded python for melange in Google summer of code. During an internship with IroneOne I worked mostly on an iOS app which is objective C. And my first task in my first job was building a nodejs application, which is Javascript. While going through the tutorial, I felt pike is made of good parts of many of those languages.

Duck Typing


There are both pros and cons of Duck Typing. While coding python, ruby and javascript code I felt great and previlaged to be able to use dynamic data typing. But it wasn't unclear to me, that some of the code that takes advantage of Duck typing could lead to problems. Specially if they are not documented or the documentation is not reffered. Sometimes it took some of my time to understand poorly documented javascript APIs.

On the other hand, sometimes, In early days as an undergraduate I felt frustrated to not being able to return couple of integers and a string in a single array from a java function.

Pike has a simple yet effective solution. In addition to its three basic types, int float and string it has another queer little type, mixed. You can specify a variable as mixed and and store any value irrespective of whether it is an int float string or even a complex type.

Pike v7.8 release 866 running Hilfe v3.5 (Incremental Pike Frontend)

> int a;
> a = 7;
(1) Result: 7
> a = "aruna"
>> ;
Compiler Error: 3: Bad type in assignment.
Compiler Error: 3: Expected: int.
Compiler Error: 3: Got     : string(0..255).

> mixed b;  // using mixed type
> b = 21;
(2) Result: 21
> b = "herath";
(3) Result: "herath"


Arrays declared to hold mixed types, or not declared to hold any type, can contain values of mixed types. A function declared to return mixed type can return anything.

Syntax


I feel syntax in pike manages, again to get the best, most elagant bits out of the languages I am familiar with. It uses c style parantheses to identify scopes of funtions conditional and loop statesments. In my view much better than pythons awkward tabs/spaces.

Outshining c it has a simple 'dictionary' data structure known as mappings, with elegant syntax.

mapping(string:string) batsmen = (["Sangakkara": "Sri Lanka", "Maxwell":"Australia", "Kohli": "India"])

Arrays,

array(string) cricketers = ({"McCullum", "De Villiars", "Malinga", "O'Brien"})

In my view pike syntax is pleasing to the eye...

Interpreter


You probably have guessed from whats mentioned so far that pike is interpreted. Yes, Pike comes with an official interpreter named Hilfe.
A real time interpreter is something I find very useful to learn a language. It is of great value when developing as well. You can just enter few lines in the interpreter and see if you got the syntax or the logic correct. It's much easier and quicker than writing a program and compiling and testing or looking through documentation.

Further pike does not have many external libraries, but its distributed with many modules so you can get most of your work done with pike itself.

In Top Gear consumer advice style, pike would not treat you like a kid and hold you back from doing stuff freely, nor would it consider you a saint and over trust you with everything.

It probably is not a good idea to use pike in your next big project, for the very little support you'd get, but definitely worth knowing about and trying it out in one of those pet projects.