Skip to content

Instantly share code, notes, and snippets.

@marick
Created October 7, 2011 16:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marick/1270755 to your computer and use it in GitHub Desktop.
Save marick/1270755 to your computer and use it in GitHub Desktop.
(ns scratch.core
(:use midje.sweet
[clojure.set :only [union intersection difference]]
))
(unfinished )
(defn headed-pairs [head second-halves]
(map (fn [second-half] [head second-half])
second-halves))
(fact
(headed-pairs :head [2 3]) => [ [:head 2] [:head 3] ])
(defn tails [sequence]
(map drop (range 1 (count sequence)) (repeat sequence)))
(fact "tails produces each tail of the sequence, not including the sequence itself
or the empty list"
(tails [1 2 3]) => [ [2 3] [3] ])
(defn downward-pairs [sequence]
(mapcat headed-pairs sequence (tails sequence)))
(fact "downward-pairs combines each element with all the elements in its tail"
(downward-pairs [1 2 3]) => (just [1 2] [1 3]
[2 3]
:in-any-order)
(provided
(headed-pairs 1 [2 3]) => [ [1 2] [1 3] ]
(headed-pairs 2 [3]) => [ [2 3] ]
(tails [1 2 3]) => [ [2 3] [3] ]))
(fact "the whole thing"
(downward-pairs '[A, B, C, D])
=> (just '[[A,B], [A,C], [A,D], [B, C], [B,D], [C,D]]
:in-any-order))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment