Tuesday, January 31, 2017

Erlang - Overview

Erlang is a functional programming language which also has a runtime environment. It was built in such a way that it had integrated support for concurrency, distribution and fault tolerance. Erlang was originally developed to be used in several large telecommunication systems from Ericsson.

Erlang - Environment

Try it Option Online

You really do not need to set up your own environment to start learning Erlang programming language. Reason is very simple, we have already set up Erlang Programming environment online, so that you can compile and execute all the available examples online at the same time when you are doing your theory work.

Erlang - Basic Syntax

In order to understand the basic syntax of Erlang, let’s first look at a simple Hello World program.

Example

% hello world program
-module(helloworld). 
-export([start/0]). 

start() -> 

Erlang - Shell

The Erlang shell is used for testing of expressions. Hence, testing can be carried out in the shell very easily before it actually gets tested in the application itself.
The following example showcases how the addition expression can be used in the shell. What needs to be noted here is that the expression needs to end with the dot (.) delimiter.

Erlang - Data Types

In any programming language, you need to use several variables to store various types of information. Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in the memory to store the value associated with that variable.

Erlang - Variables

In Erlang, all the variables are bound with the ‘=’ statement. All variables need to start with the upper case character. In other programming languages, the ‘=’ sign is used for the assignment, but not in the case of Erlang. As stated, variables are defined with the use of the ‘=’ statement.

Erlang - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
Erlang has the following type of operators −
  • Arithmetic operators
  • Relational operators
  • Logical operators
  • Bitwise operators

Erlang - Loops

Erlang is a functional programming language and what needs to be remembered about all functional programming languages is that they don’t offer any constructs for loops. Instead, functional programming depends on a concept called recursion.

Erlang - Decision Making

Decision making structures requires that the programmer should specify one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

Erlang - Functions

Erlang is known as a functional programming language, hence you would expect to see a lot of emphasis on how functions work in Erlang. This chapter covers what all can be done with the functions in Erlang.

Erlang - Modules

Modules are a bunch of functions regrouped in a single file, under a single name. Additionally, all functions in Erlang must be defined in modules.
Most of the basic functionality like arithmetic, logic and Boolean operators are already available because the default modules are loaded when a program is run.

Erlang - Recursion

Recursion is an important part of Erlang. First let’s see how we can implement simple recursion by implementing the factorial program.

Erlang - Numbers

In Erlang there are 2 types of numeric literals which are integers and floats. Following are some examples which show how integers and floats can be used in Erlang.
Integer − An example of how the number data type can be used as an integer is shown in the following program. This program shows the addition of 2 Integers.

Erlang - Strings

A String literal is constructed in Erlang by enclosing the string text in quotations. Strings in Erlang need to be constructed using the double quotation marks such as “Hello World”.
Following is an example of the usage of strings in Erlang −

Erlang - Lists

The List is a structure used to store a collection of data items. In Erlang, Lists are created by enclosing the values in square brackets.
Following is a simple example of creating a list of numbers in Erlang.

Erlang - File I/O

Erlang provides a number of methods when working with I/O. It has easier classes to provide the following functionalities for files −

Erlang - Atoms

