Skip to content

Instantly share code, notes, and snippets.

View pragdave's full-sized avatar
🖍️
Alternating between Haskell and PDP-11 assembler

Dave Thomas pragdave

🖍️
Alternating between Haskell and PDP-11 assembler
View GitHub Profile
@pragdave
pragdave / ansible_logging.py
Last active December 9, 2016 03:56
Logging plugin for Ansible
# Interpret the Ansible log for humans. In particular, we
# look for command executions and format their content
#
# - name: list files
# shell: "ls -lrt *e*"
#
# Might produce
#
# TASK: [1] ***********************************************************
# changed: [localhost]
@pragdave
pragdave / roman1.ex
Last active September 24, 2019 02:21
defmodule RomanNumeral do
@mapping [
{1000, 'M'},
{900, 'CM'},
{500, 'D'},
{400, 'CD'},
{100, 'C'},
{90, 'XC'},
{50, 'L'},
{40, 'XL'},
[elixir/Book] svn commit -m 'prep for 0.15' [Book:158836]
Sending Changes.pml
Sending Enumeration.pml
Sending Introduction.pml
Sending Nodes.pml
Sending OTP-applications.pml
Sending OTP-servers.pml
Sending OTP-supervisors.pml
Sending Project.pml
Sending Protocols.pml
defmodule FibAgent do
defstruct cache: nil, highest_n: 0
def start_link do
initial_cache = Enum.into([ {0, 0}, {1, 1}], HashDict.new)
state = %__MODULE__{cache: initial_cache, highest_n: 1}
Agent.start_link(fn -> state end, name: __MODULE__)
end
#
# Given a list, return a new list with all occurrences of consecutive
# duplicated elements replaced by `{element, count}`
#
# compress [ 1,2,2,3,4,4,4,5,6,6]
# → [1, {2, 2}, 3, {4, 3}, 5, {6, 2}]
#
# This version uses the head of the result to give the effect of lookahead
defmodule Bitmap do
defstruct value: 0
end
defimpl Access, for: Bitmap do
use Bitwise
def access(%Bitmap{value: value}, bit) do
if (value &&& (1 <<< bit)) == 0, do: 0, else: 1
end
defmodule Bitmap do
defstruct value: 0
defimpl Access do
use Bitwise
def access(%Bitmap{value: value}, bit) do
if (value &&& (1 <<< bit)) == 0, do: 0, else: 1
end
end
defmodule Reflect do
use Application.Behaviour
def run do
path = :code.lib_dir(:elixir, :ebin)
load_modules_in(path)
modules = for {name,_} <- :code.all_loaded,
Regex.match?(~r/^[A-Z]/, atom_to_binary(name)),
do: name
defimpl Enumerable, for: Bitmap do
import :math, only: [log: 1]
def reduce(bitmap, {:cont, acc}, fun) do
bit_count = Enum.count(bitmap)
_reduce({bitmap, bit_count}, { :cont, acc }, fun)
end
defp _reduce({_bitmap, -1}, { :cont, acc }, _fun), do: { :done, acc }
@pragdave
pragdave / test.ex
Created October 16, 2013 03:02
Description
defmodule Test do
def fred, do: 1
end