Skip to content

Instantly share code, notes, and snippets.

View oliyh's full-sized avatar

Oliver Hine oliyh

View GitHub Profile
{
"swagger": "2.0",
"info": {
"title": "DR API",
"version": "1.0",
"description": "Dr Jan Itor at your service"
},
"produces": [
@oliyh
oliyh / someas.clj
Created June 17, 2015 12:57
Combining Clojure macros some-> and as->
(defmacro someas->
"A mixture of some-> and as-> allowing more flexibility in the step forms"
[expr name & forms]
(let [pstep (fn [step] `(if (nil? ~name) nil ~step))]
`(let [~name ~expr
~@(interleave (repeat name) (map pstep forms))]
~name)))
@oliyh
oliyh / distinct.clj
Last active August 29, 2015 14:22
distinctive - choose how to determine distinct items in Clojure
(defn distinctive
"Like clojure.core/distinct, but can take a function f by which distinctiveness is calculated,
giving similar semantics to sort-by"
([coll] (distinctive identity coll))
([distinction-fn coll]
(let [step (fn step [xs seen]
(lazy-seq
((fn [[f :as xs] seen]
(when-let [s (seq xs)]
(if (contains? seen (distinction-fn f))
@oliyh
oliyh / condas.clj
Created May 14, 2015 16:28
condas-> - A Clojure macro combining cond-> and as-> to give more flexibility in the test and step forms
(defmacro condas->
"A mixture of cond-> and as-> allowing more flexibility in the test and step forms"
[expr name & clauses]
(assert (even? (count clauses)))
(let [pstep (fn [[test step]] `(if ~test ~step ~name))]
`(let [~name ~expr
~@(interleave (repeat name) (map pstep (partition 2 clauses)))]
~name)))
@oliyh
oliyh / bidi_test.clj
Created June 19, 2014 16:14
Failing test describing how interaction with middleware like wrap-params results in unexpected entries in the route-params map
(ns bidi.bidi-test
(:require [clojure.test :refer :all]
[bidi.bidi :refer :all]
[ring.mock.request :refer :all]))
(deftest route-params-hygiene-test
(testing "other request constraints"
(let [handler
(make-handler [["/blog/user/" :userid "/article"]
@oliyh
oliyh / example.clj
Created March 17, 2014 17:13
You are in a shop and have 10p. The following items are for sale: Muffin - 5p Milkshake - 3p Chocolate - 2p Gingerbread man - 1p You need to spend ALL your money. What combinations of items can you buy such that you spend all 10p?
(def prices {:muffin 5 :milkshake 3 :chocolate 2 :gingerbread 1})
(clojure.pprint/pprint (shop 10 prices))
({:muffin 0, :chocolate 0, :milkshake 0, :gingerbread 10}
{:muffin 0, :chocolate 0, :milkshake 1, :gingerbread 7}
{:muffin 0, :chocolate 0, :milkshake 2, :gingerbread 4}
{:muffin 0, :chocolate 0, :milkshake 3, :gingerbread 1}
{:muffin 0, :chocolate 1, :milkshake 0, :gingerbread 8}
{:muffin 0, :chocolate 1, :milkshake 1, :gingerbread 5}
{:muffin 0, :chocolate 1, :milkshake 2, :gingerbread 2}
;; binary-like count with n 'bits' which can have values between from and to
;; e.g. (permutations 3 0 3) -> ([0 0] [0 1] [0 2] [1 0] [1 1] [1 2] [2 0] [2 1] [2 2])
(defn permutations [n from to]
(let [symbols (into [] (take n (repeatedly gensym)))
ranges (take n (repeatedly #(into [] (range from to))))
bindings (into [] (interleave symbols ranges))]
(eval `(for ~bindings ~symbols))))
@oliyh
oliyh / test.clj
Last active December 15, 2015 10:18
A threading macro for Clojure for use when threading functions which have varying signatures.
(deftest customisable-threading
(is (= "hello" (->>> :a (% {:a :h}) (name %) (cons % "ello") (clojure.string/join %)))))