Last active
May 17, 2022 14:58
-
-
Save skovsgaard/3f9112b78ae848ed524aedbd5acedc64 to your computer and use it in GitHub Desktop.
The content for the Hyperpolyglot entry on Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
elixir | |
====== | |
version used | |
------------ | |
1.2 | |
show version | |
------------ | |
$ elixir -v | |
grammar and invocation | |
====================== | |
hello world | |
----------- | |
$ cat ./hello.ex | |
IO.puts "Hello, World!" | |
compiler | |
-------- | |
bytecode compiler | |
----------------- | |
$ elixirc foo.ex | |
interpreter | |
----------- | |
$ elixir foo.exs | |
shebang | |
------- | |
$!/usr/bin/env elixir | |
repl | |
---- | |
$ iex | |
block delimiters | |
---------------- | |
do ; end | |
statement terminator | |
-------------------- | |
end-of-line comment | |
------------------- | |
# a comment | |
multiple line comment | |
--------------------- | |
docstring | |
--------- | |
@doc """ | |
a docstring | |
""" | |
variables and expressions | |
========================= | |
variable identifier | |
------------------- | |
# a bare, snake_cased word | |
i_am_a_variable | |
write once variable | |
------------------- | |
# previously unused variables on the left side of an equal sign will be assigned to values that make the left match the right | |
assignment | |
---------- | |
x = 3 | |
parallel assignment | |
------------------- | |
{x, y} = {1, 2} | |
[z, w] = [1, 2] | |
non-referential identifier | |
-------------------------- | |
# snake_cased bare word preceded by colon | |
:atom | |
quoted non-referential identifier | |
--------------------------------- | |
# an ascii string preceded by colon | |
:"an atom containing spaces" | |
conditional expression | |
---------------------- | |
if x > 0, do 1 | |
# or | |
if x > 0 do | |
1 | |
else | |
0 | |
end | |
case | |
---- | |
case x do | |
1 -> true | |
0 -> false | |
end | |
arithmetic and logic | |
==================== | |
true and false | |
-------------- | |
true false | |
falsehoods | |
---------- | |
false nil | |
logical operators | |
----------------- | |
and or || && | |
short circuit operators | |
----------------------- | |
relational operators | |
-------------------- | |
== === >= <= != !== | |
arithmetic operators | |
-------------------- | |
+ - / * div rem | |
unary negation | |
-------------- | |
-4 | |
integer division | |
---------------- | |
div 7, 3 | |
integer division by zero | |
------------------------ | |
bad argument in arithmetic expression | |
float division | |
-------------- | |
7 / 3 | |
float division by zero | |
---------------------- | |
bad argument in arithmetic expression | |
power | |
----- | |
:math.pow(2, 32) | |
sqrt | |
---- | |
:math.sqrt(2) | |
sqrt - 1 | |
-------- | |
bad argument in arithmetic expression | |
transcendental functions | |
------------------------ | |
:math.exp :math.log :math.sin :math.cos :math.tan :math.asin :math.acos :math.atan :math.atan2 | |
float truncation | |
---------------- | |
trunc round | |
absolute value | |
-------------- | |
abs(-3) | |
abs(-3.2) | |
integer overflow | |
---------------- | |
float literal with exponent | |
--------------------------- | |
2.0e2 | |
-2.0e-2 | |
random number | |
------------- | |
:random.uniform | |
:random.uniform(100) | |
random seed | |
----------- | |
:random.seed(17, 17, 17) | |
result of not seeding | |
--------------------- | |
interpreter uses same seed at startup. | |
bit operators | |
------------- | |
import Bitwise | |
5 <<< 1 | |
5 >>> 1 | |
5 &&& 1 | |
5 ||| 1 | |
5 ^^^ 1 | |
~~~5 | |
binary, octal, and hex literals | |
------------------------------- | |
0b101010 | |
0o52 | |
0x2A | |
strings | |
======= | |
string literal | |
-------------- | |
"don't say \"no\"" | |
newline in literal | |
------------------ | |
yes, in the triple quoted multi line strings. \n can also be used | |
""" | |
multi | |
line | |
string | |
""" | |
character escapes | |
----------------- | |
\" \' \\ \a \b \d \e \f \n \r \s \t \v \0 \xDD \uDDDD \u{D...} # where D is a digit | |
concatenate | |
----------- | |
"one " <> "two " <> "three" | |
replicate | |
--------- | |
:binary.copy("-", 80) | |
trim | |
---- | |
String.trim(" lorem ") | |
pad | |
--- | |
String.pad_leading("lorem", 10, ["$"]) | |
String.pad_trailing("lorem", 10, ["$"]) | |
number to string | |
---------------- | |
Integer.to_string(8) | |
string to number | |
---------------- | |
String.to_integer("8") | |
atom to string | |
-------------- | |
Atom.to_string(:foo) | |
string to atom | |
-------------- | |
String.to_atom("foo") | |
translate case | |
-------------- | |
String.downcase("LOREM") | |
String.upcase("lorem") | |
String.capitalize("lorem") | |
split | |
----- | |
String.split("foo bar baz") | |
Regex.split(~r/ /, "foo bar baz") | |
join | |
---- | |
String.join(["foo", "bar", "baz"]) | |
character literal | |
----------------- | |
'A' | |
string length | |
------------- | |
String.length("hello") | |
index of substring | |
------------------ | |
:binary.match("foo bar baz", "bar") | |
extract substring | |
----------------- | |
String.slice("foo bar baz", 5..3) | |
chr and ord | |
----------- | |
List.to_string([65]) | |
Enum.at('A', 0) | |
regular expressions | |
=================== | |
match test | |
---------- | |
Regex.match?(~r/.*1999/, "it's 2000") | |
String.match?("it's 2000", ~r/.*1999/) | |
substitution | |
------------ | |
Regex.replace(~r/mi/, "do re mi mi mi", "ma") | |
String.replace("do re mi mi mi", ~r/mi/, "ma") | |
dates and time | |
============== | |
current date/time | |
----------------- | |
DateTime.utc_now | |
lists | |
===== | |
list literal | |
------------ | |
[1, 2, 3] | |
cons | |
---- | |
[4 | [3, 2, 1]] | |
head | |
---- | |
hd([1,2,3]) | |
[head|_] = [1,2,3] | |
List.first [1,2,3] | |
tail | |
---- | |
tl([1,2,3]) | |
[_|tail] = [1,2,3] | |
length | |
------ | |
length([1,2,3]) | |
Enum.count([1,2,3]) | |
append | |
------ | |
[1,2] ++ [3,4] | |
sort | |
---- | |
Enum.sort([1,3,2,4]) | |
reverse | |
------- | |
Enum.reverse([1,2,3,4]) | |
membership | |
---------- | |
Enum.member?([1,2,3], 1) | |
zip | |
--- | |
Enum.zip([1,2,3], ["a","b","c"]) | |
map | |
--- | |
Enum.map([1,2,3], fn x -> x * x end) | |
filter | |
------ | |
Enum.filter([1,2,3], fn x -> x > 2 end) | |
foldl | |
----- | |
Enum.reduce([1,2,3], fn x, y -> x - y end) | |
foldr | |
----- | |
tuples | |
====== | |
tuple literal | |
------------- | |
{1, "foo", 3.14} | |
tuple element access | |
-------------------- | |
elem({1, "foo" 3.14}, 1) | |
tuple length | |
------------ | |
tuple_size({1,"foo",3}) | |
algebraic data types | |
==================== | |
record | |
------ | |
functions | |
========= | |
function definition | |
------------------- | |
def factorial(0), do: 1 | |
def factorial(n), do: n * factorial(n-1) | |
function definition with guards | |
------------------------------- | |
def factorial(n) when n > 0, do: n * factorial(n-1) | |
def factorial(0), do: 1 | |
anonymous function | |
------------------ | |
fn(x,y) -> x + y end | |
piecewise defined anonymous function | |
------------------------------------ | |
execution control | |
================= | |
if | |
--- | |
if x == 0 do | |
IO.puts "no hits" | |
else | |
IO.puts "hits" | |
end | |
for | |
--- | |
for x <- 1..10, do: IO.puts(x) | |
try/catch | |
--------- | |
try do | |
div(7, 0) | |
:ok | |
catch | |
_, _ -> :failed | |
end | |
receive message | |
--------------- | |
receive do | |
{:ok, some_value} -> :yay | |
{:error, val} when val > 0 -> :meh | |
{:error, oh_no} -> :aw | |
end | |
spawn_process | |
------------- | |
pid = spawn(fn -> :ok end) | |
send message | |
------------ | |
send(pid, :hello) | |
list processes | |
-------------- | |
Process.list | |
file handles | |
============ | |
open file for reading | |
--------------------- | |
File.open("foo.txt", [:read]) | |
open file for writing | |
--------------------- | |
File.open("foo.txt", [:write]) | |
close file | |
---------- | |
File.close(io_device) | |
read line | |
--------- | |
read character | |
-------------- | |
read term | |
--------- | |
write character | |
--------------- | |
write term | |
---------- | |
printf | |
------ | |
files | |
===== | |
file test, regular file test | |
---------------------------- | |
File.regular? "/etc/hosts" | |
dirname and basename | |
-------------------- | |
File.dirname "/etc/hosts" | |
File.basename "/etc/hosts" | |
absolute pathname | |
----------------- | |
File.absname "/etc/hosts" | |
glob paths | |
---------- | |
make directory | |
-------------- | |
File.mkdir_p("/tmp/foo/bar") | |
directory test | |
-------------- | |
File.dir? "/tmp" | |
processes and environment | |
========================= | |
command line arguments | |
---------------------- | |
System.argv | |
program name | |
------------ | |
exit | |
---- | |
System.halt | |
libraries and namespaces | |
======================== | |
load file | |
--------- | |
define namespace | |
---------------- | |
defmodule Factorial do | |
end | |
compile namespace | |
----------------- | |
c("factorial.ex") | |
use function in namespace | |
------------------------- | |
Factorial.fact 7 | |
reflection | |
========== | |
inspect namespace | |
----------------- | |
repl | |
==== | |
help | |
---- | |
h | |
clear variable | |
-------------- | |
# unnecessary as Elixir doesn't have single assignment | |
clear all variables | |
------------------- | |
display processes | |
----------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment