Monthly Archives: October 2017

Mix in Elixir

Mix

Hello World in Elixer

before we move forward in code we need to learn about Mix. If you familiar with composer in PHP,  npm in NodeJS similer Mix is Bundler . It’s acrucial part of any Elixir project and in this lession we are explore its some best feature.

Content we learn here

  • New Porject
  • Interactive
  • Compilation
  • Managing Dependencies
  • Environment

New Project

when you are ready to create a new Project in Elixir . Mix makes it easy with the mix new projectName 

commands . Replace the project Name with your own project name

mix elixir Mix in Elixir Screenshot from 2017 10 31 01 04 34 300x178

in this example i created hello_world project . some rules in Project name that is project name must be every character small character . After create project then enter this project folder using command

cd hello_world

now open our project any of your favorite  editor inside lib folder you will see hello_world.ex file . Open the file you see the code as like as it

 

before understand everything i will change the file as like as below

now we start digging our code . Every file in Elixir start with a module.  we write our own function and variable inside module.  here we create a function which return hello world. def  keyword used to start any function or module and every def is started than it need to end .

After we complete our code we need to execute our code and run .its called Interactive.

Interactive

It may be necessary to use iex within the context of our application . Mix makes it easy , We can start a news iex session

 

Starting iex this way will load your application and dependencies into your current runtime .

then we need to enter our own module by writing our module name

$ iex(1)> this part already written our command line editor we only need to write HelloWorld which is our module name.

then code enter this module then we need to write function name to execute .

both of line are same in Elixir no need to write bracket end of function name . after writing this line you see hello world in your screen .

If you not family about kotlin please see my previous tutorial

Elixir for Beginners

Elixir is a dynamic functional programming language. Its build for salable and maintainable application. Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, its used in web development and embedded software domain .

Before jump in deeper we first need to know why we need to learn Elixir .

  • It’s fantastic for serving web pages. It is so fast, in fact, that standard ways of measuring webserver performance are too slow and thus give invalid result.
  • Evaluating Elixir / Phoenix for a web-scale, performance-critical application which means you end up needing to use Tsung, which is surprise written in Erlang (same VM that Elixir uses).
  • It’s fantastic for chat/voice server apps.
  • The same tech behind Elixir is behind WhatsApp and Facebook Messenger.
  • And a lot of other tech like game servers,  the excellent gaming voice chat app https://discordapp.com/ is also powered by Erlang/Elixir on the backend.
  • It’s fantastic for driving low-level hardware (as firmware): nerves
  • It’s fantastic for creating stable, long-term, performance, well-tested codebases,
  • and many more reason

Installation Process of Elixer :

see this link , it fully describe about how to install it in your machine operating system.

no more this part we learn step by step in our tutorial series about elixer .

 

encapsulation in Kotlin

Encapsulation in Kotlin:

//Link of my previous tutorial about Object oriented programming in Kotlin . Complete below part first to better understand //

  1. Kotlin OOP part 1
  2. Kotlin OOP part 2
  3. Kotlin OOP part 3
  4. Kotlin OOP part 4

There are four visibility modifiers in Kotlin. private, protected, internal and public. We learn every part step by step with a basic code example.

 

In this example code I am not using any access modifier . basic rules of access modifier:

  • If you not provide any access modifier , public is used by default that means your declaration visible everywhere
  • if you make a declaration private means it will be visible inside this class  outer world can not access it
  • protected only visible its own class and its child class . ( we discuss it later)
  • if you make it internal it only visible in this package only

In object oriented programming sense field level data or attribute or property must be private . now we modify our previous example below format :

 

now see our code field level data is protected now . In our example Client don’t need employee first , middle or last name individually . Management only need employee full name or reverse name.

if now we want to modified employee firstName it return compile time error cause private data can not access outside of this class . It is called the encapsulation of object oriented programming .

Filed level data first , middle, last name only set when object is born or created others time filed level value can not modified.

If our client tell me he/she need mechanism to change first name and need specific user last name. This type of feature setter getter  is the best option .

 

