Skip to content

Instantly share code, notes, and snippets.

KingCode / my-ns-test.clj
Last active Jul 1, 2020
Properties test for Functional-TV challenge #384
View my-ns-test.clj
;; see
(ns my-ns-test
(:require [my-ns :as sut]
[ :as prop]
[clojure.test.check.generators :as gen]
[clojure.test.check.clojure-test :refer [defspec]]))
(let [alias-str "sut"
sym (symbol (str alias-str "/parallel?"))]
KingCode / gist:773560f4ab5bf91e660a2a26e581b036
Last active Jul 15, 2020
cond-let and cond-let| macros, to leverage bindings between test and result expressions, as well as earlier ones (for cond-let)
View gist:773560f4ab5bf91e660a2a26e581b036
;; (cond-let
;; (odd? x) [x n] (inc x)
;; (< n 10) [y (inc n)] 10
;; :else n))
;; we want the above to yield
;; (let [x n]
;; (if (odd? x)
;; (inc x)
KingCode / gist:a954ced2aeaa22f34332fc81f75f9969
Last active Oct 27, 2018
Scoping transducer for pipelining sub-structures without losing the container.
View gist:a954ced2aeaa22f34332fc81f75f9969
(require '[clojure.core.async :as async :refer [chan <!! put!]])
(defn save-to [c save-f]
(fn [rf]
([] (rf))
([acc] (rf acc))
([acc x]
(put! c (save-f x))
(rf acc x)))))
KingCode / transducer.clj
Last active Oct 1, 2018
An implemementation of the transducer API from scratch, as a learning aid.
View transducer.clj
(ns scratch.transducer
(:refer-clojure :exclude [comp map filter mapcat take drop
take-while drop-while
;; sequence
View gist:ef2ac029dbcf2881ad1762611f837d56
(ns hellospec.fizzbuzz
(:require [clojure.spec :as s]
[clojure.spec.test :as stest]
;; to accomodate KLIPSE:
;; "[...]cljs.spec.gen.frequency is undefined[...]"
#?(:clj [clojure.spec.gen :as gen]
:cljs [clojure.test.check.generators :as gen])))
;; A fizzbuzz which doesn't use condition checking/branching
(defn fizzbuzz
KingCode / gist:101ff425f1abd9693a7e8c4b5d389e74
Created Oct 20, 2016
clojure.spec usage where instrumenting a function seems to cause exercise-fn to generate non-compliant input?? See line 61 below
View gist:101ff425f1abd9693a7e8c4b5d389e74
(require '[clojure.spec :as s]
'[clojure.spec.test :as stest])
(defn fizzbuzz
([m1 m2 n]
(let [ c #(->> % vector (concat (-> %2 dec (repeat nil))) cycle)
fb-re #"Fizz|Buzz|FizzBuzz"]
(->> [(c "Fizz" m1), (c "Buzz" m2)]
(apply map
(fn [i m1 m2]
KingCode / gist:17f56029834d47b4ffc47b8c4859f995
Created Oct 17, 2016
Unconditional FizzBuzz using a regex, for any multiples m1 (the fizz) < m2 (the buzz), m1 > 1 assumed
View gist:17f56029834d47b4ffc47b8c4859f995
(defn fizzbuzz
([m1 m2 n]
(let [ c #(->> % vector (concat (-> %2 dec (repeat nil))) cycle)
fb-re #"Fizz|Buzz|FizzBuzz"]
(->> [(c "Fizz" m1), (c "Buzz" m2)]
(apply map
(fn [i m1 m2]
(or (re-matches fb-re (str m1 m2))
(range 1 (inc n))))))
You can’t perform that action at this time.