Skip to content

Instantly share code, notes, and snippets.

@jonathanyeong
jonathanyeong / elixir-pipe-operator.ex
Last active September 25, 2022 19:56
Code Snippet for Elixir Conf Talk 2020
defmacro left |> right do
[{h, _} | t] = Macro.unpipe({:|>, [], [left, right]})
fun = fn {x, pos}, acc ->
Macro.pipe(acc, x, pos)
end
:lists.foldl(fun, h, t)
end
@aleDsz
aleDsz / case.ex
Last active September 25, 2022 19:54
Elixir
case Modulo.funcao() do
{:ok, %User{role: "admin"} = user} ->
user |> faca_algo_com_admin()
{:ok, %User{role: "moderator"} = user} ->
user |> faca_algo_com_moderator()
{:ok, %User{role: "user"} = user} ->
user |> faca_algo_com_user()
@kudosqujo
kudosqujo / alias_require_use.ex
Last active September 25, 2022 19:54
[Elixir Notes] #elixir
# Alias the module so it can be called as Bar instead of Foo.Bar
alias Foo.Bar, as: Bar
alias MyApp.{Foo, Bar, Baz}
# Require the module in order to use its macros
require Foo
# Import functions from Foo so they can be called without the `Foo.` prefix
import Foo
@alexandreservian
alexandreservian / usando-stacksupervisor-iex.ex
Created March 24, 2020 19:45
Usando StackSupervisor no iex
iex> StackSupervisor.start_link
{:ok, #PID<0.230.0>}
iex> StackSupervisor.which_children
[
{ProcessInElixir.StackNormal, #PID<0.231.0>, :worker,
[ProcessInElixir.StackNormal]}
]
iex> ProcessInElixir.StackNormal.push pid(0,231,0), "Japão"
{:push, "Japão"}
iex> ProcessInElixir.StackNormal.show pid(0,231,0)
@Exadra37
Exadra37 / tags_unify.ex
Last active September 25, 2022 19:55
Elixir Tags Unify module
defmodule Utils.TagsUnify do
@moduledoc """
This module unifies the given tags, by removing duplicates and merging the
ones that are similar.
## Examples
iex> "My Elixir Status, my-elixir-status, myelixirstatus, MyElixirStatus" |> Utils.TagsUnify.string()
"MyElixirStatus"
@devstopfix
devstopfix / strings.exs
Created December 3, 2019 10:49
String processing
dataset =
[
[lang: "C", loc: 79, t: {4, :s}, mem: {40, :MB}],
[lang: "Python", loc: 11, t: {4, :s}, mem: {150, :MB}],
[lang: "Ruby", loc: 9, t: {17, :s}, mem: {3, :GB}],
[lang: "Elixir", loc: 11, t: {120, :s}, mem: {700, :MB}],
[lang: "Elixir ETS", loc: 17, t: {40, :s}, mem: {730, :MB}],
[lang: "Elixir Regex", loc: 17, t: {70, :s}, mem: {730, :MB}],
[lang: "Elixir String.split", loc: 11, t: {30, :s}, mem: {730, :MB}],
[lang: "Elixir String.split pre-compiled", loc: 17, t: {29, :s}, mem: {730, :MB}],
iex(8)> code = quote do
...(8)> defmodule Util do
...(8)> def full_name(%User{} = user) do
...(8)> "#{user.first_name} #{user.last_name}"
...(8)> end
...(8)> end
...(8)> end
{:defmodule, [context: Elixir, import: Kernel],
[
{:__aliases__, [alias: false], [:Util]},
@hrubi
hrubi / scoping.ex
Last active September 25, 2022 19:57
Elixir scoping changes
leak1.ex:
defmodule Leak1 do
def leak do
x = 0
if true do
x = 1
end
x
end
end
@cristian-sr
cristian-sr / elixir.exs
Created December 25, 2018 22:34
Elixir
Hello world
# hello.exs
defmodule Greeter do
def greet(name) do
message = "Hello, " <> name <> "!"
IO.puts message
end
end
@horvand
horvand / majom.ex
Created October 2, 2018 13:41
elixir-@
defmodule Majom do
@constseq :lists.seq(1,10,2)
def f() do
IO.inspect(quote do
@constseq
end)
IO.inspect(@constseq)
end