(defn starts-with-descending-triplet? [[a b c]]
(>= a b c))
(starts-with-descending-triplet?'(95318)) ; true(starts-with-descending-triplet? [918532]) ; false(starts-with-descending-triplet? (iterate dec 10)) ; true
Destructure strings into individual characters:
(defn row-col [[r c]]
(str "[" r ";" c "]"))
(row-col"a5") ; "[a;5]"(row-col"x0") ; "[x;0]"
Destructure a map by its keys:
(def band [{:firstname"Jim":lastname"Matheos":instrument"guitar"}
{:firstname"Bobby":lastname"Jarzombek":instrument"drums"}
{:firstname"Joey":lastname"Vera":instrument"bass"}])
(defn describe-member [{name :firstname plays :instrument}]
(str name " plays " plays))
(map describe-member band) ; ("Jim plays guitar" "Bobby plays drums" "Joey plays bass")
When destructuring maps, values come first, keys come second.
Destructure a nested map:
(def choices {:one {:song"Monument":band"Fates Warning"}
:two {:song"Trial by Fire":band"Judas Priest"}
:three {:song"Wrathchild":band"Iron Maiden"}})
(defn announce [{{song :song} :one {band :band} :three}]
(str "We start with the song " song " and finish with something from " band "."))
(announce choices)
;; "We start with the song Monument and finish with something from Iron Maiden."
Extract a given set of values from a map by providing their keys
(:keys), filling in fallback values for missing keys (:or), and
storing the argument in its entirety (:as):
(def songs [{:title"Pale Fire":duration"4m17s":album"Inside Out":artist"Fates Warning":year1994:genre"Progressive Metal"}
{:title"Ghost in the Machine":duration"4m21s":album"Silicon Messiah":artist"Blaze Bayley":year2000}])
(defn describe-song [{:keys [:title:year:genre]
:or {genre "Metal"}
:as song}]
(str title " (" year ", " genre ") defines " (count song) " properties."))
(map describe-song songs)
;; ("Pale Fire (1994, Progressive Metal) defines 6 properties.";; "Ghost in the Machine (2000, Metal) defines 5 properties."
Write a function to-records that expects and destructures a
three-element vector, and returns a map consisting of the values
extracted with appropriate keys.
Hint: Use clojure.string/split to turn the CSV lines into vectors.
Test: (->> lines (map #(clojure.string/split % #",")) (map to-records)) shall return the following sequence of maps:
Write a function diag-dist that accepts an two coordinates encoded
in a single string of the form [x1][y1][x2][y2], such as "a1d5",
indicating a move on a game board (e.g. from point a1 to point
d5). Calculate the diagonal distance of the points using Pythagoras'
theorem.
Hint: Destructure the string argument into four characters. Characters
can be converted to their code point using int. Use the notation
\x to refer to the character x.
Write a function bonus that destructures a given map, calculates a
bonus as the product of the revenue and the rate, and returns a map
consisting of the banker’s name and bonus.
Hint: Use a fallback value of 0.01 for the rate when destructuring
the map.
Test: (map bonus bankers) shall return the following sequence of maps: