Skip to content

Instantly share code, notes, and snippets.

(def naturals (drop 1 (range)))
(def fizz-buzz-seq
(map #(clojure.string/replace (str %1 %2) #"^$" (str %3))
(cycle '("" "" "Fizz"))
(cycle '("" "" "" "" "Buzz"))
naturals))
(defn fizz-buzz [n]
(clojure.string/join " " (take n fizz-buzz-seq)))
;;
;; Destructuring
;;
;; Destructuring is a concise syntax for declaratively pulling apart collections and
;; binding values contained therein as named locals within a let form.
;; (Clojure Programming, Chas Emerick, Brian Carper, Christophe Grand)
;; Since it's a facility provided by let, it can be used
;; in any expression that implicitly uses let (like fn, defn, loop, etc).
(def test-address
{:street-address "123 Test Lane"
:city "Testerville"
:state "TX"})
;"Destructuring is an arbiter: it breaks up arguments"
(= "__" ((fn [[a b]] (str b a))
[:foo :bar]))
;"Whether in function definitions"
;;--------------
;;
;; A bit about Clojure functions
;;
;;--------------
;; Functions are first class values in Clojure.
;;--------------
;; 1. fn form
(defn- happy? [n visited]
(if (contains? visited n)
false
(if (= n 1)
true
(let [ndigits (decompose-in-digits n)
sum-of-squares (apply + (map square ndigits))]
(recur sum-of-squares (conj visited n))))))

Password Validator kata

  • A password should have more than 6 chars

  • A password should contain at least 1 char in upper case

  • A password should contain at least 1 char in lower case

  • A password should contain at least 1 underscore

"_Ab3ccc" -> ok

"c" -> wrong

"_b3c" -> wrong

"_Ab3cc" -> wrong

"_Abcccc" -> wrong

Rationals addition kata

We'll program the addition of two rationals using TDD.

Suggested API for the method (in Java):

  public Rational add(Rational rational);
(ns password-validator.core)
(defn- has-more-than-6-chars? [password]
(> (count password) 6))
(defn- any? [pred coll]
(not= nil (some pred coll)))
(def ^:private contains-at-least-one-upper-case-char?
(partial any? #(Character/isUpperCase %)))