Skip to content

Instantly share code, notes, and snippets.

defmodule MyApp.Hello do
@moduledoc """
This is the Hello module.
"""
@doc """
Says hello to the given `name`.
Returns `:ok`.
def pmap(collection, func) do
collection
|> Enum.map(&(Task.async(fn -> func.(&1) end)))
|> Enum.map(&Task.await/1)
end
@nyo16
nyo16 / pipe_example.ex
Last active December 6, 2018 15:45
Example pipe operator
def registration_changeset(model, params) do
model
|> cast(params, [:email, :password])
|> validate_required([:email, :password])
|> validate_format(:email, ~r/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/)
|> unique_constraint(:email)
|> validate_length(:password, min: 8)
|> update_password?()
end
@nyo16
nyo16 / example.ex
Last active December 15, 2018 16:51
example in elixir
def is_active(nil), do: {:error, :not_found}
def is_active(%User{accounts: []}), do: {:error, :no_active_account}
def is_active(%User{accounts: accounts} = user), do: {:ok, user}
def is_active(_), do: {:error, :something_happend}