Skip to content

Instantly share code, notes, and snippets.

View msgodf's full-sized avatar

Mark Godfrey msgodf

View GitHub Profile
@msgodf
msgodf / gist:2982481
Created June 24, 2012 08:40
Functions to generate and print the Mandelbrot set
(defn vec-abs
[[x y]]
(+ (* x x)
(* y y)))
(defn vec-add
[x y]
(map + x y))
(defn vec-sq
@msgodf
msgodf / gist:2994144
Created June 26, 2012 07:36
Clojure functions _can_ be made to take more than 20 parameters.
(defn massive-arity
[& [a b c d e f g h i j k l m n o p q r s t u v w x y z]]
(print a b c d e f g h i j k l m n o p q r s t u v w x y z))
@msgodf
msgodf / gist:4251107
Created December 10, 2012 15:06
Generate all pairs of the integers [0,n), excluding those of the form [a,a] and only the first pair of the symmetric [a,b] [b,a]
(defn different-int-pairs
"Generate all pairs of the integers [0,n), excluding those of the form [a,a] and only the first pair of the symmetric [a,b] [b,a]"
[n]
(mapcat (fn [x] (map #(vector x %)
(range (inc x) n)))
(range n)))
@msgodf
msgodf / radiation_search.clj
Created December 11, 2012 08:29
Clojure solution to to "Radiation Search" problem on check.io
(ns cluster.core)
; Begin by creating 'clusters' for all the squares of a particular number
; Then repeatedly check for adjacent clusters and merge them
(defn random-grid
"A grid composed of random values in range [1,n]"
[size n]
(vec (map vec
(partition size
(repeatedly (* size size)
#(int (+ 1.5 (rand (dec n)))))))))
@msgodf
msgodf / radiation_search.py
Last active December 9, 2015 23:29
Python solution to the "Radiation Search" problem on check.io
from random import randint
# A grid of the specified size, composed of random values in range [1,n]
def random_grid(size, n):
return [[randint(0, (n - 1)) for _ in range(size)] for _ in range(size)]
# Generate all sets of integers pairs in [0,n), excluding those of the form [a a]
def int_pairs(n):
pairs = []
for i in range(0, n):
(ns sokoban.core)
(def moves {:n [0 1] :s [0 -1] :e [1 0] :w [-1 0]})
(def start-world
{:person [3 1]
:targets #{[1 3]}
:crates #{[1 2]}
:blanks #{[1 3] [2 3] [3 3]
[1 2] [2 2] [3 2]
@msgodf
msgodf / brisfunctional-ocr-kata-failures
Created April 10, 2013 08:03
Several attempts to get a sweet solution to getting digit parcel part of Brian Marick's Brisfunctional talk.
user=> (map #(partition 3 %) ["123---" "456---"])
(((\1 \2 \3) (\- \- \-)) ((\4 \5 \6) (\- \- \-)))
user=> (map #(map (partial apply str) (partition 3 %)) ["123---" "456---"])
(("123" "---") ("456" "---"))
user=> (map #(map (partial apply str) (partition 3 %)) s) ["123---" "456---"])
CompilerException java.lang.RuntimeException: Unable to resolve symbol: s in this context, compiling:(NO_SOURCE_PATH:1:1)
["123---" "456---"]
RuntimeException Unmatched delimiter: ) clojure.lang.Util.runtimeException (Util.java:219)
@msgodf
msgodf / counter_agent.clj
Created May 20, 2013 11:24
Messing around with Clojure agents
; A little function that adds its two parameters together
(defn addit [x y] (+ x y))
; This adds the value supplied in the second parameter to the atom in the first parameter, then returns the atom
(defn atom-adder [x y] (swap! x (partial addit y)) x)
(def value-atom (atom 1))
; Create an agent with a reference to the atom
(def counter-agent (agent value-atom))
@msgodf
msgodf / concurrent_sieve.clj
Created May 20, 2013 12:03
Concurrent prime number sieve. I wanted something that took a while, so that the agent could be running in the background for some time. I used an atom so that I could monitor the sieve externally.
(def sieve (atom []))
(def siever (agent sieve))
(send-off siever
(fn [x test-set]
(doall
(map (fn [n]
(if (not (some
#(zero? (mod n %))
@msgodf
msgodf / gist:5656370
Last active December 17, 2015 18:49
Trying out Java 7 NIO2's FileVisitor, along with half of Guava to attempt a slightly functional style. This is still pretty verbose in Java.
import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;
import org.apache.commons.lang3.StringUtils;