setter method get data from parameter then change data . getter method only return data .

this keyword : this is a special keyword which is used to  refer this class property or method whatever its modifier . details of this keyworkd

 

no more today practice practice practice . more and more practice developed your understand and thinking

Object Oriented Programming Kotlin (Part:04)

Object Oriented Programming Kotlin (Part:04)

OOP in kotlin is one of the most important topic in kotlin language. We will refactoring our code, in this part of this tutorial. If you see our below code we refactor our code in OOP way but line is not reduce. So we need more refactoring our code.

Another note: Create object in Kotlin no need to write new keyword .

We set our employee’s field value when the object is born. Every object oriented programming language when a object is born a special method is called which name is constructor . If you not write any constructor in your code a default constructor is created automatically . If you again see out folder Employee file

you see constructor() is created in first file of byte code. It is the default constructor.

Default constructor : a constructor which has no signature or parameter.

we need to crate a constructor which store employee first , middle and last name when object is born . So we
need to modify our Employee class with a constructor.

 

here Employee Class we write a constructor . now

now see 6 line of code is replace by only 3 line . But you uncomment  first part of this code create error cause if you write any constructor default constructor will removed.

after clicking run button if you again see the out folder Employee file you see the difference (default constructor is removed)

 

now compare both of the bitcode version of Employee Class you see the differences.

Object Oriented Programming in kotlin links:

Kotlin language supports OOP (Object Orientated Programming). There are four terms in OOP. The first term is encapsulation, second is abstraction, third inheritance and finally fourth is polymorphism

Next part of this tutorial series we will learn what is encapsulation in Kotlin.

Happy Coding!!!

Object Oriented Programming Kotlin (Part:03)

Object Oriented Programming Kotlin (Part:03)

 

note:

  • some important note of Kotlin that is filed level data or property has no default value in kotlin so we need to set initialize value of every field level data
  • In Kotlin method write using fun keyword .  similar others programming language method must be has a return type or void(has no return )

 

If you enter see left side project files you see a folder name out. expended the out folder you see the Employee file click to open this file you see the bytecode of your class . It’s only readonly. when you click to run the app , bytecode generate inside out folder using your code which is written in src folder in your project

 

the byteCode is looking similer this file . Dont worry if you not understand this after completing our full tutorial series we learn everything about this code

 

We will learn interesting feature of OOP contractor in next tutorial.

 

Happy Coding !!!

Object Oriented Programming in Kotlin (Part:02)

Object Oriented Programming in Kotlin (Part:02)

In previous part, we already had discuss why object oriented programming in kotlin is needed then started discussion about kotlin Class, kotlin Object.  Every object has two parts:

  • has: “has” means which element present in this object
  • does(): “does” means which work is done.

In this part, we will try to solve our previous problem in OOP way.

kotlin Class VS kotlin Object

 

now this part we solve this problem in Object Oriented way . First we need to create a structure with represent 200 employee . If you think a plastic manufacturer company first create a dice , then they use this dice to rebuilt all the same type plastic product. So its same to our code . We need to create a dice for every employee . so we create a class ( a class is the blueprint of object)

 right click kotlin folder then select kotlin file/class => dialog provide a class name. Here we write Employee ( it is best practice in every programming language Class name must be PasalCase (first character capitalize) ) 

 

has value also called field value, property

does()  also called method

Now we change our code in OOP pattern . Here every employee is a object of Employee class.
It is the beauty of object oriented programming in kotlin . Next tutorial we refactor our code in more structural way.

Object Oriented Programming in kotlin links:

Happy Coding!!!

Object Oriented Programming Kotlin (Part:01)

Object Oriented Programming Kotlin (Part:01)

Kotlin language supports OOP (Object Orientated Programming). Before writing any OOP code we first need to know why object oriented programming in kotlin is needed. We can solve any type of problem using procedural oriented way. So why we learn object oriented programming .

For a simple example we learn the answer of why . Let a organization has 200 members. your boss tell you write a application which get every employee’s name and output their full name and reverse name .

