Clojure map display function

I am writing a simple photo library application in Clojure. I have a library card that has a key :photos , which is a vector of photos. Then I have a function that adds a photo to the library --- it takes a library object and a photo to be added as arguments.

 (defn add-to-library [library photo] ... ) 

It returns a library map with photo added.

Now I want to "display" this function above the list of photos. I need to pass the library object through one iteration to the next.

What is the idiomatic way to do this in Clojure?

+4
source share
1 answer

Try:

(reduce add-to-library library list-of-photos) .

The reduce function is wonderful and represents a general tool that is surprisingly applicable in many specific situations. Many of these situations are yours, where you have a “collection of things,” “a function that adds a thing to this collection,” and a “list of things to add.” Perhaps this is not source material if you first learn about reduce , but I was very interested: http://clojure.com/blog/2012/05/08/reducers-a-library-and-model-for-collection-processing .html

+7
source

All Articles