Update Elixir

This commit is contained in:
Rico Sta. Cruz 2017-08-24 23:27:32 +08:00
parent caac5559cf
commit 6f66b06c89
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
2 changed files with 127 additions and 70 deletions

View File

@ -124,7 +124,7 @@
} }
/* /*
* Syntax highlight * Syntax kighlight
*/ */
.token { .token {
@ -160,6 +160,7 @@
color: $cA-3; color: $cA-3;
} }
&.comment,
&.punctuation, &.punctuation,
&.operator { &.operator {
color: $cM; color: $cM;

194
elixir.md
View File

@ -1,29 +1,26 @@
--- ---
title: Elixir title: Elixir
category: Development category: Development
layout: 2017/sheet
--- ---
## Importing ## Reference
```elixir ### Primitives
require Redux # compiles a module
import Redux # compiles, and you can use without the `Redux.` prefix
use Redux # compiles, and runs Redux.__using__/1 | `nil` | Nil/null |
use Redux, async: true | `true` _/_ `false` | Boolean |
| `'hello'` | Char list |
| `<<2, 3>>` | Binary |
| `"hello"` | Binary string |
| `:hello` | Atom |
| `[a, b]` | List |
| `{a, b}` | Tuple |
| `%{a: "hello"}` | Map |
| `%MyStruct{a: "hello"}` | Struct |
| `fn -> ... end` | Function |
import Redux, only: [duplicate: 2] ### Type checks
import Redux, only: :functions
import Redux, only: :macros
alias Foo.Bar, as: Bar
alias Foo.Bar # same as above
alias Foo.{Bar, Baz}
import Foo.{Bar, Baz}
```
## Type checks
```elixir ```elixir
is_atom/1 is_atom/1
@ -33,12 +30,16 @@ is_function/1
is_function/2 is_function/2
is_integer/1 is_integer/1
is_float/1 is_float/1
```
```elixir
is_binary/1 is_binary/1
is_list/1 is_list/1
is_map/1 is_map/1
is_tuple/1 is_tuple/1
```
```elixir
is_nil/1 is_nil/1
is_number/1 is_number/1
is_pid/1 is_pid/1
@ -56,16 +57,40 @@ left <> right # concat string/binary
left =~ right # regexp left =~ right # regexp
``` ```
### Inspecting ### Importing
```elixir ```elixir
inspect(arg, opts \\ []) require Redux # compiles a module
import Redux # compiles, and you can use without the `Redux.` prefix
use Redux # compiles, and runs Redux.__using__/1
use Redux, async: true
import Redux, only: [duplicate: 2]
import Redux, only: :functions
import Redux, only: :macros
import Foo.{Bar, Baz}
```
### Aliases
```elixir
alias Foo.Bar, as: Bar
alias Foo.Bar # same as above
alias Foo.{Bar, Baz}
``` ```
## String ## String
### Functions
```elixir ```elixir
import String import String
```
```elixir
str = "hello" str = "hello"
str |> length() #=> 5 str |> length() #=> 5
str |> codepoints() #=> ["h", "e", "l", "l", "o"] str |> codepoints() #=> ["h", "e", "l", "l", "o"]
@ -75,19 +100,33 @@ str |> capitalize() #=> "Hello"
str |> match(regex) str |> match(regex)
``` ```
### Inspecting objects
```elixir
inspect(object, opts \\ [])
```
```elixir
value |> IO.inspect()
```
## Numbers ## Numbers
### Operations
```elixir ```elixir
abs(n) abs(n)
round(n)
rem(a, b) # remainder (modulo) rem(a, b) # remainder (modulo)
div(a, b) # integer division div(a, b) # integer division
round(n)
``` ```
### Float ### Float
```elixir ```elixir
import Float import Float
```
```elixir
n = 10.3 n = 10.3
n |> ceil() #=> 11.0 n |> ceil() #=> 11.0
n |> ceil(2) #=> 11.30 n |> ceil(2) #=> 11.30
@ -101,17 +140,20 @@ Float.parse("34") #=> { 34.0, "" }
```elixir ```elixir
import Integer import Integer
```
```elixir
n = 12 n = 12
digits(n) #=> [1, 2] n |> digits() #=> [1, 2]
to_char_list(n) #=> '12' n |> to_char_list() #=> '12'
to_string(n) n |> to_string() #=> "12"
is_even(n) n |> is_even()
is_odd(n) n |> is_odd()
# Different base: # Different base:
digits(n, 2) #=> [1, 1, 0, 0] n |> digits(2) #=> [1, 1, 0, 0]
to_char_list(n, 2) #=> '1100' n |> to_char_list(2) #=> '1100'
to_string(n, 2) n |> to_string(2) #=> "1100"
parse("12") #=> 12 parse("12") #=> 12
undigits([1, 2]) #=> 12 undigits([1, 2]) #=> 12
@ -129,42 +171,46 @@ Float.to_string(34.1, [decimals: 2, compact: true]) #=> "34.1"
## Map ## Map
### Defining
```elixir ```elixir
map = %{name: "hi"} # atom keys (:name) map = %{name: "hi"} # atom keys (:name)
map = %{"name" => "hi"} # string keys ("name") map = %{"name" => "hi"} # string keys ("name")
``` ```
### Updating ### Updating
```elixir ```elixir
import Map import Map
map = %{map | name: "yo"} # key must exist map = %{map | name: "yo"} # key must exist
put(map, :id, 2) #=> %{id: 2, name: "hi"} map |> put(:id, 2) #=> %{id: 2, name: "hi"}
put_new(map, :id, 2) # only if `id` doesn't exist (`||=`) map |> put_new(:id, 2) # only if `id` doesn't exist (`||=`)
put(map, :b, "Banana") map |> put(:b, "Banana")
merge(map, %{b: "Banana"}) map |> merge(%{b: "Banana"})
update(map, :a, &(&1 + 1)) map |> update(:a, &(&1 + 1))
update(map, :a, fun a -> a + 1 end) map |> update(:a, fun a -> a + 1 end)
{old, new} = get_and_update(map, :a, &(&1 || "default")) {old, new} = map |> get_and_update(:a, &(&1 || "default"))
``` ```
### Deleting ### Deleting
```elixir ```elixir
delete(map, :name) #=> "hi" map |> delete(:name) #=> "hi"
pop(map, :name) #=> %{id: 1} map |> pop(:name) #=> %{id: 1}
``` ```
### Reading ### Reading
```elixir ```elixir
get(map, :id) #=> 1 map |> get(:id) #=> 1
keys(map) #=> [:id, :name] map |> keys() #=> [:id, :name]
values(map) #=> [1, "hi"] map |> values() #=> [1, "hi"]
to_list(map) #=> [id: 1, name: "hi"] map |> to_list() #=> [id: 1, name: "hi"]
#=> [{:id, 1}, {:name, "hi"}] #=> [{:id, 1}, {:name, "hi"}]
``` ```
@ -186,8 +232,6 @@ Map.new([:a, :b], fn x -> {x, x} end) #=> %{a: :a, b: :b}
## List ## List
Also see [Enum](#enum).
```js ```js
import List import List
list = [ 1, 2, 3, 4 ] list = [ 1, 2, 3, 4 ]
@ -201,10 +245,12 @@ flatten(list)
flatten(list, tail) flatten(list, tail)
``` ```
Also see [Enum](#enum).
## Enum ## Enum
```elixir ```elixir
# consider streams instead
import Enum import Enum
# High-order # High-order
@ -223,14 +269,18 @@ any?(list) #=> true
concat(list, [:d]) #=> [:d] concat(list, [:d]) #=> [:d]
``` ```
Also, consider streams instead.
## Tuples ## Tuples
### Tuples
```elixir ```elixir
tuple = { :a, :b } tuple = { :a, :b }
elem(tuple, 1) # like tuple[1] tuple |> elem(1) # like tuple[1]
put_elem(tuple, index, value) tuple |> put_elem(index, value)
tuple_size(tuple) tuple |> tuple_size()
``` ```
### Keyword lists ### Keyword lists
@ -270,9 +320,17 @@ apply(fun, args)
apply(module, fun, args) apply(module, fun, args)
``` ```
## Syntax ### Function heads
### [Structs](http://elixir-lang.org/getting-started/structs.html) ```elixir
def join(a, b \\ nil)
def join(a, b) when is_nil(b) do: a
def join(a, b) do: a <> b
```
## Structs
### Structs
```elixir ```elixir
defmodule User do defmodule User do
@ -284,18 +342,12 @@ end
%User{}.struct #=> User %User{}.struct #=> User
``` ```
## Functions See: [Structs](http://elixir-lang.org/getting-started/structs.html)
### Function heads
```elixir
def join(a, b \\ nil)
def join(a, b) when is_nil(b) do: a
def join(a, b) do: a <> b
```
## Protocols ## Protocols
### Defining protocols
```elixir ```elixir
defprotocol Blank do defprotocol Blank do
@doc "Returns true if data is considered blank/empty" @doc "Returns true if data is considered blank/empty"
@ -330,6 +382,8 @@ end
## Comprehensions ## Comprehensions
### For
```elixir ```elixir
for n <- [1, 2, 3, 4], do: n * n for n <- [1, 2, 3, 4], do: n * n
for n <- 1..4, do: n * n for n <- 1..4, do: n * n
@ -356,7 +410,7 @@ for dir <- dirs,
end end
``` ```
## Modules ## Misc
### Metaprogramming ### Metaprogramming
@ -376,7 +430,7 @@ def on_def(_env, kind, name, args, guards, body)
def load_check def load_check
``` ```
## Regexp ### Regexp
```elixir ```elixir
exp = ~r/hello/ exp = ~r/hello/
@ -384,7 +438,7 @@ exp = ~r/hello/i
"hello world" =~ exp "hello world" =~ exp
``` ```
## [Sigils](http://elixir-lang.org/getting-started/sigils.html) ### Sigils
```elixir ```elixir
~r/regexp/ ~r/regexp/
@ -394,11 +448,10 @@ exp = ~r/hello/i
~c(char list) ~c(char list)
``` ```
Allowed chars: `/` `|` `"` `'` `(` `[` `{` `<` `"""` Allowed chars: `/` `|` `"` `'` `(` `[` `{` `<` `"""`.
See: [Sigils](http://elixir-lang.org/getting-started/sigils.html)
## [Typespecs](http://elixir-lang.org/getting-started/typespecs-and-behaviours.html) ### Type specs
Useful for [dialyzer](http://www.erlang.org/doc/man/dialyzer.html)
```elixir ```elixir
@spec round(number) :: integer @spec round(number) :: integer
@ -407,7 +460,10 @@ Useful for [dialyzer](http://www.erlang.org/doc/man/dialyzer.html)
@spec add(number, number) :: number_with_remark @spec add(number, number) :: number_with_remark
``` ```
## Behaviours Useful for [dialyzer](http://www.erlang.org/doc/man/dialyzer.html).
See: [Typespecs](http://elixir-lang.org/getting-started/typespecs-and-behaviours.html)
### Behaviours
```elixir ```elixir
defmodule Parser do defmodule Parser do
@ -425,9 +481,9 @@ defmodule JSONParser do
end end
``` ```
[Reference](http://elixir-lang.org/docs/stable/elixir/Module.html) See: [Module](http://elixir-lang.org/docs/stable/elixir/Module.html)
## References ## References
{: .-one-column}
- [Learn Elixir in Y minutes](https://learnxinyminutes.com/docs/elixir/) - [Learn Elixir in Y minutes](https://learnxinyminutes.com/docs/elixir/)