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 🙂

Focusing on Solutions

We had a class in oneness university a year back and there was a session on the questions that arise in the mind. Had learnt that quality of questions that arise in the mind affects the quality of life. Contemplating and working on it over a year now, found that some common questions that arise in the mind can be categorized to,

+ Why,  When,  What, How etc.

These questions can lead to focusing on either the problem or solution. Solutions are always Futuristic.

When the questions of Why, What, When etc. comes up  in the mind with a past context,  ‘Why’ more often lead to regret.’Why’ for a happy event that happened in the past does not arise. ‘When’ many a times produced anxiety. ‘What’ though inquisitive was not so powerful.

The question ‘How‘ was so powerful that it changed the whole outlook of thinking. When ‘How’ was placed in the mind, it started making the mind more innovative. The mind was no longer worrying about any problem, but started to produce different solutions. The mind moves into finding out ways to achieve something.  It always gives a good feeling while contributing something very useful to someone.

  • How do I achieve something ?
  • How can I make something exceptional ?
  • How did you do it ?

Learnt that ‘why’ can define a purpose behind  a solution that can draw people to use our products, from a ted talk, “How great Leaders inspire Action“.

 

Centeredness is something I had learnt in recent class. For arriving at a great solution, it is important to ignore our perspectives on something and hear the views from others shoes. This is to be ‘other-centered‘, rather than being ‘self-centered’. I realised this is ‘Art of Listening’.

This is very useful in all cases. Some cases where this has to be present for sure are,

+ While negotiating with someone
+ While dealing with customers/stake holders

One of the highly respected friends who serves in a leadership role in a big company said to me,  ‘I Focus only on Future’. This was implicitly conveying a message that he was always focusing on solutions. While discussing with a senior product manager, he was speaking on the importance of listening from customers view. This implicitly conveyed about ‘other-centeredness’.

Combining the two aspects ‘Asking How’ and ‘Being Other-Centered’ takes the mind to another level which is ‘Focusing on Solutions‘ 🙂

 

 

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 🙂

Words of silence

When Buddha got enlightened on that full moon day in the month of May, he maintained silence for a whole week. All the angels in the heaven got frightened. They knew that it was only once in a millennium that someone blossoms like Buddha. They requested him to say something. He said, “Those who know, know even without my saying it and those who do not know will not know even by my saying it.”

Buddha said, “Words cannot convey it. As many scriptures in the past have revealed, truth begins where words end.”

The angels said, “What you say is right. But consider those who are in between, who are neither fully enlightened nor totally ignorant. For them, a few words will give a push. For their sake, you speak and every word of yours will create silence.”

The purpose of words is to create silence. If words create more noise, they have not reached their goal.

— from Buddha, Manifestation of Silence
[courtesy – a facebook post from Sri RaviShankar]

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.

Thoughts and Energy level of the body

I started experimenting with anandha mandala during the last couple of weeks. I found during the period that the thoughts that arise are very much related with the energy level of the body.  When the energy levels are low the thoughts are often questions/complaints and seek an escape from the situation. While on the other hand, immediately after doing the ananda mandala, the thoughts are entirely different on the same issue.

If one does believe that the thoughts belong to an individual and acts based on them, it might be necessary to raise the energy levels of the body.  I guess there are many forms of breathing exercises in the form of yoga which is would help. The physical exercises like running and cycling also  helps in regulating the breathe, in turn affecting the mind. If some of these are followed consistently, the reaction for a particular situation would be often positive..

While on the other hand, if one observes the movement of thought and realizes the futile nature of the same and renounces it  in relationships with people, the energy would remain still. Which means the thought would find its use for some technical work and then cease. This, with my current observations is the way to be in the present. While am inquiring on these, I get a feeling that its a new way of living and its still sinking in me…

Note: these are just my personal observations through experimenting with teachings from oneness university and j krishnamurthy.

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..