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))
aisamu
November 14, 2019, 12:49pm
3
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"))
aisamu
November 14, 2019, 12:55pm
4
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
orestis
November 14, 2019, 2:13pm
5
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
I understand how I was mistaken now as well.
Thanks @orestis and @aisamu !!
1 Like
system
Closed
May 15, 2020, 5:29am
7
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.