You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Immutability helps in scalability -- since our data is immutable it can be copied across many different processes, and we don't have to worry about other processes trying to mutate our data.
Actor Model
Actor receives some kind of messages, instructions are sent to the actor, and the actor responds with something.
Processes
Runs in processes
Identified by PID
Inter-communication by message passing
Each process has its own stack & heap allocation. This makes garbage collection really fast.
Actor
Receives messages in their own mailbox
Executed FIFO
Very cheap to create <3 kb memory
Communicate with message passing
PID == process ID. You can see which PID our REPL is operating under by using the self/o function.
iex(33)>self#PID<0.109.0>
Hello World
Script files takes .exs extensions
Compiled files are .ex
All of the code inside elixir stays inside its modules.
For convention, the file name and module name must be the same.
#hello.exs# create a moduledefmoduleHellodo#create functiondefworld()doIO.puts("Hello Elixir")endendHello.world()
Elixir is a compiled language. scripts are generall used for internal dev files.
You can run scripts by typing elixir hello.exs in your terminal
You can compile elixir files by typeing elixirc hello.exs. Compiling creates a beam file. Normally we will not compile files this way, but instead we will use the mix tool.
You can also compile in the elxir repl by using the c command.
iex(2)>chars='Mel''Mel'iex(3)>icharsTerm'Mel'DatatypeListDescriptionThis is alistofintegersthatisprintedasasequenceofcharactersdelimitedbysinglequotesbecausealltheintegersinitrepresentprintableASCII characters.Conventionally, a list of Unicode codepointsisknownasacharlistandalistofASCIIcharactersisasubsetofit.Rawrepresentation[77,101,108]ReferencemodulesListImplementedprotocolsCollectable, Enumerable, IEx.Info, Inspect, List.Chars, String.Chars
To concatenate charlists, and any lists use ++ operator
The list inside Elixir are actually linked lists. So indexing operations do not work. They are singly linked lists.
iex(6)>list=["a","b","c"]["a","b","c"]iex(7)>list[0]**(ArgumentError) the Access moduledoesnotsupportaccessinglistsbyindex,got: 0AccessingalistbyindexistypicallydiscouragedinElixir,insteadweprefertousetheEnummoduletomanipulatelistsasawhole.Ifyoureallymustaccessalistelementbyindex,youcanEnum.at/1orthefunctionsintheListmodule(elixir1.16.1) lib/access.ex:334: Access.get/3iex:7: (file)
This is because linked lists are recursive by nature.
We work with lists by either using recursive functions, or use in-built modules.
Enum module:
Enum.at(list, 0)
iex(8)>Enum.at(list,0)"a"
You can see all functions available in a module by typeing Enum. and then hitting tab in the REPL.
Arity is the number of arguments that a function takes.
h macro: it stands for helper function. It prints out the documentation for a module or function.
iex(9)>hEnum.atdefat(enumerable,index,default\\nil)@specat(t(),index(),default())::element()|default()Finds the elementatthegivenindex(zero-based).Returnsdefaultifindexisoutofbounds.Anegativeindexcanbepassed,whichmeanstheenumerableisenumeratedonceandtheindexiscountedfromtheend(forexample,-1 finds the last element).## Examplesiex>Enum.at([2,4,6],0)2iex>Enum.at([2,4,6],2)6iex>Enum.at([2,4,6],4)niliex>Enum.at([2,4,6],4,:none):none
Instantiated with the {} notation. Data inside the tuple is saved continuously inside of memory. Don't use more that 2/3 elements inside a tuple as it is very expensive to add/remove items from the tuple. use lists for large data sets.
Structs are maps with reduced functionality, compile-time checks, and default values. Reduced functionality means that structs cannot use protocols defined for maps like Enum, but can use functions from the Map module.
In order to create a structure, you need to create a module.
Macros: codes that generates other code. defmodule and defstruct are macros.