Skip to content

Instantly share code, notes, and snippets.

View epfahl's full-sized avatar

Eric Pfahl epfahl

  • Hillsborough, North Carolina
View GitHub Profile
@ityonemo
ityonemo / test.md
Last active July 19, 2024 06:53
Zig in 30 minutes

A half-hour to learn Zig

This is inspired by https://fasterthanli.me/blog/2020/a-half-hour-to-learn-rust/

Basics

the command zig run my_code.zig will compile and immediately run your Zig program. Each of these cells contains a zig program that you can try to run (some of them contain compile-time errors that you can comment out to play with)

@epfahl
epfahl / unification.ex
Last active July 5, 2024 03:08
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}
@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 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