brave-clojure-web icon indicating copy to clipboard operation
brave-clojure-web copied to clipboard

Fix mapify description in Core Functions in Depth

Open devurandom opened this issue 9 years ago • 0 comments
trafficstars

The following code:

(defn mapify
  "Return a seq of maps like {:name \"Edward Cullen\" :glitter-index 10}"
  [rows]
  (map (fn [unmapped-row]
         (reduce (fn [row-map [vamp-key value]]
                   (assoc row-map vamp-key (convert vamp-key value)))
                 {}
                 (map vector vamp-keys unmapped-row)))
       rows))

Is explained as:

In this function, `map` transforms each row—vectors like `["Bella Swan" 0]` — into a map by using `reduce` in a manner similar to the first example in “`reduce`” above.
First, `map` creates a seq of key-value pairs like `([:name "Bella Swan"] [:glitter-index 0])`.
Then, `reduce` builds up a map by associating a vamp key with a converted vamp value into `row-map`.

This is wrong and confusing. The row-vectors is not ["Bella Swan" 0] but ["Bella Swan" "0"], not [:glitter-index 0] but [:glitter-index "0"]. Additionally the references to map could be made less ambiguous. I suggest following text (changes highlighted using**`):

In this function, **the outer** `map` transforms each row — vectors like `["Bella Swan" "0"]` — into a map by using `reduce` in a manner similar to the first example in “`reduce`” above.
First, **the innermost** `map` creates a seq of key-value pairs like `([:name "Bella Swan"] [:glitter-index "0"])`.
Then, `reduce` builds up a map by associating a vamp key with a converted vamp value into `row-map`.

devurandom avatar Apr 10 '16 11:04 devurandom