Skip to content

Instantly share code, notes, and snippets.

View JEG2's full-sized avatar

James Edward Gray II JEG2

View GitHub Profile
@JEG2
JEG2 / probability.exs
Last active March 4, 2023 18:26
Experimenting with cumulative probability
trials = 10_000
simulation =
4
|> Stream.iterate(fn dollars ->
case :rand.uniform(4) do
1 -> dollars - 1
2 -> dollars + 1
_n -> dollars
end
defmodule TicTacToe do
def play do
move(" ", " ", " ", " ", " ", " ", " ", " ", " ", "X")
end
def draw_grid(a1, b1, c1, a2, b2, c2, a3, b3, c3) do
IO.puts "\n a b c "
IO.puts "1 #{a1} | #{b1} | #{c1} "
IO.puts " ---+---+---"
IO.puts "2 #{a2} | #{b2} | #{c2} "
defmodule TicTacToe do
def play do
move(" ", " ", " ", " ", " ", " ", " ", " ", " ", "X")
end
def draw_grid(a1, b1, c1, a2, b2, c2, a3, b3, c3) do
IO.puts "\n a b c "
IO.puts "1 #{a1} | #{b1} | #{c1} "
IO.puts " ---+---+---"
IO.puts "2 #{a2} | #{b2} | #{c2} "
defmodule SQLTest do
use ExUnit.Case, async: false
alias Replicator.{Config, Database, Connection, TestHelper}
defmodule Examples do
use Replicator.SQL
none(:prepare, "CREATE TABLE examples (id SERIAL, name TEXT)")
none(:insert, "INSERT INTO examples (name) VALUES ($name)")
@JEG2
JEG2 / Sync Work.scpt
Created February 4, 2018 22:44
My OmniFocus <-> Pivotal Tracker integration.
tell application "OmniFocus"
tell default document
set work_project to first flattened project whose name is "Close PivotalTracker Stories"
set work_tasks to flattened tasks of work_project
set tasks_string to ""
repeat with the_task in work_tasks
set task_name to the name of the_task
if completed of the_task then
set task_status to "delivered"
else
defmodule Fermat do
require Integer
def mpow(n, 1, _m), do: n
def mpow(n, k, m) when Integer.is_even(k) do
x = mpow(n, div(k, 2), m)
rem((x * x), m)
end
def mpow(n, k, m) when Integer.is_odd(k) do
x = mpow(n, k - 1, m)
defmodule Gunfight do
defstruct shooters: %{ }, kills: ""
@result_limit :math.pow(10, 9) |> Kernel.+(9) |> round
def new, do: %__MODULE__{ }
def point(%__MODULE__{shooters: shooters} = gunfight, pointer, "0") do
%__MODULE__{gunfight | shooters: Map.delete(shooters, pointer)}
end
@JEG2
JEG2 / playbook.yml
Created February 1, 2017 22:37
Install Erlang and Elixir.
---
- hosts: builders
tasks:
- name: Fetch Erlang Solutions repository
get_url:
url: https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb
dest: /tmp/erlang-solutions_1.0_all.deb
- name: Add Erlang Solutions repository
become: true
apt:
@JEG2
JEG2 / counter.rb
Created January 25, 2017 15:20
A wannabe GenServer.
class Counter
def initialize(initial_count = 0)
@mailbox = Queue.new
@replies = Queue.new
@state = initial_count
@server = process_messages
end
def increment
mailbox.push([:handle_increment, 1])
@JEG2
JEG2 / runtime_types.exs
Created October 19, 2016 20:11
How to get typespec information from Elixir at runtime.
defmodule TypeSpecs do
def for_module(module) do
{:ok, {^module, [{:abstract_code, {:raw_abstract_v1, attributes}}]}} =
module |> :code.which |> :beam_lib.chunks([:abstract_code])
attributes
|> Enum.filter_map(fn
{:attribute, _, :spec, _function_and_types} -> true
_attribute -> false
end, fn {:attribute, _, :spec, function_and_types} ->
function_and_types