In the Om.next wiki, there is example code like this (code listing at the bottom of the linked page):
(apply dom/ul nil
(map person list))
Since dom/ul takes a variable number of arguments, what is the benefit of using apply vs this:
(dom/ul nil (map person list))
This second approach seems more analogous to passing an array as children in vanilla React, which is supported.
Actual question (response to comment): Is apply necessary? If so, why doesn't the latter approach work? If not, what benefit is gained from using apply like in the example?
The ClojureScript signature of
dom/ulis apparently something like(defn ul [attrs & children] ...). Herechildrenis a vararg. The caller can pass in 0 or more children. Therefore, the following invocations are valid:Now suppose you have a collection of elements that you'd like to wrap in a ul. The collection maybe comes from
(map person list), which returns a sequence of elements. If ul is invoked like(dom/ul nil (map person list)), only a single child argument is passed toul, and it is of the wrong type. Each child is expected to be an element, but a collection of element is passed in instead.applyto the rescue. The job ofapplyis deal with the situation where we have a collection of arguments, which must be passed in individually. So,(dom/ul nil (person jack) (person jill))is equivalent to(apply dom/ul nil (map person [jack jill])).applyis necessary in this case to make sure the arguments are passed toulas that function is expecting them. Skippingapplyis not an option.