Skip to content

Instantly share code, notes, and snippets.

@steven-solomon
Last active July 18, 2019 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steven-solomon/f072ec2a3457cfa216d3045e541c8e26 to your computer and use it in GitHub Desktop.
Save steven-solomon/f072ec2a3457cfa216d3045e541c8e26 to your computer and use it in GitHub Desktop.
Code for My First Week With Elixir as a Rubyist
defmodule OurServer do
use GenServer
# ...
def call({:message, caller_data}, _from, state) do
{:reply, caller_data, [caller_data]}
end
end
defmodule OurServer do
use GenServer
# ...
def cast({:message, caller_data}, state) do
{:noreply, caller_data}
end
end
iex> "Not a Match" = a # Throws an error
** (MatchError) no match of right hand side value: "Some String"
iex> a = "Some String" # Binds string to a
"Some String"
iex> "Some String" = a # Matches a to the pattern
"Some String"
iex> [head | tail] = [1, 2, 3, 4, 5]
iex> head
1
iex> tail
[2, 3, 4, 5]
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
defmodule Example do
def any?([], _a), do: false # Matches when the first param is an empty list
end
defmodule Example do
def any?([], _a), do: false
def any?([head | _tail], a) do
if head == a do
true
end
end
end
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