Dive into ErLang

I recently had a chance to learn Erlang. In the beginning, it was quite hard to find a good book or tutorial that gave a clear idea of the language and the best practices. Hoping this blog can help someone who wants to get started.

As I began, it started to strike me that modern languages like Go, Ruby, Javascript have parts which have some similarity with Erlang. The parts include concurrency aspects w.r.t. passing messages using channels in Golang, the way functions  return results of last executed expression in Ruby and first class functions in Golang/Javascript.

Erlang’s history revolves around telecom industry. It has been known for concurrency using light-weight processes, fault-tolerance, hot loading of code in production environments etc. The Open Telecom Platform (OTP) is another key aspect of Erlang which provides the framework for distributed computing and all other aspects mentioned above.

Some key points to keep in mind,

  • Values are immutable
  • Assignment operator (=) and functions work based on pattern matching
  • No iterative statements like for, while, do..while.. etc. recursive functions serve the purpose
  • Lists, Tuples (records – a hack using tuples) are very important data structures
  • If, Case.. Of.. are the conditional blocks
  • Guards are additional pattern matching clauses used with functions and Case.. Of..
  • Every expression should return a value and last expression in a function automatically returns the result
  • Functions are first class citizens
  • Usage of punctuations ‘; , .’ etc. (one can relate this to indentation requirements in python)

Lets gets started with some code samples,

% Execute this from erlang shell. erl is the command
> N = 10.
10
%% The above statement compares N with 10 and binds N with 10 if its unbound. If its already bound with some other value, exception is thrown.

%% Tuple
> Point = {5, 6}
{5,6}

%% List
> L = [2, 5, 6, 7].
[2, 5, 6, 7]

%% Extracting Head and Tail from List is key for List processing/transformations
> [H|T] = [2, 5, 6, 7].
>H.
2
>T.
[5, 6, 7]

%% List comprehensions
> Even = [E || E <- L, E rem 2 =:= 0].
[2,6]

Lets take a look at the simple functions,

helloworld () -> hello_world.

The simple function will return hello_world, a constant string – atom. Lets have a look at a recursive function,

%% Factorial of N
factorial (N) when N =:= 0 -> 1;
factorial (N) -> N * factorial(N-1).

%% Tail recursion
tail_factorial (N) -> tail_factorial(N, 1).
tail_factorial (0, Acc) -> Acc;
tail_factorial (N, Acc) -> tail_factorial (N-1, Acc * N).

The factorial functions demonstrate how Erlang does pattern matching on function parameters, usage of  Guard (‘when’ clause), punctuations. We could have also written the statement as ‘factorial(0) -> 1;’ .

The second version tail_factorial demonstrates the optimized version using tail recursion to simulate the iterative method. In this method Erlang would remove the previous stack frames using Last Call Optimization (LCO). It is important to understand both techniques as recursion is used quite extensively.

Erlang has the following data types – atom, number, boolean (based on atom), strings and binary data. atom’s occupy more space and its better to using binary data type for strings of larger sizes.

Other builtin data structures are queues, ordsets, sets, gb_trees . Error, Throw, Exit, Try.. Of .. Catch statements provide the exception handling capabilities.

The most interesting part of the language is about spawning light weight processes and passing messages between them,

– module (dolphin-server).
%% API
– export ([dolphin_handler/0]).
dolphin_handler() ->
  receive
         do_a_flip ->
               io:format (“How about no ? ~n”);
          fish ->
io:format (“So long and thanks for the fish! ~n”);
          _ ->
               io:format (“we’re smarter than you humans 😉 ~n”);
     end,
     dolphin_handler().
%% From shell, compile dolphin-server module

>c(‘dolphin-server’).

%% Spawns a new process
> Dolphin = spawn (‘dolphin-server’, dolphin_handler, []).

<0.124.0> %% process id of the newly spawned process

%% Now start passing messages!!

