Cljs: Use `apply` to draw image

Hello Community…

I am trying to repeat an image by calling a function

(defn spa []
  [:div.wrapper
   [:div {:align "center"} ; 2fr
    (apply draw-from-vec ["dev-1"])
    (apply draw-from-vec ["dev-2"])
    (apply drawfrom-vec ["dev-3"]))

draw-from-vec looks like this:-

(defn draw-from-vec [dcn-id]
  [:div {:class "crop-container" :align "center"}
   [:img {:src "img/tmp.jpg" :id dcn-id :height 100 :on-click (fn []
                                                                (prn dcn-id)
                                                                (reset! app-state (rawdata/get-apps dcn-id)))}]])

Why is apply draw-from-vec ["dev-1" "dev-2" "dev-3"] showing 1 image? Whereas apply on each is displaying 3? I want to be able to get the result of using apply thrice by a single statement. Any idea?

Thanks

1 Like

Doing something like this works fine:

(for [x ["dev-1" "dev-2" "dev-3"]]
      (draw-from-vec x))

Welcome!

Unless your example is simpler than the real use-case, it might be preferable to just call the function without the apply indirection

(=
 (apply draw-from-vec ["dev-1"])
 (draw-from-vec "dev-1"))

Calling apply with more than one element in the collection means applying the function once to all the arguments, not once per argument

(=
 (apply draw-from-vec ["dev-1" "dev-2" "dev-3"])
 (draw-from-vec "dev-1" "dev-2" "dev-3"))

The for evaluates to:

((draw-from-vec "dev-1")
 (draw-from-vec "dev-2")
 (draw-from-vec "dev-3"))

Which is different than

(draw-from-vec "dev-1" "dev-2" "dev-3")
1 Like

You might want map instead:

(map draw-from-vec ["dev-1" "dev-2" "dev-3"])

is equivalent to

(for [x ["dev-1" "dev-2" "dev-3"]]
   (draw-from-vec x))
1 Like

(map) works :slight_smile:

I understand how I was mistaken now as well.

Thanks @orestis and @aisamu!!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.