In procedural oriented way you write code this way. Suppose we mansion first 5 members name as: MD John Ahamed, Mr Sunil , Md Humayun Ahamed, Mr Sourav Saha , Md Arif Hossain .

you write code

every employee sense you write 5 lines of code . So 200 employee you need 200*5 = 1000 line of code. One another issue is if 2 people has same name how to manage . This code indicating for deeper problem in the system also known bad smell of code.

So how we solve this problem . Now solving this type of problem object oriented programming is needed.

If we create a structure for every employee and reuse this structure for every employee sense that solve our problem.

The structure which is used every employee sense it called Class

Everything in this world is Object . kotlin object has two part:

  • has
  • does()

suppose Laptop is a object , why it is object

  • laptop
    • has (mouse, keyboard, speaker, processor, ram …… many more )
    • does( solve mathmatical problem, listening music , playing games etc)

has means which element present in this object and does means which work is done .

Here we think more object

  • Shop is a object
    • has ( goods, employee, computer, a room etc ….)
    • does( provide everyday’s need etc..)
  • Dream is a object
    • has ( a subject  )
    • does( provide feel, emotion etc..)

seen and unseen every element in this word is object . So try to find out 10 element which is object and tell me why it is object.

kotlin Class: a class is the blueprint of object

Object Oriented Programming in Kotlin is one of the most important topic in kotlin language.

Object Oriented Programming in kotlin links:

No more today . Next post  we will solve our previous problem in OOP way

Arrays in Kotlin

Two main ways to create array in kotlin. Helper function arrayOf() and constructor Array() . 

details of arrayOf() function

to access any element of this array we need to write myArray[index] to get specific value.

It is noted that in array you can assign different data type value . for example

To enforce that all the element of array must be fixed size , we need to use generics . arrayOf<T>

first line of this code example is wrong cause we define it must be integer type array but we assign different data type value inside this array. So If we use generics must follow the data type first.

We also have other utility functions to create arrays of other types such as charArrayOf()booleanArrayOf()longArrayOf()shortArrayOf()byteArrayOf(), and so on. Behind the scenes, using these functions will create an array of their respective Java primitive types. In other words, intArrayOf() will compile to the regular Java primitive type int[]byteArrayOf() will be byte[]longArrayOf() will be long[], and so on.

 

The Array() Constructor

Now see how to create array With Array(). This constructor requires a size of array and a lamda expression. The lamda expression is to initialize the array with elements

In this code we tell array size is 5 . second part generate value using key or array . so the value of this array 0,2,4,6 and 8

 

note: we learn lamda expression in another part of this tutorial series

 

loop in Kotlin

Similar with any other language  loop in kotlin .

  • for loop
  • while loop
  • do while loop

basic for loop example

this loop is equivalent of java for loop

note:  in Kotlin if you want to access variable inside double quotation use $ sign to access this variable .

While Loop

do while loop 

 

Conditional Statement in Kotlin (if, when)

at first write a simple program then discuss

this program we read input from user in command line and convert it in Integer . In kotlin readLine() function is used to get input from command line .

If else condition in Kotlin is same in any other language . inside if block condition true then enter if block if not code enter else block .

this code also write in short form of if.. else cause inside of block has only one line bracket is not needed. But best practice is use bracket if block has one or multi line .

 

Nested If else example Grade Point :

here we write nested if else grade point program .

this program also we can write more easily using another system

this range is equivalent of (mark in 0..32) == (mark>=0 && mark<33)

Ranges:

Range expressions are formed with rangeTo functions that have the operator form .. which is complemented by in and !in. Range is defined for any comparable type, but for integral primitive types it has an optimized implementation. We learn ranges more details next part to tutorial series.

 

When Experssion

Another conditional statement using in every programming language that is SWITCH.  in kotlin switch is replace by when operator.

 when matches its argument against all branches sequentially until some branch condition is satisfied. whencan be used either as an expression or as a statement.

The else branch is evaluated if none of the other branch conditions are satisfied. If when is used as an expression, the else branch is mandatory, unless the compiler can prove that all possible cases are covered with branch conditions.