Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • "You don't have to do that: apply is also a variadic function that can take arguments before the final sequence argument e.g. (apply foo "one" others) You can pass any number of individual arguments before the final sequence argument to apply." But wouldn't others in this case be passed as a list, and not have its members "flattened" into the remaining arg slots? Commented Apr 22, 2019 at 2:59
  • 1
    @brundolf in that case others would have its members flattened/spread across the remaining argument positions. (apply + 1 2 [3 4]) is equivalent to (apply + 1 [2 3 4]) and (apply + [1 2 3 4]). Commented Apr 22, 2019 at 3:49
  • Odd. What's the exact algorithm there? Surely it can't be, "flatten every sequence passed to apply"? "Flatten the last argument if it's a sequence", perhaps? Commented Apr 22, 2019 at 4:17
  • @brundolf It basically assumes that the last parameter is a list. Any other arguments before it and after the function argument are treated as elements to to consd to the argument list. Commented Apr 22, 2019 at 10:59
  • 1
    It's perfectly possible with recur. Given (defn f [x & xs] ...), there are two variables that need to be passed to any recur call: x and xs. You don't need the apply, because the compiler knows exactly which arity is being dispatched to (the current one). Thus: (defn f [x & xs] (if foo 0, (recur (inc x) (rest xs)))( Commented Apr 22, 2019 at 19:51