Day 3 of erlang was all about concurrency. This is the place where erlang really shines.
As a first exercise you have to look into the OTP documentation. OTP is a framework/library that makes concurrency in erlang even simpler. There are some base templates that offer you all kinds of client/server functionality. Here is a great description of the design principles of OTP: http://www.erlang.org/doc/design_principles/des_princ.html.
The first coding exercise
In the first example we have to create a monitor for the translate_service. I used an OTP supervisor to do this, so first I converted the translate server to use the gen_server:
Next I wrote a simpel monitor by using the gen_supervisor from otp:
So in OTP it is really easy to start a server process and monitor it. You have quite a lot of options on how the monitor should handle an error situation.
The other coding exercises
The other three coding exercises are about the roulette example and its doctor. First I adjusted the classes to otp. and the doctor as supervisor: After that I had to monitor the doctor so it restarts itself. I didn’t find any possible solution to make a process restart itself, so I think the exercise would be to create a monitor that monitors another monitor;-). Well in otp a supervisor can also monitor another supervisor so I created another supervisor that supervises the doctor: I really want to start with clojure and have some exams ahead of me so I decided to skip the last exercise and the bonus exercises. Maybe another day I will give them a try.
Erlang is a bit strange to me. I love the concurrency design and error handling, especially in combination with otp. But the syntax is a bit strange for me, and some simpel things seem harder than they should be. Maybe this is because I’m more used to imperatif programming languages. All things considered I enjoyed the language and will hopefully give it another go.