Explore tens of thousands of sets crafted by our community.
Elixir Language Syntax
25
Flashcards
0/25
Variable Scope
Variables in Elixir are scoped to the block they are defined in. Outer scope variables are accessible inside blocks but not vice versa. ```elixir if true do a = 5 end # a is not accessible here ```
Anonymous Functions
Anonymous functions in Elixir are defined using the 'fn' keyword and are called using the dot (.) operator. ```elixir add = fn a, b -> a + b end result = add.(1, 2) ```
Pipe Operator
The pipe operator '|>' is used to chain functions together, passing the result of the previous function as the first argument of the next. ```elixir result = 1 |> add(2) |> add(3) ```
Pattern Matching
Pattern matching is a fundamental concept in Elixir where '=' is used to match values, often used for extracting values from complex data types. ```elixir {a, b} = {1, 2} ```
Modules
Modules in Elixir are used to group functions, defined with 'defmodule'. ```elixir defmodule Math do def add(a, b), do: a + b end ```
Enumerables and Streams
Elixir provides 'Enum' and 'Stream' modules for working with collections. 'Enum' is eager, 'Stream' is lazy. ```elixir Enum.map([1, 2, 3], fn x -> x * 2 end) ```
Atoms
Atoms are constants whose value is their own name, used as identifiers. ```elixir status = :ok ```
Comprehensions
Comprehensions in Elixir are a shorthand for generating lists based on existing collections. ```elixir for x <- [1, 2, 3, 4], do: x * x ```
Conditional 'if'
The 'if' macro is used for simple conditional checks, returning a value. ```elixir result = if true, do: :ok, else: :error ```
Lists
Lists are used to store an ordered collection of items, enclosed in square brackets []. ```elixir list = [1, 2, 3, 4, 5] ```
Pattern Matching in Function Parameters
Elixir allows pattern matching directly in function parameter lists to simplify function clauses. ```elixir def process({:ok, result}), do: IO.puts result def process({:error, reason}), do: IO.puts reason ```
The 'case' Construct
The 'case' construct is used to match against different patterns, similar to a switch statement. ```elixir case {1, 2, 3} do {4, 5, 6} -> "No match" {1, x, 3} -> "Matched with #{x}" end ```
Aliases
Aliases in Elixir are capitalized identifiers used to reference modules. ```elixir alias MyProject.Math, as: Maths ```
Recursion
Recursion is the process in which a function calls itself directly or indirectly. ```elixir def sum([head | tail]), do: head + sum(tail) def sum([]), do: 0 ```
Concurrency with Processes
Elixir uses lightweight processes for concurrency, created with 'spawn'. ```elixir pid = spawn(fn -> perform_complex_calculation() end) ```
String Interpolation
String interpolation allows you to insert the value of variables into strings using #{}. ```elixir name = "world" greeting = "Hello, #{name}!" ```
Guard Clauses
Guard clauses in Elixir are used to supplement pattern matching in functions, by providing additional checks. ```elixir def is_adult(age) when age >= 18 do true end ```
Erlang Interoperability
Elixir can interact with Erlang libraries seamlessly using the ':atom' syntax. ```elixir :crypto.hash(:sha256, "hello") ```
Binaries and Bitstrings
Binaries are used to represent and manipulate binary data, with bitstrings being a generalization. ```elixir binary = <<1, 2, 3>> ```
Sigils
Sigils in Elixir are used for working with different literals, like strings and regular expressions, written with a tilde '~'. ```elixir regex = ~r{[aeiou]} ```
Maps
Maps are the key-value data structure in Elixir, defined with the %{} syntax. ```elixir map = %{a: 1, b: 2} ```
Mix Tool
Mix is a build tool that provides tasks for creating, compiling, and testing Elixir projects. ```terminal mix new my_project ```
Tuples
Tuples are used to store a fixed number of items, enclosed in curly braces {}. ```elixir tuple = {1, :ok, "hello"} ```
Keyword Lists
Keyword lists are a list of tuples where the first item is an atom, used mainly for options. ```elixir options = [debug: true, verbose: false] ```
Protocols
Protocols in Elixir define a set of functions that must be implemented for a specific data type. ```elixir defprotocol Size do def calculate(data) end ```
© Hypatia.Tech. 2024 All rights reserved.