Skip to content

Instantly share code, notes, and snippets.

@mgwidmann
mgwidmann / hotcode.ex
Last active September 25, 2022 19:57
Hot code swapping
# To show hot code uploading, we first need to build a simple phoenix project so we can see it happen in real time.
# Start by making a new phoenix project
$ mix phoenix.new hotcode
# Go into the directory
$ cd hotcode
# Add exrm dependency to mix.exs file
{:exrm, "~> 1.0.3"}
@mgwidmann
mgwidmann / supervisor.ex
Last active September 25, 2022 19:58
Overview of the Supervision system
# The golden trinity of Erlang is the secret sauce behind what makes
# Elixir a strong choice for any backend application and/or system.
# https://tkowal.wordpress.com/2015/10/20/failing-fast-and-slow-in-erlang-and-elixir/
# The supervision and worker system, commonly referred to as OTP (Open
# Telecom Platform, which has nothing to do with telephony), is what
# gives Erlang/Elixir applications them a robust "self-healing" type
# property that makes them extremely fault tolerant.
@todd
todd / mix.exs
Created April 25, 2016 02:32
Redis Keyevents -> RabbitMQ Messages
# mix.exs
defmodule RedisRabbitElixir.Mixfile do
use Mix.Project
def project do
[app: :redis_rabbit_elixir,
version: "0.0.1",
elixir: "~> 1.2",
build_embedded: Mix.env == :prod,
@moklett
moklett / task1.exs
Last active May 7, 2024 09:59
Elixir Task - Crash Handling
# This demonstrates that, when using async/await, a crash in the task will crash the caller
defmodule Tasker do
def good(message) do
IO.puts message
end
def bad(message) do
IO.puts message
raise "I'm BAD!"
end
@wbprice
wbprice / MySort.exs
Created May 1, 2016 00:02
Troubleshooting - Sort Contents of Elixir Enumerables
"""
Given that "Sample.txt" exists and looks like: https://dl.dropboxusercontent.com/u/2736799/learn-elixir/exercises/sample.txt"
"""
{:ok, words} = File.read "Sample.txt"
splitWords = String.split(words, "\n\n")
streamSplitWords = Stream.take_every(splitWords, 2)
|> Stream.filter(&String.first(&1) == "d")
@Jazznight
Jazznight / immutable_map.ex
Last active September 25, 2022 19:57
[Elixir] Immutable Map operation
Imagine you have the following structure:
```elixir
iex> users = [
john: %{name: "John", age: 27, languages: ["Erlang", "Ruby", "Elixir"]},
mary: %{name: "Mary", age: 29, languages: ["Elixir", "F#", "Clojure"]}
]
```
We have a keyword list of users where each value is a map containing the name, age and a list of programming languages each user likes. If we wanted to access the age for john, we could write:
@mileszs
mileszs / authenticate_job_poster.ex
Created October 4, 2016 16:50
Plug for authenticating via session or token
defmodule ElixirJobBoard.Plugs.AuthenticateJobPoster do
import Plug.Conn
import Phoenix.Controller, only: [put_flash: 3, redirect: 2]
alias ElixirJobBoard.Repo
alias ElixirJobBoard.User
def init(opts), do: opts
def call(conn, _opts) do
defmodule LearningElixir do
use Application
# See http://elixir-lang.org/docs/stable/elixir/Application.html
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec, warn: false
children = [
# Define workers and child supervisors to be supervised
# The call 'Elixir.NAClaims.ClaimAgent':add_claim_field(
# cpid@1::pid(),
# cf@4::#{'__struct__':='Elixir.NAClaims.ClaimField', ...},
# fd@1::#{'__struct__':='Elixir.NAClaims.FieldDefs.FieldDef', ...},
# opts@1::any())
# will never return since the success typing is
# (
# pid(),
# #{'__struct__':='Elixir.NAClaims.ClaimField', ...},
# #{'__struct__':='Elixir.NAClaims.FieldDefs.FieldDef', ...},
defmodule Kipatest.AccessToken do
use Kipatest.Web, :model
@type t :: %{__struct__: atom}
@primary_key {:id, Ecto.UUID, autogenerate: true}
schema "access_tokens" do
field :is_valid, :boolean, default: true
field :refresh_token, Ecto.UUID, autogenerate: true
field :expired_at, Timex.Ecto.DateTime