elixir: try to get a hello world in

This commit is contained in:
Rico Sta. Cruz 2017-08-30 01:24:56 +08:00
parent 382f085c93
commit e1f3df96e2
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 96 additions and 1 deletions

View File

@ -7,7 +7,102 @@ updated: 201708
weight: -10
---
## Reference
## Getting started
{: .-three-column}
### Hello world
{: .-prime}
```elixir
# hello.exs
defmodule Greeter do
def greet(name) do
IO.puts "Hello, " <> name <> "!"
end
end
Greeter.greet("world")
```
```bash
elixir hello.exs
# Hello, world!
```
{: .-setup}
### Variables
```elixir
age = 23
```
### Maps
```elixir
user = %{
name: "John",
city: "Melbourne"
}
```
```elixir
IO.puts "Hello, " <> user.name
```
{: .-setup}
### Lists
```elixir
users = [ "Tom", "Dick", "Harry" ]
```
{: data-line="1"}
```elixir
Enum.map(user, fn user ->
IO.puts "Hello " <> user
end)
```
### Piping
```elixir
source
|> transform(:hello)
|> print()
```
{: data-line="2,3"}
```elixir
# Same as:
print(transform(source, :hello))
```
These two are equivalent.
### Pattern matching
```elixir
user = %{name: "Tom", age: 23}
%{name: username} = user
```
{: data-line="2"}
This sets `username` to `"Tom"`.
### Pattern matching in functions
```elixir
def greet(%{name: username}) do
IO.puts "Hello, " <> username
end
user = %{name: "Tom", age: 23}
```
{: data-line="1"}
Pattern matching works in function parameters too.
## Types
### Primitives