Skip to content

Instantly share code, notes, and snippets.

View epfahl's full-sized avatar

Eric Pfahl epfahl

  • Hillsborough, North Carolina
View GitHub Profile
defmodule PLF do
@doc """
Given a list of `{x, y}` nodes that the function must pass through, return
a function any input `x`.
For inputs less than the minimum `x` values among the nodes, extrapolate
with a constant equal to the `y` value of the min-`x` node. Similar
extrapolation occurs for `x` larger than the max-`x` node.
Note: When the number of nodes is large, this can be made more efficient
@epfahl
epfahl / quicksort.ex
Created March 2, 2024 12:51
Quicksort in Elixir
defmodule Quicksort do
@spec sort([any()], :asc | :desc) :: [any()]
def sort(values, direction \\ :asc) do
sorted = sort(values)
if direction == :asc do
sorted
else
Enum.reverse(sorted)
end
@epfahl
epfahl / neumann_exp.ex
Created November 29, 2023 13:13
Implementation of von Neumann's exponential sampling algorithm.
defmodule Exp do
def draw() do
x = :rand.uniform()
draw(x, x, 1, 0)
end
defp draw(x, u, n, k) do
u_next = :rand.uniform()
if u > u_next do
defmodule MergeSort do
@spec sort(list()) :: list()
def sort([]), do: []
def sort([h]), do: [h]
def sort([_ | _] = list) do
{left, right} = divide(list)
left_sorted = sort(left)
right_sorted = sort(right)
@epfahl
epfahl / l3.ex
Last active March 24, 2023 01:02
A little logic language (L3).
# Discussed in https://www.ericpfahl.com/beyond-unification-a-basic-logical-toolkit/
defmodule L3.Var do
alias L3.Var
@type t :: %Var{id: any}
defstruct [:id]
@spec new(any) :: Var.t()
def new(id), do: %Var{id: id}
@epfahl
epfahl / unification.ex
Last active March 24, 2023 01:01
Unification algorithm in Elixir.
# Discussed in https://www.ericpfahl.com/from-pattern-matching-to-unification/
defmodule Var do
alias Var
@type t :: %Var{id: any}
defstruct [:id]
@spec new(any) :: Var.t()
def new(id), do: %Var{id: id}

Keybase proof

I hereby claim:

  • I am epfahl on github.
  • I am udikorn (https://keybase.io/udikorn) on keybase.
  • I have a public key ASC-Qn48i6lXIbcY44rIuSgaliYNzJ_WgOqFQnKL9gqdZAo

To claim this, I am signing this object: