Last active
July 18, 2019 08:13
-
-
Save steven-solomon/f072ec2a3457cfa216d3045e541c8e26 to your computer and use it in GitHub Desktop.
Code for My First Week With Elixir as a Rubyist
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
defmodule OurServer do | |
use GenServer | |
# ... | |
def call({:message, caller_data}, _from, state) do | |
{:reply, caller_data, [caller_data]} | |
end | |
end |
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
defmodule OurServer do | |
use GenServer | |
# ... | |
def cast({:message, caller_data}, state) do | |
{:noreply, caller_data} | |
end | |
end |
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
iex> "Not a Match" = a # Throws an error | |
** (MatchError) no match of right hand side value: "Some String" |
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
iex> a = "Some String" # Binds string to a | |
"Some String" | |
iex> "Some String" = a # Matches a to the pattern | |
"Some String" |
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
iex> [head | tail] = [1, 2, 3, 4, 5] | |
iex> head | |
1 | |
iex> tail | |
[2, 3, 4, 5] |
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
iex> user = %{id: 1, login: %{user_name: 'bob114', password: 'Apple12Sauce!'}} | |
iex> %{id: id} = user | |
iex> id | |
1 | |
iex> %{login: %{user_name: user_name}} = user | |
iex> user_name | |
bob114 |
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
defmodule Example do | |
def any?([], _a), do: false # Matches when the first param is an empty list | |
end |
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
defmodule Example do | |
def any?([], _a), do: false | |
def any?([head | _tail], a) do | |
if head == a do | |
true | |
end | |
end | |
end |
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
defmodule Example do | |
def any?([], _a), do: false | |
def any?([head | tail], a) do | |
if head == a do | |
true | |
else | |
any?(tail, a) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment