Skip to content

Instantly share code, notes, and snippets.

@ctford
ctford / multiple-of.idr
Created March 1, 2014 16:30
Trying to prove that adding two numbers with a common factor results in another number with that factor.
data MultipleOf : Nat -> Nat -> Type where
NoneOf : (m : Nat) -> MultipleOf m Z
Next : MultipleOf m n -> MultipleOf m (n + m)
add : MultipleOf m a -> MultipleOf m b -> MultipleOf m (a + b)
add (NoneOf _) y = y
add (Next x) y = add x (Next y)
(ns polymorphism.core)
(def basket
[{:price 88 :name "Chocolate"}
{:price 22 :name "Newspaper"}
{:price 3 :name "Lettuce"} ])
(defn receipt [items]
(map
#(str (:price %) " " (:name %))
@ctford
ctford / overriden-version.clj
Created May 24, 2014 22:09
Transitive dependencies resulting in Clojure 1.3.0 being used.
(defproject pecha "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.5.1"]
[overtone "0.8.1"]
[leipzig "0.8.0"]])
@ctford
ctford / map.clj
Created May 30, 2014 21:15
An alternative implementation of map that won't silently return nils for missing keys.
(ns strict-map.map)
(deftype StrictMap [inner]
clojure.lang.IPersistentMap
(assoc [this k v]
(StrictMap. (.assoc inner k v)))
(assocEx [this k v]
(StrictMap. (.assocEx inner k v)))
(without [this k]
(StrictMap. (.without inner k)))
@ctford
ctford / map_test.clj
Created June 2, 2014 09:11
Tests for the strict map.
(ns strict-map.map-test
(:require [midje.sweet :refer :all]
[strict-map.map :refer [strict]]))
(fact "Attempting to get a missing key from a strict map throws an exception."
(-> {:key :value} strict :key) => :value
(->> {:key :value} strict :key) => :value
(-> {:key :value} strict map?) => true
(-> {:key :value} strict (:foo :default)) => :default
(-> {:key :value} strict (apply [:key])) => :value
@ctford
ctford / providing.clj
Created June 8, 2014 18:05
Two potential syntaxes for supporting Midje mocks without vars.
(fact "I can use triple even though it's not defined in a var."
(map triple [1]) => [3]
(provided (triple 1) => 3 :times 1))
(fact "I can bind a mock to a locally scoped name and use it like any other value."
(let [triple (providing (triple 1) => 3 :times 1)]
(map triple [1])) => [3])
@ctford
ctford / isort.idr
Created July 2, 2014 18:47
A length-safe insertion sort.
insert : Ord a => a -> Vect n a -> Vect (1 + n) a
insert x [] = [x]
insert x (y::ys) with (x <= y)
| True = x::y::ys
| False = y::insert x ys
isort : Ord a => Vect n a -> Vect n a
isort [] = []
isort (x::xs) = insert x (isort xs)
@ctford
ctford / unit-weights.clj
Last active August 29, 2015 14:04
It's impossible to tell which bag is fake for any strategy, for a given weight.
(defn unit-weights
"The weight of a single coin for each of the bags being fake, for a given total weight.
e.g. (unit-weights 11110 [1 10 100 1000 10000])"
[weight strategy]
(let [total (reduce + strategy)
fake (partial * 9/10)]
(map
(fn [bag] (/ weight (+ (- total bag) (fake bag))))
strategy)))
@ctford
ctford / song.clj
Created September 28, 2014 19:22
Tuesday - written with Overtone and Leipzig.
(ns tuesday.song
(:require [overtone.live :refer :all]
[leipzig.melody :refer :all]
[leipzig.scale :as scale]
[leipzig.live :as live]
[leipzig.chord :as chord]
[leipzig.temperament :as temperament]))
; Instruments
(definst bass [freq 110 volume 1.0]
@ctford
ctford / song.clj
Created September 30, 2014 19:50
Suits me fine
(ns suits-me-fine.song
(:require [overtone.live :refer :all]
[leipzig.melody :refer :all]
[leipzig.scale :as scale]
[leipzig.live :as live]
[leipzig.chord :as chord]
[leipzig.temperament :as temperament]))
; Instruments
(definst bass [freq 110 volume 1.0]