> Dolphin ! fish.
So long and thanks for the fish!
>Dolphin ! “blah blah”
we’re smarter than you humans 😉
This demonstrates the compilation of dolphin-server module, spawning of the dolphin-server as a separate process and communicating to the process using messages.
Further to passing messages, erlang provides capabilities around linking multiple processes, passing references for reliability, policies around restarting processes as a group/single etc.

Open Telecom Platform (OTP)

While it is interesting to write all the boiler plate code for spawning and communicating with multiple processes, Erlang makes it easy for by providing all these generic pieces of functionality as behaviour modules in OTP framework.
OTP design principles define how to structure Erlang code in the form of processes, modules and directories.
The following are the behavior modules,
  • gen_server – for implementing server side piece in client/server
  • supervisor – for implementing a supervisor in a supervisor tree. It takes care of the details regarding restarting of processes, fault-tolerance etc.
  • gen_event – for implementing event handling functionality
  • gen_statem – for implementing state machines

https://learnyousomeerlang.com/contents is one of the best online books for understanding how to use Erlang,  best practices while building an application which is production ready.  Its good to take pauses and move with book as its quite exhaustive, but wonderfully written 🙂

Advertisements

Polyglot – Learn, Share, Collaborate – Hackfest 2016!!

Polyglot project aims to provide the summary of various choices available for each of the components while developing a web application. It details their strengths so that one can easily choose the right component to build a great solution.

hackfest_2016

I had gone through several blogs, stack-overflow/quora answers to choose a proper database, programming language, web-framework etc. to build a solution in the past. Most of them were out-dated and I had to keep track of the date for each of the posts.

So for this HACKFEST 2016, wondered how would it be if we could share the learning through a wiki and collaboratively maintain an up-to-date content. I had a hunch that this might be a problem that many would have faced and would be good to solve.

It starts with Questions/Concerns one should keep in mind before starting a project. It goes deep enough, providing a Syntax Cheat Sheet so that one can use it to directly shift the mind from one language to another by going through a single page. It also lists various WebFrameWorks and several Programming Language choices. Am a big fan of Rails and GoLang. The idea is a work in progress..

The wiki is available on github!! It would be nice to collaborate and make it better 🙂

 

C to C++ tour

I was requested during the beginning of this year to give a crash course on C++ for the developers with C background in our company. A while back, sankar gave an introduction to go language using http://tour.golang.org/ . Fascinated by the interface of gotour, I wanted to give the C to C++ tour using a similar interface but needed it in a quick time.

I discussed it with sankar and he came up with https://github.com/psankar/kuvalai/. He masters in pulling people to learning something new 😉 So I quickly learnt a bit of Go and contributed to kuvalai. It was taking a while to get it done, so we discussed and decided to hack up the go-tour. Made it to work with c++!!

Image

Readme.txt – explains howto apply the go tour patches and get it running.

All the programs and the article is now available at https://github.com/chenthillrulz/cpp-tour 🙂 I wanted to put this up on webserver so that it can benefit others, esp. beginners to c++ and students. But since I don’t have any webspace at the moment, thats going to take time 😉

It was really challenging to construct simple, connected, practical examples for demonstrating the features. I wanted this tour to go simply like a movie. I did not know that I would enjoy so much doing this stuff 🙂 Got some happy, encouraging feedback from my peers after the training sessions. Perhaps I should thank my manager, Pradeep for persuading me to do this stuff. And my team, some of whom are still pushing me for the final session!!

Have conducted four sessions and the last one would cover advanced concepts such as traits, functors, template specialization, c++-11 features etc. The last session is taking time as I dont have practical experience on using traits, but still want to get some practical examples 🙂  Working on it!!

The descriptions in the doc. many require some polishing. It has about 42 sections at this point. And as always patches are welcome!!!

writing a blog after quite some time, refreshing 🙂

Exchange WebServices Offline AddressBook

It is now possible to download GAL contents for offline usage through exchange web services in Evolution. Offline GAL is termed as Offline Address-book (OAB) in Exchange WebServices and it may contain one or more Offline Address-lists (OAL).