An atom is a literal, a constant with name. An atom is to be enclosed in single quotes (') if it does not begin with a lower-case letter or if it contains other characters than alphanumeric characters, underscore (_), or @.
The following program is an example of how atoms can be used in Erlang. This program declares 3 atoms, atom1, atom_1 and ‘atom 1’ respectively. So you can see the different ways an atom can be declared.

Erlang - Maps

A map is a compound data type with a variable number of key-value associations. Each key-value association in the map is called an association pair. The key and value parts of the pair are called elements. The number of association pairs is said to be the size of the map.

Erlang - Tuples

A tuple is a compound data type with a fixed number of terms. Each term in the Tuple is called an element. The number of elements is said to be the size of the Tuple.
An example of how the Tuple data type can be used is shown in the following program.

Erlang - Records

Erlang has the extra facility to create records. These records consist of fields. For example, you can define a personal record which has 2 fields, one is the id and the other is the name field. In Erlang, you can then create various instances of this record to define multiple people with various names and id’s.
Let’s explore how we can work with records.

Erlang - Exceptions

Exception handling is required in any programming language to handle the runtime errors so that normal flow of the application can be maintained. Exception normally disrupts the normal flow of the application, which is the reason why we need to use Exception handling in our application.

Erlang - Macros

Macros are generally used for inline code replacements. In Erlang, macros are defined via the following statements
  • -define(Constant, Replacement).
  • -define(Func(Var1, Var2,.., Var), Replacement).

Erlang - Header Files

Header files are like include files in any other programming language. It is useful for splitting modules into different files and then accessing these header files into separate programs. To see header files in action, let’s look at one of our earlier examples of records.

Erlang - Preprocessors

Before an Erlang module is compiled, it is automatically processed by the Erlang Preprocessor. The preprocessor expands any macros that might be in the source file and inserts any necessary include files.
Ordinarily, you won’t need to look at the output of the preprocessor, but in exceptional circumstances (for example, when debugging a faulty macro), you might want to save the output of the preprocessor.

Erlang - Pattern Matching

Patterns look the same as terms – they can be simple literals like atoms and numbers, compound like tuples and lists, or a mixture of both. They can also contain variables, which are alphanumeric strings that begin with a capital letter or underscore. A special "anonymous variable", _ (the underscore) is used when you don't care about the value to be matched, and won't be using it.

Erlang - Guards

Guards are constructs that we can use to increase the power of pattern matching. Using guards, we can perform simple tests and comparisons on the variables in a pattern.
The general syntax of the guard statement is as follows −
function(parameter) when condition ->
Where,

Erlang - BIFS

BIFs are functions that are built into Erlang. They usually do tasks that are impossible to program in Erlang. For example, it’s impossible to turn a list into a tuple or to find the current time and date. To perform such an operation, we call a BIF.

Erlang - Binaries

Use a data structure called a binary to store large quantities of raw data. Binaries store data in a much more space efficient manner than in lists or tuples, and the runtime system is optimized for the efficient input and output of binaries.

Erlang - Funs

Funs are used to define anonymous functions in Erlang. The general syntax of an anonymous function is given below

Syntax

F = fun (Arg1, Arg2, ... ArgN) ->
      ...
   End
Where

Erlang - Processes

The granularity of concurrency in Erlang is a process. A process is an activity/task that runs concurrently with and is independent from the other processes. These processes in Erlang are different than the processes and threads most people are familiar with. Erlang processes are lightweight, operate in (memory) isolation from other processes, and are scheduled by Erlang’s Virtual Machine (VM).

Erlang - Email

To send an email using Erlang, you need to use a package available from github for the same. The github link is – https://github.com/Vagabond/gen_smtp
This link contains an smtp utility which can be used for sending email from an Erlang application. Follow the steps to have the ability to send an email from Erlang

Erlang - Databases

Erlang has the ability to connect to the traditional databases such as SQL Server and Oracle. Erlang has an inbuilt odbc library that can be used to work with databases.

Erlang - Ports

In Erlang, ports are used for communication between different programs. A socket is a communication endpoint that allows machines to communicate over the Internet by using the Internet Protocol (IP).

Erlang - Distributed Programming

Distributed Programs are those programs that are designed to run on networks of computers and that can coordinate their activities only by message passing.
There are a number of reasons why we might want to write distributed applications. Here are some of them.

Erlang - OTP

OTP stands for Open Telecom Platform. It’s an application operating system and a set of libraries and procedures used for building large-scale, fault-tolerant, distributed applications. If you want to program your own applications using OTP, then the central concept that you will find very useful is the OTP behavior. A behavior encapsulates common behavioral patterns — think of it as an application framework that is parameterized by a callback module.

Erlang - Concurrency

Concurrent programming in Erlang needs to have the following basic principles or processes.
The list includes the following principles −

piD = spawn(Fun)

Creates a new concurrent process that evaluates Fun. The new process runs in parallel with the caller. An example is as follows −

Erlang - Performance

When talking about performance the following points need to be noted about Erlang.
  • Funs are very fast − Funs was given its own data type in R6B and was further optimized in R7B.
  • Using the ++ operator − This operator needs to be used in the proper way. The following example is the wrong way to do a ++ operation.

Erlang - Drivers

Sometimes we want to run a foreign-language program inside the Erlang Runtime System. In this case, the program is written as a shared library that is dynamically linked into the Erlang runtime system. The linked-in driver appears to the programmer as a port program and obeys exactly the same protocol as for a port program.

Erlang - Web Programming

In Erlang, the inets library is available to build web servers in Erlang. Let’s look at some of the functions available in Erlang for web programming. One can implement the HTTP server, also referred to as httpd to handle HTTP requests.
The server implements numerous features, such as −

Erlang - Quick Guide

Erlang - Overview

Erlang is a functional programming language which also has a runtime environment. It was built in such a way that it had integrated support for concurrency, distribution and fault tolerance. Erlang was originally developed to be used in several large telecommunication systems from Ericsson.

Erlang - Useful Resources

The following resources contain additional information on Erlang. Please use them to get more in-depth knowledge on this.

Discuss Erlang

Erlang is a general purpose or you might say a functional programming language and runtime environment. It was built in such a way that it had inherent support for concurrency, distribution and fault tolerance. Erlang was originally developed to be used in several large telecommunication systems. But it has now slowly made its foray into diverse sectors like ecommerce, computer telephony and banking sectors as well.

Elixir - Overview

Elixir is a dynamic, functional language designed for building scalable and maintainable applications. Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.

Elixir - Environment

In order to run Elixir, you need to set it up locally on your system. To install Elixir, you'll first need erlang. On some platforms, elixir packages come with erlang in them.

Elixir - Basic Syntax

We'll start with the customary 'Hello World' program. Start the elixir interactive shell using
iex
After the shell starts, use the IO.puts function to "put" the string on the console output. Enter the following in your elixir shell:
IO.puts "Hello world"

Elixir - Data Types

For using any language, you need to understand the basic data types the language supports. In this chapter, we will discuss 7 basic data types supported by the elixir language: integers, floats, booleans, atoms, strings, lists and tuples.

Elixir - Variables

A variable provides us with named storage that our programs can manipulate. Each variable in Elixir has a specific type, which determines the size and layout of the variable's memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable.
Elixir supports the following basic types of variables:

Elixir - Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. There are a LOT of operators provided by elixir. They are divided in the following categories:
  • Arithmetic operators
  • Comparison operators
  • Boolean operators
  • Misc operators

Elixir - Pattern Matching

Pattern matching is a technique which Elixir inherits form Erlang. Pattern matching is a very powerful technique that allows us to extract simpler substructures from complicated data structures like lists, tuples, maps, etc.

Elixir - Decision Making

Decision making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.
Following is the general from of a typical decision making structure found in most of the programming language:

Elixir - Strings

Strings in Elixir are inserted between double quotes, and they are encoded in UTF-8. Unlike C and C++ where default strings are ASCII encoded and only 256 different characters are possible, UTF-8 consists of 66536 code points. This means that UTF-8 encoding consists of those many different possible characters. Since the strings use utf-8, we can also use symbols like: ö, ł, etc.

Elixir - Char lists

A char list is nothing more than a list of characters. For example:
IO.puts('Hello')
IO.puts(is_list('Hello'))
When running above program, it produces following result:

Elixir - Lists and Tuples

(Linked) Lists

A linked list is a heterogeneous list of elements that are stored at different locations in memory and are kept track of by using references. Linked lists are very useful data structures especially used a lot in functional programming.

Elixir - Keyword lists

So far we haven’t discussed any associative data structures, i.e. data structures that are able to associate a certain value (or multiple values) to a key. Different languages call these different names like dictionaries, hashes, associative arrays, etc.

Elixir - Maps

Keyword lists are a convenient way to address content stored in lists by key, but underneath, Elixir is still walking through the list. That might be OK if you have other plans for that list requiring walking through all of it, but it can be unnecessary overhead if you’re planning to use keys as your only approach to the data.
This is where maps come to your rescue. Whenever you need a key-value store, maps are the “go to” data structure in Elixir.