Skip to content

Instantly share code, notes, and snippets.

View imranismail's full-sized avatar

Imran Ismail imranismail

View GitHub Profile
@imranismail
imranismail / queue.ex
Created April 10, 2017 08:24
Elixir Wrapper for Erlang Queue module
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
@imranismail
imranismail / conn.js
Last active March 29, 2017 02:32
Functional Example in JS
function new = () => ({
_struct: "Conn",
host: null,
path: null,
port: null,
scheme: null,
params: {},
requestHeaders: [],
responseHeaders: [],
responseBody: {},
@imranismail
imranismail / readme.md
Last active April 23, 2017 12:16
Tips on Elixir that you might not know of

Elixir Tips and Tricks That You Might Not Know Of

SaaS/Multitenant Migrations with Ecto

defmodule App.Team do
  use App, :schema

  ...
@imranismail
imranismail / cache.ex
Created March 21, 2017 02:33
Caching API with Maxwell and Cachex
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
@imranismail
imranismail / weekly_stats.ex
Last active March 8, 2017 08:42
Weekly Stats in Ecto
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
@imranismail
imranismail / attachment.ex
Created January 1, 2017 16:54
Media Prototype
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
@imranismail
imranismail / README.md
Last active January 5, 2017 15:37
Implementing Ruby's lonely operator using Elixir's custom infix function

Implementing Ruby's lonely operator using Elixir's custom infix function

iex> import ViewHelper
iex> user = %{name: "Imran", address: %{postcode: 33160}}
iex> user ~> :name
# "Imran"
iex> user ~> :address ~> :postcode
# 33160
iex> user ~> :address ~> :country
@imranismail
imranismail / README.md
Last active December 27, 2016 09:10
Enum for Ecto

Usage

iex> alias Data.Post
iex> Post |> Post.by_status(:published) |> Repo.all()
iex> Post |> Post.by_status([:draft, :published]) |> Repo.all()

References

https://github.com/gjaldon/ecto_enum

@imranismail
imranismail / README.md
Last active July 28, 2017 08:48
Repo composition

Repo Composition

Motive

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:
@imranismail
imranismail / service-workers.md
Created November 18, 2016 08:25 — forked from Rich-Harris/service-workers.md
Stuff I wish I'd known sooner about service workers

Stuff I wish I'd known sooner about service workers

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.

Use Canary for development instead of Chrome stable

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.