Skip to content

Instantly share code, notes, and snippets.

View andrewhao's full-sized avatar

Andrew Hao andrewhao

View GitHub Profile
@andrewhao
andrewhao / 01.ex
Last active November 25, 2024 21:33
Lightweight Elixir dependency injection
module MyApp.MyModule do
@my_collaborating_module Application.get_env(:my_app, :my_collaborating_module)
def perform() do
@my_collaborating_module.do_something()
end
end
@andrewhao
andrewhao / game.ex
Last active August 7, 2023 21:37
Dynamic Supervisors in Elixir
defmodule Game do
use GenServer
def init(game_id) do
{:ok, %{game_id: game_id}}
end
def start_link(game_id) do
GenServer.start_link(__MODULE__, game_id, name: {:global, "game:#{game_id}"})
end
@andrewhao
andrewhao / 1.ex
Last active April 1, 2018 02:19
Mocking interfaces in Elixir with Mox
module MyApp.Alexa do
def call do
# ...
get_country_and_zip_code(device_id, consent_token)
|> do_some_other_transformation
# ...
end
defp get_country_and_zip_code(device_id, consent_token) do
url =
@andrewhao
andrewhao / ValidatedForm.Model.elm
Last active October 12, 2017 23:32
Introduction to Elm
type alias Model =
{ name : String
, phone : String
}
model : Model
model =
{ name = ""
, phone = ""
@andrewhao
andrewhao / domain-messaging-new-way.rb
Last active May 27, 2018 06:29
Evented Rails - Decoupling complex domains in Rails with Domain Events
# -----------------------------
# Passenger (Experience) Domain
# -----------------------------
module Passenger
# app/domains/passenger/find_driver_match.rb
class FindDriverMatch
include Wisper::Publisher
def call(passenger, ride_requirements)
# Publishes the event (a fact), but lets someone else do the rest of the hard work.
@andrewhao
andrewhao / OriginalTodo.elm
Created June 20, 2017 18:22
Elm Study Sessions - Part 3
-- Todo.elm
import Html exposing (Html, Attribute, button, div, ul, li, h1, text, input)
import Html.Attributes exposing (..)
import Html.Events exposing (onClick, onInput)
main =
Html.beginnerProgram { model = model, view = view, update = update }
-- MODEL
@andrewhao
andrewhao / session_1.md
Last active May 12, 2017 20:58
Carbon Five Elm Study Sessions

Session 1

Welcome to the :c5: Elm study sessions! In this 45-minute session Zoe and Andrew are going to try to lead you through the basics of the Elm language.

You will leave this session with a decent understanding of the core language.

What is Elm?

Elm is a strongly-typed language developed by Evan Czaplicki (apparently, a high school classmate of Sue Anna's). It was a MIT senior thesis project of his (see his paper) in 2012.

@andrewhao
andrewhao / asynchronous_operation.rb
Last active December 19, 2016 17:48
Evented Rails
module Ridesharing
  class RidesController < ApplicationController
    def post
      # Hail a time-traveling Delorean: 
      HailDelorean.hail(current_user.id)
      render text: 'Hailing a cab, please wait for a response...'
    end
  end
end
@andrewhao
andrewhao / add_view_paths.rb
Last active January 25, 2023 18:31
Domain-oriented Rails file structures
module Ridesharing
class DriverController
append_view_path(‘app/domains’)
end
end
@andrewhao
andrewhao / -
Created September 8, 2016 18:17
<?xml version="1.0" encoding="utf-8" ?>
<!ELEMENT tsung (information?, clients, servers, monitoring?, load, options?, sessions)>
<!ELEMENT information (name|description|username|organisation)*>
<!ELEMENT name (#PCDATA)>
<!ELEMENT description (#PCDATA)>
<!ELEMENT username (#PCDATA)>
<!ELEMENT organisation (#PCDATA)>