Update
This commit is contained in:
parent
e670475731
commit
7ac4aa8e2d
139
elixir.md
139
elixir.md
|
@ -32,6 +32,7 @@ is_reference/1
|
|||
left != right # equal
|
||||
left !== right # match
|
||||
left ++ right # concat lists
|
||||
left <> right # concat string/binary
|
||||
left =~ right # regexp
|
||||
```
|
||||
|
||||
|
@ -76,20 +77,142 @@ Map.put(map, key, value)
|
|||
### String
|
||||
|
||||
```elixir
|
||||
String.length(str)
|
||||
String.codepoints(string)
|
||||
String.slice(str, 0..-1)
|
||||
String.split(str, " ")
|
||||
String.capitalize(str)
|
||||
String.match(string, regex)
|
||||
import String
|
||||
str = "hello"
|
||||
str |> length() #=> 5
|
||||
str |> codepoints() #=> ["h", "e", "l", "l", "o"]
|
||||
str |> slice(2..-1) #=> "llo"
|
||||
str |> split(" ") #=> ["hello"]
|
||||
str |> capitalize() #=> "Hello"
|
||||
str |> match(regex)
|
||||
```
|
||||
|
||||
### Float
|
||||
|
||||
```elixir
|
||||
import Float
|
||||
n = 10.3
|
||||
n |> ceil() #=> 11.0
|
||||
n |> ceil(2) #=> 11.30
|
||||
n |> to_string() #=> "1.030000+e01"
|
||||
n |> to_string([decimals: 2, compact: true])
|
||||
|
||||
Float.parse("34") #=> { 34.0, "" }
|
||||
```
|
||||
|
||||
### Integer
|
||||
|
||||
```elixir
|
||||
import Integer
|
||||
n = 12
|
||||
digits(n) #=> [1, 2]
|
||||
to_char_list(n) #=> '12'
|
||||
to_string(n)
|
||||
is_even(n)
|
||||
is_odd(n)
|
||||
|
||||
# Different base:
|
||||
digits(n, 2) #=> [1, 1, 0, 0]
|
||||
to_char_list(n, 2) #=> '1100'
|
||||
to_string(n, 2)
|
||||
|
||||
parse("12") #=> 12
|
||||
undigits([1, 2]) #=> 12
|
||||
```
|
||||
|
||||
### Type casting
|
||||
|
||||
```elixir
|
||||
Float.parse("34.1") #=> {34.1, ""}
|
||||
Integer.parse("34") #=> {34, ""}
|
||||
|
||||
Float.to_string(34.1) #=> "3.4100e+01"
|
||||
Float.to_string(34.1, [decimals: 2, compact: true]) #=> "34.1"
|
||||
```
|
||||
|
||||
### Map
|
||||
|
||||
```js
|
||||
import Map
|
||||
map = %{id: 1, name: "hi"}
|
||||
|
||||
delete(map, :name) #=> "hi"
|
||||
pop(map, :name) #=> %{id: 1}
|
||||
|
||||
put(map, :id, 2) #=> %{id: 2, name: "hi"}
|
||||
put_new(map, :id, 2) # only if `id` doesn't exist
|
||||
|
||||
get(map, :id) #=> 1
|
||||
keys(map) #=> [:id, :name]
|
||||
values(map) #=> [1, "hi"]
|
||||
|
||||
to_list(map) #=> [id: 1, name: "hi"]
|
||||
#=> [{:id, 1}, {:name, "hi"}]
|
||||
|
||||
merge(map, %{name: "hello"})
|
||||
|
||||
Map.new([{:b, 1}, {:a, 2}])
|
||||
Map.new([a: 1, b: 2])
|
||||
Map.new([:a, :b], fn x -> {x, x} end) #=> %{a: :a, b: :b}
|
||||
```
|
||||
|
||||
### List
|
||||
|
||||
Also see [Enum](#enum).
|
||||
|
||||
```js
|
||||
import List
|
||||
list = [ 1, 2, 3, 4 ]
|
||||
|
||||
first(list)
|
||||
last(list)
|
||||
|
||||
flatten(list)
|
||||
flatten(list, tail)
|
||||
```
|
||||
|
||||
### Enum
|
||||
|
||||
```elixir
|
||||
Enum.reduce(list, acc, fn)
|
||||
Enum.map(list, fn)
|
||||
# consider streams instead
|
||||
import Enum
|
||||
|
||||
# High-order
|
||||
reduce(list, acc, fn)
|
||||
map(list, fn)
|
||||
reject(list, fn)
|
||||
any?(list, fn)
|
||||
empty?(list, fn)
|
||||
|
||||
list = [:a, :b, :c]
|
||||
at(list, 0) #=> :a
|
||||
count(list) #=> 3
|
||||
empty?(list) #=> false
|
||||
any?(list) #=> true
|
||||
|
||||
concat(list, [:d]) #=> [:d]
|
||||
```
|
||||
|
||||
There's really way too many things, just see <https://learnxinyminutes.com/docs/elixir/>.
|
||||
|
||||
## Syntax
|
||||
|
||||
### [Structs](http://elixir-lang.org/getting-started/structs.html)
|
||||
|
||||
```elixir
|
||||
defmodule User do
|
||||
defstruct name: "", age: nil
|
||||
end
|
||||
|
||||
%User{name: "John", age: 20}
|
||||
```
|
||||
|
||||
### Functions
|
||||
|
||||
### Function heads
|
||||
|
||||
```elixir
|
||||
def join(a, b \\ nil)
|
||||
def join(a, b) when is_nil(b) do: a end
|
||||
def join(a, b) do: a <> b; end
|
||||
```
|
||||
|
|
|
@ -0,0 +1,79 @@
|
|||
---
|
||||
title: Phoenix: Migrations
|
||||
category: Elixir
|
||||
---
|
||||
|
||||
## Creating
|
||||
|
||||
```
|
||||
$ mix ecto.gen.migration add_posts_table
|
||||
creating priv/repo/migrations/20160602085927_add_posts_table.exs
|
||||
...
|
||||
|
||||
$ mix ecto.migrate
|
||||
$ mix ecto.rollback
|
||||
```
|
||||
|
||||
|
||||
## Tables
|
||||
|
||||
```elixir
|
||||
create table(:documents) do
|
||||
add :title, :string
|
||||
add :title, :string, size: 40
|
||||
add :title, :string, default: "Hello"
|
||||
add :title, :string, default: fragment("now()")
|
||||
add :title, :string, null: false
|
||||
add :body, :text
|
||||
add :age, :integer
|
||||
add :price, :float
|
||||
add :price, :float, precision: 10, scale: 2
|
||||
add :published_at, :datetime
|
||||
add :group_id, references(:groups)
|
||||
add :object, :json
|
||||
|
||||
timestamps # inserted_at and updated_at
|
||||
end
|
||||
|
||||
create_if_not_exists table(:documents) do: ... end
|
||||
```
|
||||
|
||||
```elixir
|
||||
alter table(:posts) do
|
||||
add :summary, :text
|
||||
modify :title, :text
|
||||
remove :views
|
||||
end
|
||||
```
|
||||
|
||||
```elixir
|
||||
rename table(:posts), :title, to: :summary
|
||||
rename table(:posts), to: table(:new_posts)
|
||||
```
|
||||
|
||||
```elixir
|
||||
drop table(:documents)
|
||||
drop_if_exists table(:documents)
|
||||
|
||||
table(:documents)
|
||||
table(:weather, prefix: :north_america)
|
||||
```
|
||||
|
||||
## Indices
|
||||
|
||||
```elixir
|
||||
create index(:posts, [:slug], concurrently: true)
|
||||
create unique_index(:posts, [:slug])
|
||||
drop index(:posts, [:name])
|
||||
```
|
||||
|
||||
## Execute
|
||||
|
||||
```elixir
|
||||
execute "UPDATE posts SET published_at = NULL"
|
||||
execute create: "posts", capped: true, size: 1024
|
||||
```
|
||||
|
||||
## References
|
||||
|
||||
* [Ecto.Migration](http://devdocs.io/phoenix/ecto/ecto.migration)
|
|
@ -0,0 +1,39 @@
|
|||
---
|
||||
title: Phoenix
|
||||
category: Elixir
|
||||
---
|
||||
|
||||
### Directory
|
||||
|
||||
```
|
||||
config/
|
||||
web/
|
||||
controllers/
|
||||
models/
|
||||
views/
|
||||
|
||||
templates/
|
||||
static/
|
||||
```
|
||||
|
||||
## Migrations
|
||||
|
||||
```
|
||||
$ mix ecto.gen.migration add_posts_table
|
||||
creating priv/repo/migrations/20160602085927_add_posts_table.exs
|
||||
...
|
||||
|
||||
$ mix ecto.migrate
|
||||
```
|
||||
|
||||
### [Ecto.Migration](http://devdocs.io/phoenix/ecto/ecto.migration)
|
||||
|
||||
```
|
||||
create index(:posts, [:slug], concurrently: true)
|
||||
create table(:documents) do
|
||||
add :body, :string
|
||||
add :deletion_key, :string
|
||||
|
||||
timestamps
|
||||
end
|
||||
```
|
Loading…
Reference in New Issue