Skip to content

Instantly share code, notes, and snippets.

@mmmries
mmmries / game_of_life_encoding.exs
Created January 17, 2020 17:21
A solution for how to encode a list of "on" cell locations into a game-of-life encoding
defmodule GOL do
def encode(list) do
encode(nil, list)
end
# Figure out the starting condition, whether we start "on" or "blank"
def encode(nil, [1 | rest]), do: encode({:on, 1, 1}, rest)
def encode(nil, rest), do: encode({:blank, 1}, rest)
# If we have a sequence of "on" cells we need to know when it started and whether we are consecutive
# with the last cell
@mmmries
mmmries / controller.ex
Last active April 20, 2020 13:12
Kick off job after saving changes
defmodule UserController do
def create(params) do
changeset = params
|> cast([:name, :email])
|> validate_required([:email])
|> validate_other_stuff()
WithSideEffects.create(changeset)
end
end
@mmmries
mmmries / association_loader.rb
Created June 2, 2020 23:41
Association Loader for ruby-graphql
# from https://github.com/Shopify/graphql-batch/blob/c84a5236ecb1c38fa275c76ee38017f30b7074be/examples/association_loader.rb
# This handles creating generic association loaders (see team_member_graph.rb for an example)
class AssociationLoader < GraphQL::Batch::Loader
def self.validate(model, association_name)
new(model, association_name)
nil
end
def initialize(model, association_name)
@model = model