পৃষ্ঠাসমূহ

Search Your Article

CS

Pageviews

Tuesday, January 31, 2017

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.

Monday, January 30, 2017

Elixir - Modules

In Elixir we group several functions into modules. We’ve already used many different modules in the previous chapters such as the String module, Bitwise module, Tuple module, etc.
In order to create our own modules in Elixir, we use the defmodule macro. We use the def macro to define functions in that module:

Elixir - Aliases

In order to facilitate software reuse, Elixir provides three directives (alias, require and import) plus a macro called use summarized below:
# Alias the module so it can be called as Bar instead of Foo.Bar
alias Foo.Bar, as: Bar

# Ensure the module is compiled and available (usually for macros)
require Foo

Elixir - Functions

A function is a set of statements organized together to perform a specific task. Functions in programming work mostly like function in math. You give functions some input, they generate output based on the input provided. There are 2 types of functions in elixir:

Elixir - Recursion

Recursion is a method where the solution to a problem depends on solutions to smaller instances of the same problem. Most computer programming languages support recursion by allowing a function to call itself within the program text.

Elixir - Loops

Due to immutability, loops in Elixir (as in any functional programming language) are written differently from imperative languages. For example, in an imperative language like C, you would write:
for(i = 0; i < 10; i++) {
  printf("%d", array[i]);

Elixir - Enumerables

An enumerable is an object that may be enumerated. "Enumerated" means to count off the members of a set/collection/category one by one (usually in order, usually by name).
Elixir provides the concept of enumerables and the Enum module to work with them. The functions in the Enum module are limited to, as the name says, enumerating values in data structures.

Elixir - Streams

All the functions in the Enum module are eager. Many functions expect an enumerable and return a list back. This means that when performing multiple operations with Enum, each operation is going to generate an intermediate list until we reach the result. We saw an example for the same in the previous chapter.

Elixir - Structs

Structs are extensions built on top of maps that provide compile-time checks and default values.

Defining structs

To define a struct, the defstruct construct is used:
defmodule User do
   defstruct name: "John", age: 27
end

Elixir - Protocols

Protocols are a mechanism to achieve polymorphism in Elixir. Dispatching on a protocol is available to any data type as long as it implements the protocol. Lets look at an example of using protocols. We used a function called to_string in the previous chapters to convert from other types to the string type.

Elixir - File IO

File IO is an integral part of any programming language as it allows the language to interact with the files on the file system. In this chapter we'll have a look at 2 modules: Path and File.

Elixir - Processes

In Elixir, all code runs inside processes. Processes are isolated from each other, run concurrent to one another and communicate via message passing. Elixir’s processes should not be confused with operating system processes. Processes in Elixir are extremely lightweight in terms of memory and CPU (unlike threads in many other programming languages). Because of this, it is not uncommon to have tens or even hundreds of thousands of processes running simultaneously.