Skip to content

Instantly share code, notes, and snippets.

View NicMcPhee's full-sized avatar

Nic McPhee NicMcPhee

View GitHub Profile
@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 / 4Clojure-graph-connectivity.clj
Created January 29, 2013 05:38
Solution to 4Clojure's Graph Connectivity problem (http://www.4clojure.com/problem/91). This was hard enough that I did it in Eclipse with Counterclockwise (with some unit tests, etc.), and then turned it into one big fn at the end to paste into 4Clojure.
(ns four-clojure-problems.graph-connectivity)
(use 'clojure.test)
;; Take a connection map (maps nodes to sets of
;; nodes that are all the nodes the key node
;; connects to) and an edge, and returns a new,
;; updated connection map.
(defn process-connection [connections-map edge]
(let [a (first edge)
@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 / characters.clj
Last active May 25, 2016 22:52
The initial simple characters example from Chapter 4 of Programming Clojure by Emerick, et al. I added a little ```sleep``` to ```loot``` because otherwise often Bilbo got all the loot.
(ns examples.characters)
;; 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 / counters.clj
Last active September 16, 2019 10:59
Three different ways of doing counters in Clojure, one of which doesn't work and thereby illustrates the scope of atomicity and a difference between atom and ref.
;;;;;;;;;;;;;;;;;;
;; The first way
;;;;;;;;;;;;;;;;;;
;; This use of atom is simple and works fine.
(def counter (atom -1))
(defn next-value []
(swap! counter inc))
@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]).
@NicMcPhee
NicMcPhee / testWordWrap.py
Created April 3, 2013 23:12
Unfinished solution to the word wrap kata from the 3 April 2013 coding dojo at the University of Minnesota, Morris. We got a number of good tests written and can pass several of them, but time ran out just as we were wrestling with where to put the line break in more complex situations. For the record the "customer" (me) requested that the funct…
import wordWrap
import unittest
import sys
class TestWordWrap(unittest.TestCase):
def setUp(self):
self.string = "Andrew is a pretty cool dude!"
def tearDown(self):