This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns shades.lenses) | |
; We only need three fns that know the structure of a lens. | |
(defn lens [focus fmap] {:focus focus :fmap fmap}) | |
(defn view [x {:keys [focus]}] (focus x)) | |
(defn update [x {:keys [fmap]} f] (fmap f x)) | |
; The identity lens. | |
(defn fapply [f x] (f x)) | |
(def id (lens identity fapply)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns polymorphism.core) | |
(def basket | |
[{:price 88 :name "Chocolate"} | |
{:price 22 :name "Newspaper"} | |
{:price 3 :name "Lettuce"} ]) | |
(defn receipt [items] | |
(map | |
#(str (:price %) " " (:name %)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn takes [dish minutes] | |
(-> dish (update-in [:time] #(+ % minutes)))) | |
(defn sit [minutes] | |
(fn [dish] (-> dish (takes minutes)))) | |
(defn add [ingredient attributes] | |
(fn [dish] (-> dish (assoc ingredient attributes) (takes 1)))) | |
(defn water-for [ingredient] |