The check-box, ‘Cache offline address-book’ would be sensitive if the OAB url is discovered using the AutoDiscover service from the previous page (‘Receiving’). On clicking the fetch buton, the available offline address-lists would be displayed in the combo box.  The user can select the address-list which he is interested in.

If the GAL is not selected for offline usage.  A GAL folder would be created in Contacts component and would be used just for auto-completion when Evolution is online.

To change an offline address-list, one just needs to go back to ‘Receiving page’ in preference and select a different address-list. This would remove the old OAL folder and its contents and create a new OAL folder.  At the moment, evolution-ews supports caching only one OAL, though it is very easy to extend it to support many. I feel it would sufficient to have one at the moment, but if a need arises, the plugin can be extended to create new OAL folders on demand.

If an address-list is chosen for offline usage, one CAN auto-complete while the caching is in progress!! All operations are asynchronous and cancellable.

Internals

OAL’s would be listed in the link  <OABUrl> + ‘oab.xml’. They would be available as compressed files, compressed using LZXD format.  There are three versions of OAB ‘2, 3, 4’ and Evolution supports downloading, version 4 OAB Full Details file.

LZXD

To put it simply, lzxd extends the lzx decompression format adding support for differential updates. I have picked up the lzx decompression code from libmspack and modified it to make it decompress the lzxd file. The support for differential update is yet to added.

OAB

The format for the uncompressed OAB Version 4 file is available at http://msdn.microsoft.com/en-us/library/cc463914%28v=EXCHG.80%29.aspx . Evolution-ews has the decoder which decodes the oab file, converts to EContact and then we  populates the address-book sqlitedb in chunks of 1000 contacts.

And now the contents are available for offline use 🙂

CamelUrl props

“oab_offline” – says if oab is marked for offline.

“oal_selected” – contains the selected “oal id:oal name”

“oaburl” – contains the oab url

ESource props

“hosturl” – contains the host url, used for auto-completion.

“oaburl” – contains the oab url.

“oalid” – id of the OAL.

“gal” – a boolean that it indicates that its gal folder.

self introspection and ‘My experiments with truth’

There has been some issues  hindering my work for quite sometime.  With the help of teachings/realizations from oneness and ‘http://jkrishnamurti.org/index.php‘ I was able to constantly get over it. But suddenly I was confused again whether the current direction would lead anywhere and remained in it hoping I would get someone to guide.

I used to get guidance from Harish Krishnaswamy w.r.t directions related to my work and later after he left, used to seek my mother’s guiadance though she has no real idea of software industry. She usually gets answers through her prayers and they have served flawlessly when I outght to make some critical decisions. Though I would just get to know only ‘yes or no’ from her. Sometimes my reasoning will coincide with her answer and many times it wont. Later in time I would be surprised to see the reason behind her answer. At times it would be comical to look back on my reasoning  foolishness. This has constantly led me to renounce my thoughts and take no credit for my action. [i have some discussions with other people, but i will omit them as they are co-workers atm]

Many a times, there has been a quite a lot of youtube videos and forwarded messages that came to rescue.  I started seeking guidance from God with some reasoning rather than ‘yes’ or ‘no’. While I waited for a while lingering on the confusion with no hope that the hindering issues will be solved, suddenly I came across Mahatma Gandhi’s autobiography, ‘My experiments with truth’.

Reading this autobiography just reinforced me to seek the Truth again. The fear of uncertainty on the unsolved and long standing issues was certainly confusing whether I was on the right path and at right place. But reading and digesting the life of Gandhi the confusion has vanished 🙂 Some quotes which I would like to remember and re-inforce,

‘ I have found by experience that man makes his plans to be often upset by God, but, at the same time where the ultimate goal is the search of truth, no matter how a man’s plans are frustrated, the issue is never injurious and often better than anticipated’

This was the answer for me 🙂

There were certain other quotes which I liked that added as a knowledge and making me extremely humble,

‘That the good of the individual is contained in the good of all. That a lawyer’s work has the same value as the barber’s inasmuch as all have the same right of earning their livehood from their work’

