The following is faster, though it does not scale very well for large 
indices because of ridiculous if chains...

```
@generated function droptup{N,T,i}(x::NTuple{N,T}, ::Type{Val{i}})
    @assert 1 <= i <= N
    args = [:(x[$j]) for j in deleteat!([1:N...], i)]
    Expr(:tuple, args...)
end

@generated function droptup{N,T}(x::NTuple{N,T}, i::Int)
    quote
        @nif $N d->(i==d) d-> droptup(x, Val{d})
    end
end

using BenchmarkTools
x = (10,20,30,40)

@benchmark droptup($x, 4)
```

BenchmarkTools.Trial: 
  samples:          10000
  evals/sample:     1000
  time tolerance:   5.00%
  memory tolerance: 1.00%
  memory estimate:  0.00 bytes
  allocs estimate:  0
  minimum time:     6.00 ns (0.00% GC)
  median time:      6.00 ns (0.00% GC)
  mean time:        6.11 ns (0.00% GC)
  maximum time:     70.00 ns (0.00% GC)



On Friday, June 24, 2016 at 4:50:59 PM UTC+2, jw3126 wrote:
>
> Okay thanks, it works! However it has extremely poor performance. I would 
> love to do this stack allocated. 
>
> ```
> using BenchmarkTools
> function subtuple(t::Tuple,i::Integer)
>     idx = 1:length(t)
>     idx = setdiff(idx,i)
>     t[idx]
> end
>
> @benchmark subtuple($(1,2,3,4), $1)
> ```
>
> BenchmarkTools.Trial: 
>   samples:          10000
>   evals/sample:     10
>   time tolerance:   5.00%
>   memory tolerance: 1.00%
>   memory estimate:  1.33 kb
>   allocs estimate:  22
>   minimum time:     1.52 μs (0.00% GC)
>   median time:      1.69 μs (0.00% GC)
>   mean time:        1.96 μs (9.07% GC)
>   maximum time:     323.58 μs (98.21% GC)
>
>
> On Friday, June 24, 2016 at 4:42:17 PM UTC+2, STAR0SS wrote:
>>
>> You can do something like that:
>>
>> t = tuple(1,2,3,4)
>>
>> function subtuple(t::Tuple,i::Integer)
>>     idx = 1:length(t)
>>     idx = setdiff(idx,i)
>>     t[idx]
>> end
>>
>> subtuple(t,3)
>>
>> (1,2,4)
>>
>

Reply via email to