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 round5 | |
"Round to the closest positive multiple of 5. | |
Negative numbers round to 0, which is not | |
considered a multiple of 5." | |
{:test (fn [] (let [roundsto (fn [e] #(= e (round5 %)))] | |
(testing "Negative numbers" | |
(is (every? (roundsto 0) (range -20 0)))) | |
(testing "Positive numbers rounding to 5" | |
(is (every? (roundsto 5) (range 0 8)))) | |
(testing "Positive numbers rounding not to 5" |
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
:find (pull ?e [*]) | |
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
;; fixed version of 'state' at http://mishadoff.com/blog/clojure-design-patterns/#episode-3-state | |
;; takeaway: if you call 'swap!' twice on the same atom, you are probably making a mistake | |
(def user {:name "Jackie Brown" | |
:balance 0 | |
:subscription? false}) | |
(def ^:const SUBSCRIPTION_COST 30) | |
(defn pay |
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
;; I think it would be a mistake to introduce temporal coupling to prevent typos. | |
;; The example program below lets you identify "missing" keys specs at | |
;; the time and place of your choosing, and then handle them as you | |
;; deem appropriate, without imposing those decisions on other | |
;; users of spec. | |
(require '[clojure.spec.alpha :as s] | |
'[clojure.set :as set]) |
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
;; fix for code shown at https://twitter.com/metcarob/status/918764898041188352 | |
(def recieved-slack-vars (atom {})) | |
;; unnecessarily complex | |
(defn return-second-arg [arg1 arg2] | |
arg2) | |
(defn set-recieved-slack-vars | |
[newval] | |
(swap! recieved-slack-vars return-second-arg newval)) | |
(set-recieved-slack-vars {:botname "Test"}) |
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
;; try this form-by-form at a REPL | |
(require '[clojure.spec.alpha :as s]) | |
;; create an inline DSL to describe the FizzBuzz world | |
(defmacro divides-by | |
[nm n] | |
`(s/def ~nm (s/and pos-int? #(zero? (mod % ~n))))) | |
;; specify FizzBuzz | |
(divides-by ::fizz 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
(1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz" 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz" 31 32 "Fizz" 34 "Buzz" "Fizz" 37 38 "Fizz" "Buzz" 41 "Fizz" 43 44 "FizzBuzz" 46 47 "Fizz" 49 "Buzz" "Fizz" 52 53 "Fizz" "Buzz" 56 "Fizz" 58 59 "FizzBuzz" 61 62 "Fizz" 64 "Buzz" "Fizz" 67 68 "Fizz" "Buzz" 71 "Fizz" 73 74 "FizzBuzz" 76 77 "Fizz" 79 "Buzz" "Fizz" 82 83 "Fizz" "Buzz" 86 "Fizz" 88 89 "FizzBuzz" 91 92 "Fizz" 94 "Buzz" "Fizz" 97 98 "Fizz" "Buzz") |
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
(require '[clojure.spec.alpha :as s]) | |
(s/def ::coord nat-int?) | |
(s/def ::x ::coord) | |
(s/def ::y ::coord) | |
(s/def ::xy (s/keys :req-un [::x ::y])) | |
(s/def ::point (s/map-of #{:point} ::xy)) | |
(s/def ::username (s/and string? seq)) | |
(s/def ::user (s/map-of #{:user} ::username)) |
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
(require '[datomic.api :as d]) | |
(def uri "datomic:mem://foo") | |
(d/create-database uri) | |
(def conn (d/connect uri)) | |
(def schema [{:db/ident :foo | |
:db/fn #db/fn | |
{:lang "java" | |
:params [db a b c] | |
:code "return a;"}}]) |
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
(def argses (->> (s/exercise ::cs/xy-chart-args 10 ex/generators) | |
(mapv first))) |