Skip to content

Instantly share code, notes, and snippets.

Avatar

Mike Buhot mbuhot

View GitHub Profile
@mbuhot
mbuhot / opaque_user.ex
Created Sep 14, 2020
Enforcing smart constructor in Elixir
View opaque_user.ex
defmodule Opaque.User do
@moduledoc """
Demonstrates a ridiculous method for enforcing that a type remains in a valid state using a closure and internal secret
"""
alias __MODULE__
# Ensure that the internal closure can only be called from within this module
@update_key :crypto.strong_rand_bytes(8)
@mbuhot
mbuhot / FSharpConverters.fs
Last active Nov 24, 2020
System.Text.Json converters for F# Option, List and Map types
View FSharpConverters.fs
namespace System.Text.Json
open System
open System.Collections.Generic
open System.Text.Json.Serialization
// Converts Option<T> to/from JSON by projecting to null or T
type OptionValueConverter<'T>() =
inherit JsonConverter<'T option>()
@mbuhot
mbuhot / text_process.ex
Created Jul 31, 2018
Fast text processing in elixir
View text_process.ex
defmodule TextProcess do
def generate_files(num_files, lines_per_file) do
Enum.each(1..num_files, fn i ->
filename = "data/test_#{i |> Integer.to_string() |> String.pad_leading(3, "0")}.csv"
File.open!(filename, [:write], fn file ->
Enum.each(1..lines_per_file, fn _ ->
IO.puts(file, line())
end)
end)
@mbuhot
mbuhot / exq_statsd_middleware.ex
Created Mar 26, 2018
StatsD middleware for Exq
View exq_statsd_middleware.ex
defmodule ExqStatsD do
@behaviour Exq.Middleware.Behaviour
alias Exq.Middleware.Pipeline
@moduledoc """
This behavior will send stats to statsd endpoint for monitoring.
"""
require Logger
View number_to_word.ex
defmodule NumberToWord do
@spec say(integer) :: String.t
def say(n), do: n |> say_io() |> IO.iodata_to_binary()
@spec say_io(integer) :: iodata
def say_io(1), do: "one"
def say_io(2), do: "two"
def say_io(3), do: "three"
def say_io(4), do: "four"
def say_io(5), do: "five"
View scrabble.ex
defmodule Scrabble do
def score("", n), do: n
def score("A" <> rest, n), do: score(rest, n + 1)
def score("B" <> rest, n), do: score(rest, n + 3)
def score("C" <> rest, n), do: score(rest, n + 3)
def score("D" <> rest, n), do: score(rest, n + 2)
def score("E" <> rest, n), do: score(rest, n + 1)
def score("F" <> rest, n), do: score(rest, n + 4)
def score("G" <> rest, n), do: score(rest, n + 2)
def score("H" <> rest, n), do: score(rest, n + 4)
@mbuhot
mbuhot / GenServerSwapOutTest.ex
Created Jun 15, 2016
Swap out genserver implementation per test
View GenServerSwapOutTest.ex
defmodule MyService do
def foo() do
GenServer.call(:my_service, :foo)
end
end
defmodule MyServiceFake do
use GenServer
def handle_call(:foo, _from, state) do
{:reply, :fake, state}
@mbuhot
mbuhot / index.md
Last active Jun 7, 2016
Bureaucrat output with Swagger info
View index.md
title search
Simple API
true

Simple API

A Simple API.

@mbuhot
mbuhot / swagger.json
Created Jun 7, 2016
Example swagger for bureaucrat integration
View swagger.json
{
"swagger": "2.0",
"info": {
"version": "1.0.0",
"title": "Simple API",
"description": "A Simple API.",
"termsOfService": "Use at your own risk!",
"contact": {
"name": "Joe P. Coder"
},
You can’t perform that action at this time.