Re: Using asynchronous clojure function

28 views
Skip to first unread message

Brandon R

unread,
Dec 13, 2020, 1:58:53 PM12/13/20
to clo...@googlegroups.com
Hi Ganesh,

The function called wrap-content-type takes in another function (handler) and a content-type (string), and returns a function. The function that it returns takes in a request, which is a Clojure map, that has the keys that you see in the ring docs here: https://github.com/ring-clojure/ring/wiki/Concepts#requests

That function that is returned, that takes in a request, is known as middleware. It calls the handler passed into wrap-content-type, which is another handler function (returns a ring response), then attaches the content type passed into wrap-content-type to the headers of the ring response. So the response might then look like {:headers {"Content-Type": "application/json"}} (if "application/json" was passed as content-type), along with whatever other keys/values were on the response before it was passed into the function.

FYI, the #beginners channel of the Clojurians Slack is super helpful for general questions like this.

Happy coding,
Brandon

On Sun, Dec 13, 2020 at 7:08 AM Ganesh Neelekani <ganeshn...@gmail.com> wrote:
Hello Team,

I am a moderate Clojure user and now I planned to explore more Clojure, I used to 
write different functions every time where I could write asynchronous functions. 

As came from object-oriented background and trying to understand asynchronous functions

I took some example on using fn functions

((fn [& nums] (/ (apply + nums) (count nums))) 1 2 3 4)

In the above statement, 1 2 3 4 is added after the function defined as input,
But in the below function I did not understand how fn behaves, what is the input for a request, Or what is the methodology , Basically I am not understanding the flow,

(defn wrap-content-type [handler content-type] 
     (fn [request] 
           (let [response (handler request)] 
                (assoc-in response [:headers "Content-Type"] content-type))))

Can someone help me, please?

Thanks,
Ganesh N

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/c460dada-9752-4f7f-931d-3766fe64411cn%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages