If I have a recursive function that builds up a tuple, where in each function it creates a new tuple by appending/prepending an element to the tuple, is it better to grow it towards the front or towards the back? Does it matter? Is one of them easier on the compiler even if they end up ultimately performing the same?
Take this silly function as an example, and imagine that i don't care whether the numbers are ascending or descending (since I can always adjust the calling code to work either way):
julia> function tuple_one_through_n(n, t=())::Tuple
if n === 0
t
else
tuple_one_through_n(n-1, (n, t...))
end
end
tuple_one_through_n (generic function with 2 methods)
julia> tuple_one_through_n(5)
(1, 2, 3, 4, 5)
Is there any reason to prefer splatting t
before n
or after? Thanks!
EDIT: For more context: we're appending tuples like this because we're actually using this as an immutable function trace, and creating a new tuple by appending is thread safe, so it works even if we spawn new tasks: each task will then get it's own stack trace growing that traces all the callers.
I think a better option would probably be something like an immutable PersistentVector
from https://github.com/JuliaCollections/FunctionalCollections.jl/blob/master/README.md, but for the easiest first pass I was just using tuples, and i was wondering if there was any reason to prefer one order or the other. :) Since some people probably have a valid use case for growing Tuples, I thought I'd ask.
set
? β Rowenarowland