Elixir Tips and Tricks That You Might Not Know Of
defmodule App.Team do
use App, :schema
...
defmodule Queue do | |
def insert(queue, item), do: :queue.in(item, queue) | |
def insert_last(queue, item), do: :queue.in_r(item, queue) | |
def member?(queue, item), do: :queue.member(item, queue) | |
def filter(queue, fun), do: :queue.filter(fun, queue) | |
def split(queue, n) do |
function new = () => ({ | |
_struct: "Conn", | |
host: null, | |
path: null, | |
port: null, | |
scheme: null, | |
params: {}, | |
requestHeaders: [], | |
responseHeaders: [], | |
responseBody: {}, |
defmodule Shopify.Cache do | |
use Maxwell.Middleware | |
alias :timer, as: Timer | |
@ttl Timer.minutes(5) | |
def call(conn, next, opts), do: get!(conn, fn _ -> super(conn, next, opts) end) | |
def get!(key, fallback \\ nil) do |
initial = %{sunday: 0, monday: 0, tuesday: 0, wednesday: 0, thursday: 0, friday: 0, saturday: 0} | |
from(o in Order, | |
where: o.inserted_at > fragment("DATE_TRUNC('week', CURRENT_TIMESTAMP)"), | |
group_by: fragment("EXTRACT(DOW FROM ?)", o.inserted_at), | |
order_by: fragment("EXTRACT(DOW FROM ?)", o.inserted_at), | |
select: {fragment("EXTRACT(DOW FROM ?)", o.inserted_at), count(o.id)} | |
) | |
|> Repo.all() | |
|> Enum.reduce(initial, fn |
defmodule Blog.Attachment do | |
use Blog, :schema | |
@primary_key {:id, :binary_id, autogenerate: true} | |
@storage_path "attachments" | |
schema "attachments" do | |
field :file, :any, virtual: true | |
belongs_to :post, Post |
iex> alias Data.Post
iex> Post |> Post.by_status(:published) |> Repo.all()
iex> Post |> Post.by_status([:draft, :published]) |> Repo.all()
Ecto.Repo by default doesn't allow overrides. So when you need to compose functions but maintain an API parity with the Ecto.Repo. You'd need to compose the functions in another module and things can get messy really fast that way.
Can't invoke super
== Compilation error on file lib/app/repo.ex ==
** (CompileError) lib/app/repo.ex:6: no super defined for all/2 in module App.Repo. Overridable functions available are:
I recently had several days of extremely frustrating experiences with service workers. Here are a few things I've since learned which would have made my life much easier but which isn't particularly obvious from most of the blog posts and videos I've seen.
I'll add to this list over time – suggested additions welcome in the comments or via twitter.com/rich_harris.
Chrome 51 has some pretty wild behaviour related to console.log
in service workers. Canary doesn't, and it has a load of really good service worker related stuff in devtools.