Map Reduce Sandwich

A common pattern when manipulating lots of data can be described using the Map-Reduce Sandwich analogy.

Map Reduce Sandwidch

Write the map reduce sandwich

Write map and reduce functions to create the map reduce sandwich



You could have a collection of strings, each string represents an ingredients.

Then you could slice the ingredient by adding the string "sliced" to each ingredient.

You could then reduce each "sliced ingredient" into a sandwich

Reveal answer...

Slice our ingredients

(defn slice [item]
  (str "sliced " item))

Map Slice over the ingredients

(def prepared-ingredience
  (map slice ["bread" "cucumber" "pepper" "tomato" "lettuce" "onion"]))

Reduce our ingredients to a sandwich

(defn make-sandwich [ingredience]
  (reduce str (interpose ", " ingredience)))

(str "A tasty sandwich made with " (make-sandwich prepared-ingredience))

Here is the same map-reduce sandwich using a slightly different syntax

(->> ["bread" "cucumber" "pepper" "tomato" "lettuce" "onion"]
     (map #(str "sliced " %) ,,,)
     (interpose ", " ,,,)
     (reduce str ,,,)
     (str "I have a tasty sandwich made with " ,,,))

results matching ""

    No results matching ""