Skip to content

Instantly share code, notes, and snippets.

@RedPenguin101
Created June 13, 2020 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RedPenguin101/d604a6ba40bdb021c7b7d2b9e7d92f1b to your computer and use it in GitHub Desktop.
Save RedPenguin101/d604a6ba40bdb021c7b7d2b9e7d92f1b to your computer and use it in GitHub Desktop.
Why is my instrument not working?
(ns cashflows
(:require [clojure.spec.alpha :as s]
[clojure.spec.test.alpha :as stest]))
(s/def ::date (s/inst-in #inst "1990" #inst "2050"))
(s/def ::amount number?)
(s/def ::cashflow (s/cat :date ::date :amount ::amount))
(s/exercise ::cashflow 5)
(defn date-from [cashflow]
(nth cashflow 0))
(defn amount-from [cashflow]
(nth cashflow 1))
(defn net-cashflow
"Returns the net cashflow amount of a sequence of cashflows"
[cashflows]
(reduce #(+ %1 (amount-from %2)) 0 cashflows))
(s/fdef net-cashflow
:args (s/coll-of ::cashflow)
:ret number?)
(def example [[#inst "2020-01" -200]
[#inst "2020-03" -200]
[#inst "2020-07" -300]
[#inst "2020-10" -300]
[#inst "2021-08" 150]
[#inst "2021-09" 150]
[#inst "2021-11" 150]
[#inst "2021-12" 50]
[#inst "2022-06" 500]
[#inst "2022-08" 120]])
(s/explain-str (s/coll-of ::cashflow) example) ;; true
(net-cashflow example) ;; instrument complains
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment