1# Numbers 20b0101011 31234 ; 0x1A ; 0xbeef ; 0763 ; 0o123 43.14 ; 5.0e21 ; 0.5e-12 5100_000_000 6 7# these are not valid numbers 80b012 ; 0xboar ; 0o888 90B01 ; 0XAF ; 0O123 10 11# Characters 12?a ; ?1 ; ?\n ; ?\s ; ?\c ; ? ; ?, 13?\x{12} ; ?\x{abcd} 14?\x34 ; ?\xF 15 16# these show that only the first digit is part of the character 17?\123 ; ?\12 ; ?\7 18 19# Atoms 20:this ; :that 21:'complex atom' 22:"with' \"\" 'quotes" 23:" multi 24 line ' \s \123 \xff 25atom" 26:... ; :<<>> ; :%{} ; :% ; :{} 27:++; :--; :*; :~~~; ::: 28:% ; :. ; :<- 29 30# Strings 31"Hello world" 32"Interspersed \x{ff} codes \7 \8 \65 \016 and \t\s\\s\z\+ \\ escapes" 33"Quotes ' inside \" \123 the \"\" \xF \\xF string \\\" end" 34"Multiline 35 string" 36 37# Char lists 38'this is a list' 39'escapes \' \t \\\'' 40'Multiline 41 char 42 list 43' 44 45# Binaries 46<<1, 2, 3>> 47<<"hello"::binary, c :: utf8, x::[4, unit(2)]>> = "hello™1" 48 49# Sigils 50~r/this + i\s "a" regex/ 51~R'this + i\s "a" regex too' 52~w(hello #{ ["has" <> "123", '\c\d', "\123 interpol" | []] } world)s 53~W(hello #{no "123" \c\d \123 interpol} world)s 54 55~s{Escapes terminators \{ and \}, but no {balancing} # outside of sigil here } 56 57~S"No escapes \s\t\n and no #{interpolation}" 58 59:"atoms work #{"to" <> "o"}" 60 61# Operators 62x = 1 + 2.0 * 3 63y = true and false; z = false or true 64... = 144 65... == !x && y || z 66"hello" |> String.upcase |> String.downcase() 67{^z, a} = {true, x} 68 69# Free operators (added in 1.0.0) 70p ~>> f = bind(p, f) 71p1 ~> p2 = pair_right(p1, p2) 72p1 <~ p2 = pair_left(p1, p2) 73p1 <~> p2 = pair_both(p1, p2) 74p |~> f = map(p, f) 75p1 <|> p2 = either(p1, p2) 76 77# Lists, tuples, maps, keywords 78[1, :a, 'hello'] ++ [2, 3] 79[:head | [?t, ?a, ?i, ?l]] 80 81{:one, 2.0, "three"} 82 83[...: "this", <<>>: "is", %{}: "a keyword", %: "list", {}: "too"] 84["this is an atom too": 1, "so is this": 2] 85[option: "value", key: :word] 86[++: "operator", ~~~: :&&&] 87 88map = %{shortcut: "syntax"} 89%{map | "update" => "me"} 90%{ 12 => 13, :weird => ['thing'] } 91 92# Comprehensions 93for x <- 1..10, x < 5, do: {x, x} 94pixels = "12345678" 95for << <<r::4, g::4, b::4, a::size(4)>> <- pixels >> do 96 [r, {g, %{"b" => a}}] 97end 98 99# String interpolation 100"String #{inspect "interpolation"} is quite #{1+4+7} difficult" 101 102# Identifiers 103abc_123 = 1 104_018OP = 2 105A__0 == 3 106 107# Modules 108defmodule Long.Module.Name do 109 @moduledoc "Simple module docstring" 110 111 @doc """ 112 Multiline docstring 113 "with quotes" 114 and #{ inspect %{"interpolation" => "in" <> "action"} } 115 now with #{ {:a, 'tuple'} } 116 and #{ inspect { 117 :tuple, 118 %{ with: "nested #{ inspect %{ :interpolation => %{} } }" } 119 } } 120 """ 121 defstruct [:a, :name, :height] 122 123 @doc ~S''' 124 No #{interpolation} of any kind. 125 \000 \x{ff} 126 127 \n #{\x{ff}} 128 ''' 129 def func(a, b \\ []), do: :ok 130 131 @doc false 132 def __before_compile__(_) do 133 :ok 134 end 135end 136 137# Structs 138defmodule Second.Module do 139 s = %Long.Module.Name{name: "Silly"} 140 %Long.Module.Name{s | height: {192, :cm}} 141 ".. #{%Long.Module.Name{s | height: {192, :cm}}} .." 142end 143 144# Types, pseudo-vars, attributes 145defmodule M do 146 @custom_attr :some_constant 147 148 @before_compile Long.Module.Name 149 150 @typedoc "This is a type" 151 @type typ :: integer 152 153 @typedoc """ 154 Another type 155 """ 156 @opaque typtyp :: 1..10 157 158 @spec func(typ, typtyp) :: :ok | :fail 159 def func(a, b) do 160 a || b || :ok || :fail 161 Path.expand("..", __DIR__) 162 IO.inspect __ENV__ 163 __NOTAPSEUDOVAR__ = 11 164 __MODULE__.func(b, a) 165 end 166 167 defmacro m() do 168 __CALLER__ 169 end 170end 171 172# Functions 173anon = fn x, y, z -> 174 fn(a, b, c) -> 175 &(x + y - z * a / &1 + b + div(&2, c)) 176 end 177end 178 179&Set.put(&1, &2) ; & Set.put(&1, &2) ; &( Set.put(&1, &1) ) 180 181# Function calls 182anon.(1, 2, 3); self; hd([1,2,3]) 183Kernel.spawn(fn -> :ok end) 184IO.ANSI.black 185 186# Control flow 187if :this do 188 :that 189else 190 :otherwise 191end 192 193pid = self 194receive do 195 {:EXIT, _} -> :done 196 {^pid, :_} -> nil 197 after 100 -> :no_luck 198end 199 200case __ENV__.line do 201 x when is_integer(x) -> x 202 x when x in 1..12 -> -x 203end 204 205cond do 206 false -> "too bad" 207 4 > 5 -> "oops" 208 true -> nil 209end 210 211# Lexical scope modifiers 212import Kernel, except: [spawn: 1, +: 2, /: 2, Unless: 2] 213alias Long.Module.Name, as: N0men123_and4 214use Bitwise 215 2164 &&& 5 2172 <<< 3 218 219# Protocols 220defprotocol Useless do 221 def func1(this) 222 def func2(that) 223end 224 225defimpl Useless, for: Atom do 226end 227 228# Exceptions 229defmodule NotAnError do 230 defexception [:message] 231end 232 233raise NotAnError, message: "This is not an error" 234