Skip to content

Instantly share code, notes, and snippets.

View danj3's full-sized avatar

Dan Janowski danj3

View GitHub Profile
@danj3
danj3 / elmdb_stage.ex
Created March 26, 2018 17:45
Elixir: GenStage producer from an :elmdb (LMDB) database using a cursor for linear sequence
defmodule ElmdbStage do
@moduledoc """
Create a GenStage for an :elmdb based LMDB database cursor.
This is for linearly reading from a db
"""
use GenStage
def start_link( env, db ) do
GenStage.start_link( __MODULE__, { env, db } )
end
@danj3
danj3 / hash_signature.ex
Last active March 26, 2018 14:30
Elixir: create an armored sha256 hash of an input
defmodule HashSignature do
@doc """
encode input to a sha256 and output as an armored string.
results should be fully portable and consistent with other
tools.
"""
def get( input ) do
:crypto.hash( :sha256, input )
|> :crypto.bytes_to_integer
|> (fn i -> :io_lib.format("~64.16.0b",[i]) |> hd end ).()
@danj3
danj3 / time_agent.ex
Created March 26, 2018 13:25
Elixir agent for active tracking execution time
defmodule TimeAgent do
def start_link do
Agent.start_link( fn -> { Map.new, Map.new } end, name: __MODULE__ )
end
def time_run( m, f, a ) do
{ us, result } = :timer.tc( m, f, a )
Agent.cast( __MODULE__, __MODULE__, :add_sample, [ to_string(m) <> "." <> to_string(f), us ] )
result
@danj3
danj3 / term_round_trip.exs
Created March 15, 2018 00:34
Elixir: example of turning a term to a string and a string to a term
defmodule TermRoundTrip do
@moduledoc """
Convert a term to a readable string then back to a term
"""
def term_to_binary( term ) do
:io_lib.format("~p.~n", [ term ] )
|> :erlang.iolist_to_binary
end
@danj3
danj3 / day4.ex
Created December 19, 2017 04:53
Advent of Code (AOC) 2017 Day 4
defmodule Day4 do
def parser( input ) do
String.split( input, "\n" )
|> Enum.reduce( [], fn "", acc -> acc; line,acc -> [ String.split(line," ") | acc ] end )
end
def start1( input ) do
parser( input ) |> step1a( [valid: 0, invalid: 0] )
end
@danj3
danj3 / day3.ex
Created December 15, 2017 14:00
Advent of Code (AOC) 2017 Day 3
defmodule Day3 do
# This does more work than required but is still O(1)
# diagonal low right is even square root
def start1( val ) do
col_height = :math.sqrt( val ) |> Float.ceil |> trunc
col_height = if Integer.mod( col_height, 2 ) == 0, do: col_height+1, else: col_height
low_right_corner = :math.pow( col_height, 2 ) |> trunc
distance = low_right_corner - val
ring_length = col_height * 4 - 4
@danj3
danj3 / day2.ex
Created December 12, 2017 01:41
Advent of Code 2017 Day 2
defmodule Day2 do
def parser( input ) do
String.split( input, "\n" )
|> Enum.map( fn k ->
String.split( k, "\t")
|> Enum.map( &String.to_integer/1 )
end )
end
def start1( input ) do
input
@danj3
danj3 / timeout_example.ex
Created October 18, 2017 00:39
Elixir GenServer use of timeout as 3rd element return from init()
defmodule TimeoutExample do
use GenServer
def init(_args), do: {:ok, 0, 5_000}
def handle_info(:timeout, state) do
IO.inspect(state)
{:noreply, state+1, 5_000 + state*1000}
end
end
iex> {:ok, p} = GenServer.start(TimeoutExample, [])