‘Hate the sin and not the sinner’ This which I also realized from JKrishnamurthy’s talk where he says ‘Knowledge in relationship is destructive’. On my introspection into these leads to say, in trying to act intelligently we may think that attaching a character to a person is useful. But overtime am realizing through experiences that a conscious act using the knowledge would cause differences in dealing with people and move one away from humility, truth.  More of these characterization in memory leads to unwanted burden of conversation between thoughts in mind, which becomes a real struggle later.

I also liked Gandhi’s view on parenting Part III, chapter 6, spirit of service (Page:191).  His ways to impart spiritual and litreture teachings for children at  Part IV, chapter 32, As Schoolmaster to chapter 34, Training of the spirit (page:311-318, in my edition).

I also realized the power of vow from this reading. I have taken a vow this Sunday April 17th and it is working wonderfully well though there were some tests. I will wait for couple of months more before I speak about the vow.  I would probably read Bhagvad Gita as well.

Lightening up Evolution with Exchange Web Services

Off-late we have been working quite aggressively on improving our exchange connectivity using Exchange Web Services .  Some evolution hackers sat together during GUADEC 2010 and discussed on the focus areas which our community users as well as corporates would be interested in. Exchange Web Services was on top of the list and David Woodhouse kick-started the work at the same time!  The development went on in David’s repo – http://git.infradead.org/evolution-ews.git and you can watch out the progress there..

 

 

 

 

 

 

 

 

The festival season has started for evolution exchange and we have evolution showing the folders, mails and meetings using Exchange Web Services.  We currently just have the read-only support for mailer and calendar at the moment and we are working towards providing a complete support for calendar, mails and contacts.

Its always very nice to thanks all the contributors. Thanks to David Woodhouse who kick-started it, Michael meeks he is always there :), Johnny, Bharath Acharya, Akhil  Laddha, Chen, Fridrich who has been constantly getting it to compile on Windows…

There are more developers getting involved now and thanks to the organizations for supporting the development!! We are looking forward to deliver the package by the beginning of May 2011 for all the users.. One would be able to use EWS connector with Evolution version 2.32 onwards..

Evolution 2.30 released!!

This is one of the memorable releases of evolution considering the amount of work which has gone into this release.  We could consider this as a preview for GNOME 3.0. To provide a snapshot of the major changes in this release,

  • Diligent UI re-architecture to give farewell to bonobo.
  • Improved imap implementation with live view updates, non-blocking operations and IDLE support, IMAPX.
  • DBus transport replacing bonobo for address-book and calendar.
  • GNOME 3.0 cleanup tasks. Gets rid of LibGnome, LibGnomeUi, LibArtLgpl, LibGLade.
  • Evolution-mapi with push mail support and reduced memory footprint.
  • Contacts map  plugin.

Getting rid of bonobo from evolution was not only a cleanup work, but it enables evolution to shed off lot of weight to get flying.  This has provided a lot of positive side-effects,  one of the best which needs to be quoted is the startup speed of preference dialog. Evolution is now sleak and fast. If one has used the previous versions of evolution,  no one needs to explain for appreciating the work. All credits goes to Matthew Barnes who has done an extra-ordinary job here.

We also have the new imap implementation IMAPX, which provides substantial performance improvements over the old imap provider making all the operations non-blocking in nature.

Credits to Ross Burton for replacing  bonobo with dbus transport in EDS.  This would pave way for us to fix some out-standing issues which had been put aside waiting for this change to dbus.  There is also some work-in-progress for making this extremely stable in threaded environment.

We have listed the probable tasks which would be taken up for the future releases at http://live.gnome.org/Evolution/PlanningFuture.  We would be picking up some tasks from there for Evolution 3.0 depending on the effort available. If you have suggestions  feel free to get in touch with us to get it added..

Thanks to Akhil Laddha (bug master), Milan Crha (patch master), Johnny Jacob, Bharath Acharya and all others (many to list everyone here.. ) who have made significant contributions for making this release successful. Thanks to all the contributors, translators, distributors and users!!