Skip to content

Instantly share code, notes, and snippets.

View ecmendenhall's full-sized avatar

Connor Mendenhall ecmendenhall

View GitHub Profile
@ecmendenhall
ecmendenhall / core.clj
Last active February 12, 2016 13:32
Prime factors two ways: Traditional unit tests and simple-check generative tests.
(ns primes.core)
(defn trial-division [n divisor factors]
(if (< n 2)
factors
(if (= 0 (mod n divisor))
(recur (/ n divisor) divisor (conj factors divisor))
(recur n (inc divisor) factors))))
(defn prime-factors [n]
@ecmendenhall
ecmendenhall / speclj-check-that.clj
Created October 21, 2013 20:45
A macro for Speclj-style simple-check assertions.
(ns my-great-project.core-spec
(:require [speclj.core :refer :all]
[simple-check.core :as sc]
[simple-check.generators :as gen]
[simple-check.properties :as prop]
[my-great-project.core :refer :all]))
(defmacro check-that [desc n property]
`(it ~desc
(let [check# (sc/quick-check ~n ~property)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.clojars.ecmendenhall</groupId>
<artifactId>schtitt</artifactId>
<version>0.9.2.1-SNAPSHOT</version>
<description>A very simple HTTP server.</description>
(describe "Converting hex to base64"
(it "parses hex values from two-character strings"
(should= 0x2f (parse-hex "2f")))
(it "splits hex strings to sequences of strings"
(should= ["00" "1a" "2b" "88"]
(split-hex-string "001a2b88")))
(it "converts hex strings into sequences of hex values"
(ns crypto-challenges.core
(:require [clojure.edn :refer [read-string]]
[clojure.string :refer [lower-case]]))
(def alphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
(def base64-values (str alphabet
(lower-case alphabet)
"0123456789+/"))
(ns prime-factors.core
(:require [clojure.core.logic :refer :all])
(:require [clojure.core.logic.fd :refer [in interval eq]]))
(defn factor-pairs [number]
(run* [pair]
(fresh [factor1 factor2]
(in factor1 factor2 (interval 2 number))
(eq (= number (* factor1 factor2)))
(== pair [factor1 factor2]))))
#lang racket/base
(require rackunit)
(define simple-q '((1 2 3) (6 5 4)))
(define empty-queue '(()()))
(check-equal? empty-queue '(() ())
"An empty queue is a list containing two empty lists.")

Breadth-first numbering with a pure functional queue

My solution to Chris Okasaki's functional pearl: given a tree, reproduce a tree with the same structure with nodes numbered in breadth-first order (using only immutable data structures, of course).

For more on streams and lazy lists, check out chapter 3 of SICP. For more on pure functional queues, see this other paper by Chris Okasaki.

I'm not completely happy with my solution. On the plus side, it generalizes to non-binary trees. But performing a breadth-first search to calculate node numbers and a depth-first map to apply them is inefficient. I tried to construct my solution from the

@ecmendenhall
ecmendenhall / quiet-queries.clj
Created June 20, 2013 00:21
Quiet cascalog queries
(require '[cascalog.io :refer [with-log-level])
(defmacro ?<-- [& forms] `(with-log-level :fatal (?<- ~@forms)))
@ecmendenhall
ecmendenhall / on-lisp-chapter-6.clj
Last active December 17, 2015 17:29
Network closures in Clojure, from Chapter 6 of "On Lisp."
;; Figure 6.2 – Representation and definition of nodes
(def nodes (atom {}))
(defn defnode [name contents & [yes no]]
(swap! nodes assoc name {:contents contents :yes yes :no no}))
;; Figure 6.3 – Sample network
(defn make-nodes [node-maker]
(node-maker :people "Is the person a man?" :male :female)
(node-maker :male "Is he living?" :live :dead)