0% found this document useful (0 votes)
196 views2 pages

Cheat Sheet: Types Modules

Elixir Language Cheatsheet

Uploaded by

Emma Foster
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
196 views2 pages

Cheat Sheet: Types Modules

Elixir Language Cheatsheet

Uploaded by

Emma Foster
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Types

Integer! 1234 0xcafe 0177 0b100 10_000 Float ! Atom ! Tuple List ! ! 1.0 3.1415 6.02e23 :foo :me@home :"with spaces" { 1, 2, :ok, "xy" } (like array) [ 1, 2, 3 ] [ head | tail ] 'abc' here doc ''' (see Enum and List modules) Keyword List [ a: "Foo", b: 123 ] Binary List/String << 1, 2 >> or "abc" """ here doc """ << name::[props], >> binary, bits, bitstring, bytes, oat, integer, utf8, utf16, utf32, size(n), signed/unsigned, big/little native Truth ! true, false, nil Range ! a..b (like linked list)

Modules
defmodule mod_name do @moduledoc "description" @doc "description" function/macro end require Module use Module ! calls Module.__using__ import [what,] Module [,only:|except:] what can be :all, :functions, :macros or :default (:all but no _func) alias mod_path [, as: Name] @attribute_name value Call Erlang: :module.function_name

cheat sheet
[Link] v0.10.0

Command line
elixir [options] [Link]/[Link] iex iex -S script (e.g., iex -S mix) iex --name local iex --sname [Link] --cookie [Link] or use $HOME/.[Link] mix new / run / test / deps / etc. [Link] species build details

Guard Clause
Part of pattern match when expr where operators in expr are limited to:

Anonymous Functions
fn parms [guard]-> body parms [guard] -> body end function do parms [guard] -> body parms [guard] -> body end call with func.()

==, !=, ===, !==, >, <, <=, >=, or, and, not, !, +, -, *, /, in, is_atom is_binary is_bitstring is_boolean is_exception is_oat is_function is_integer is_list is_number is_pid is_port is_record is_reference is_tuple, abs(num), bit_size(bits), byte_size(bits), div(num,num), elem(tuple, n), oat(term), hd(list) lengt(list), node(), node(pid|ref|port), rem (num,num), round(num), self(), size(tuple| bits), tl(list), trunc(num), tuple_size(tuple) <> and ++ (left side literal)

iex Commands
#iex:break! back to prompt c "[Link]" compile r Module reload h function_name help v [n] session history

Shortcut: &1,&2 etc.

Named Functions
(Only in modules, records, etc) def name(parms) [guard] do expression end def name(parms) [guard], do: expr Default params: parameter // default defp for private functions Multiple heads with different params and/ or guards allowed.

do: vs do/end
something do expr end something, do: expr

Operators
=== !== and or not == != && || ! >, >=, <, <= +, -, *, /! ! div, rem! ! ! ! (strict) (relaxed) (oat) (integer)

else, rescue, try, ensure also generate keyword args

binary1 <> binary2! ! (concat) list1 ++ list2 ! ! (concat)

List Comprehension
lc x inlist list1, y inbits bin2, x < y, do: {x,y}

Copyright 2013 The Pragmatic Programmers, LLC. Feel free to use without modication for noncommercial applications. Content/design by Andy Hunt & Dave Thomas.

Records
defrecord Name, eld: default, [ do functions end ] [Link](eld: value, ) { Name, eld, value, } (See Record module)
(same as)

Pipelines
expr |> f1 |> f2(a,b) |> f3(c) f3(f2(f1(expr), a, b), c)

Control Flow Protocols


defprotocol [Link] do @moduledoc description @only [list of types] (optional) def name(parms) end dempl [Link], for: type do @moduledoc description def name(type, value) do expr end end Allowed types: Any Atom BitString Function List Number PID Port Record Reference Tuple if expr do exp else exp end | | | | cond do bool -> exp bool -> exp end

case expr do match [guard] -> exp match [guard] -> exp end

[Link]/books/elixir

Metaprogramming
defmacro macroname(parms) do parms are quoted args return quoted code which is inserted at call site end quote do: returns internal rep. quote bind_quoted: [name: name] do: ... unquote do: only inside quote, injects code fragment without evaluation Evaluating an internal rep. fragment: Code.eval_quoted fragment With binding: Code.eval_quoted frag, [name: value]

Regexp
%r{pattern}opts
f g! i m! r! s! u! x! match beg of ml string use named groups case insensitive ^ and $ match each line in ml reluctant (not greedy) . matches newline unicode patterns ignore whitespace and comments

Processes
pid = spawn(anon_function) pid = spawn(mod, func, args) (also spawn_link) receive do {sender, msg, } -> sender <- { :ok, value } after timeout -> ... end

Sigils
%type{ content } Delimiter: { }, [ ], ( ), <>, or non-word char %B binary %b binary w/interpolation %C character list %c character list w/interpolation %R regexp %r regexp w/interpolation %W words (white space delim) %w words w/interpolation

Predefined Names
__MODULE__ __FILE__ __DIR__ __ENV__ __CALLER__ (macros only)

Copyright 2013 The Pragmatic Programmers, LLC. Feel free to use without modication for noncommercial applications. Content/design by Andy Hunt & Dave Thomas.

You might also like