Elixir Syntax

From Elixir Wiki
Jump to navigation Jump to search

Introduction[edit]

Elixir is a dynamic, functional programming language built on top of the Erlang virtual machine (BEAM). It provides a concise and expressive syntax that allows developers to write clean and maintainable code. This article aims to provide an overview of the Elixir syntax, covering its key features and conventions.

Data types[edit]

Atoms[edit]

An atom is a constant whose value is its own name. It is used to represent static values, such as status codes or names. Atoms are declared by writing a lowercase word or a word enclosed in single quotes:

Example:

ok
hello
'Hello, World!'

Numbers[edit]

Elixir supports both integers and floating-point numbers. Integers can be written in decimal, hexadecimal, octal, and binary representations. Floating-point numbers can be written using the decimal or the scientific notation:

Example: 10 0xFF 0o777 0b1010 3.14 12.5e-3

Strings[edit]

Strings in Elixir are delimited by double quotes. They can contain escape sequences and interpolations:

Example: "Hello, World!" "Today is #{Date.utc_today()}."

Tuples[edit]

A tuple is an ordered collection of elements, enclosed in curly braces. Tuples can hold any combination of data types:

Example: {:ok, 42} {"John", 25}

Lists[edit]

A list is a dynamic collection of elements, enclosed in square brackets. Lists can hold any combination of data types and are primarily used for pattern matching and recursion:

Example: [1, 2, 3] [true, "Hello", :ok]

Maps[edit]

A map is a collection of key-value pairs. Keys can be any data type, and values can be any Erlang term. Maps are defined using a literal syntax or the %{} syntax:

Example: %{name: "John", age: 25} %{:ok => "Success", :error => "Failure"}

Anonymous Functions[edit]

Anonymous functions, also known as lambda functions, allow you to create function literals. They are declared using the fn keyword followed by argument names and the function body:

Example: fn x -> x * 2 end fn a, b -> a + b end

Control flow[edit]

Pattern Matching[edit]

Pattern matching is a powerful feature in Elixir that allows for complex data manipulation and conditional branching. It is used extensively in function clauses and case statements:

Example: {status, message} = {:ok, "Success"} case response do

 {:ok, result} -> IO.puts("Result: #{result}")
 {:error, reason} -> IO.puts("Error: #{reason}")

end

Conditional Expressions[edit]

Elixir provides if, unless, and case expressions for control flow based on conditions:

Example: if condition do

 # do something

else

 # do something else

end

unless condition do

 # do something

else

 # do something else

end

case value do

 pattern1 -> # do something
 pattern2 -> # do something else
 _ -> # default case

end

Conclusion[edit]

This article provided an introduction to the Elixir syntax, covering the key data types, control flow constructs, and conventions. Understanding the Elixir syntax is essential for writing clean, efficient, and maintainable code in the Elixir programming language. For more in-depth information, refer to the articles on specific topics available on this wiki.