Skip to content

Instantly share code, notes, and snippets.

# James Edward Gray II JEG2

Last active March 4, 2023 18:26
Experimenting with cumulative probability
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 trials = 10_000 simulation = 4 |> Stream.iterate(fn dollars -> case :rand.uniform(4) do 1 -> dollars - 1 2 -> dollars + 1 _n -> dollars end
Created June 20, 2019 02:08
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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} "
Created June 19, 2019 20:02
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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} "
Created March 18, 2018 17:05
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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)")
Created February 4, 2018 22:44
My OmniFocus <-> Pivotal Tracker integration.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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
Created November 11, 2017 04:21
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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)
Created July 12, 2017 16:16
A naive solution for: https://ipsc.ksp.sk/2017/real/problems/g.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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
Created February 1, 2017 22:37
Install Erlang and Elixir.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 --- - 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:
Created January 25, 2017 15:20
A wannabe GenServer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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])
Created October 19, 2016 20:11
How to get typespec information from Elixir at runtime.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
 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