Skip to content

Instantly share code, notes, and snippets.

View NicMcPhee's full-sized avatar

Nic McPhee NicMcPhee

View GitHub Profile
@NicMcPhee
NicMcPhee / plotting_evolved_sine_functions_with_gorilla_repl.clj
Last active August 29, 2015 14:08
An example of using Gorilla Repl and Clojure to plot some evolved approximations to the sine function. The evolution was done with the ECJ system (http://cs.gmu.edu/~eclab/projects/ecj/). I'd never used Gorilla Repl, and I was really impressed with how easy it was to get it up and running and generate some basic sketches with. If you want to see…
;; gorilla-repl.fileformat = 1
;; **
;;; # Plotting some evolved sine results
;;;
;;; I decided to play with [Gorilla Repl](http://gorilla-repl.org) to try to plot some of the evolved Sine results just to make sure we were solving the problem we thought we were solving.
;;;
;;; The short version is that Gorilla Repl was quite excellent, and we are indeed solving the sine problem :-)
;;;
;;; One of the nifty things is that Clojure allowed me to define the symbol % to be protected division, so we can paste in functions straight from ECJ and run them without having to do any annoying search/replace action to get things in a format that our graphing tool can process.
@NicMcPhee
NicMcPhee / ConvertingSignedBytes.java
Created March 31, 2015 23:53
A little example showing how to convert Java's signed bytes to the appropriate unsigned integer value.
public class ConvertingSignedBytes {
public static void main(String[] args) {
byte x = (byte) 37;
byte y = (byte) 150;
printByte(x);
printByte(y);
}
public static void printByte(byte b) {
int value;
@NicMcPhee
NicMcPhee / ProjEuler_prob1.rkt
Created August 19, 2012 02:22
Various solutions to Problem 1 in Project Euler (http://projecteuler.net/problem=1)
; A Racket solution similar to the one-line Ruby solution.
(require racket/base) ; Needed for in-range
(require racket/sequence) ; Needed for sequence->list
(foldl + 0
(filter (lambda (n) (or (= (remainder n 3) 0)
(= (remainder n 5) 0)))
(sequence->list (in-range 0 1000))))
@NicMcPhee
NicMcPhee / CoinToss.ex
Last active November 3, 2015 23:19
Cryptographically fair coin toss in Elixir
# Person A flips and Person B chooses "heads" or "tails".
# Person B wins if their choice matches the flip, otherwise
# person A wins.
defmodule CoinToss do
def run_simulation(num_people) do
counterPid = spawn(__MODULE__, :counter_loop, [0, 0])
Process.register(counterPid, :counter)
people = create_people(num_people)
@NicMcPhee
NicMcPhee / CoinTossDistributed.ex
Created November 3, 2015 23:22
Distributed cryptographically fair coin toss in Elixir with multiple nodes.
# Person A flips and Person B chooses "heads" or "tails".
# Person B wins if their choice matches the flip, otherwise
# person A wins.
defmodule CoinToss do
def run_simulation(first_host, second_host, num_people) do
counterPid = spawn(__MODULE__, :counter_loop, [0, 0])
:global.register_name(:counter, counterPid)
people = create_people(first_host, second_host, num_people)
@NicMcPhee
NicMcPhee / age-person.clj
Last active December 13, 2015 18:09
Simple example of atoms and atomic updates based on an example from Chapter 4 of Programming Clojure by Emerick, et al.
(ns examples.age-person)
;; Based on an example from Chap 4 of Programming Clojure
;; by Emerick, et al
(defmacro futures
[n & exprs]
(vec (for [_ (range n)
expr exprs]
`(future ~expr))))
@NicMcPhee
NicMcPhee / lazy-eval-example.clj
Last active December 14, 2015 01:19
An example of why using map instead of doseq to perform a side-effecting operation on items in a collection can fail in subtle and difficult to debug ways.
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; This logging code is taken straight from Chapter 4 of
;; Programming Clojure by Emerick, et al. If you're having
;; trouble finding your console output in Eclipse using
;; counterclockwise, add the file output from the book and
;; use that instead.
(def console (agent *out*))
@NicMcPhee
NicMcPhee / message_queue.erl
Last active December 14, 2015 15:29
A simple Erlang example showing that messages that aren't immediately processed aren't lost, but are held for possible matching later. If you spawn a process P in the wait state initially, msg messages that are sent to it are not processed, but they're not lost, they're just held in the mailbox. When you then switch P to it's run state, then it …
%% @author mcphee
%% @doc A simple example showing that messages that
%% aren't immediately processed aren't lost, but are
%% held for possible matching later.
-module(message_queue).
-export([wait/0, run/0]).
% P = spawn(message_queue, wait, [])
@NicMcPhee
NicMcPhee / coin_toss.erl
Created March 7, 2013 17:07
An example in Erlang of a crytographically fair distributed coin toss. This example also includes totally unnecessary calls to an inefficient recursive definition of the fibonacci function to generate some non-trivial computation in each process so we can better see the parallelism in the system monitors.
%% @author mcphee
%% @doc An example in Erlang of a crytographically fair
%% distributed coin toss.
% There are a ton of commented out erlang:display/1 calls that illustrate
% a fairly brute force approach to debugging this sort of code. It can be
% really difficult to tell what's going on in a process, and using erlang:display/1
% (which should *only* be used for debugging instead of "real" I/O) can be
% a useful way to see what's happening.
@NicMcPhee
NicMcPhee / dist_coin_toss.erl
Last active December 15, 2015 12:38
An example in Erlang of a crytographically fair distributed coin toss. This example also includes totally unnecessary calls to an inefficient recursive definition of the fibonacci function to generate some non-trivial computation in each process so we can better see the parallelism in the system monitors. This is almost identical to the previous…
%% @author mcphee
%% @doc An example in Erlang of a crytographically fair
%% distributed coin toss.
-module(dist_coin_toss).
%% ====================================================================
%% API functions
%% ====================================================================
-export([run/3, pair/2, pair_all/1, create_people/3, make_person/3, counter_loop/2, wait/1, wait_for_choice/5, wait_for_outcome/4]).