# Making an iterator that splats keys and values quickly into kwargs

**URL:** <https://discourse.julialang.org/t/making-an-iterator-that-splats-keys-and-values-quickly-into-kwargs/89357>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [October 27, 2022, 10:57am UTC](https://discourse.julialang.org/t/making-an-iterator-that-splats-keys-and-values-quickly-into-kwargs/89357 "2022-10-27T10:57:49Z")\
**Posts on this page:** 1\
**Showing post:** 14

<div class="post-metadata">

**Author:** ![uniment](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/uniment/32/24532_2.png) [@uniment](https://discourse.julialang.org/u/uniment)\
**Post date:** [November 1, 2022, 1:19am UTC](https://discourse.julialang.org/t/making-an-iterator-that-splats-keys-and-values-quickly-into-kwargs/89357/14 "2022-11-01T01:19:11Z")

</div>

> [@Raf](#):
>
> `@assume_effects` is the newer slightly safer way of doing things like this

Nice!

```julia
julia> Base.@assume_effects :foldable function my_merge_names(an::Tuple{Vararg{Symbol}}, bn::Tuple{Vararg{Symbol}})
           @nospecialize an bn
           names = Symbol[an...]
           for n ∈ bn
               n ∈ an || push!(names, n)
           end
           (names...,)
       end
my_merge_names (generic function with 1 method)

julia> @btime my_merge_names((:a,:b,:c),(:d,:e,:f))
  1.000 ns (0 allocations: 0 bytes)
(:a, :b, :c, :d, :e, :f)

```

> [@Raf](#):
>
> `@generated` functions are another way.

I need more practice figuring out how to translate a function like `merge_names` into a `@generated` form… the manual examples were basically just reimplementations of multiple dispatch.

> [@Raf](#):
>
> From my example you can see you don’t always need these macros.

Yes absolutely! This is just as performant, and doesn’t rely on black magic:

```julia
julia> function my_merge_names(an::Tuple{Vararg{Symbol}}, bn::Tuple{Vararg{Symbol}})
           @nospecialize an bn
           names = reduce(bn; init=an) do acc, k
               k ∈ an ? acc : (acc..., k)
           end
           (names...,)
       end
my_merge_names (generic function with 1 method)

julia> @btime my_merge_names((:a,:b,:c),(:d,:e,:f))
  1.000 ns (0 allocations: 0 bytes)
(:a, :b, :c, :d, :e, :f)

```

> [@oxinabox](#):
>
> many functions in Base are much older than generators being easy in julia.

Trying a sugary generator approach…

```julia
julia> function my_merge_names(an::Tuple{Vararg{Symbol}}, bn::Tuple{Vararg{Symbol}})
           @nospecialize an bn
           (an..., (k for k ∈ bn if k ∉ an)...)
       end
my_merge_names (generic function with 1 method)

julia> @btime my_merge_names((:a,:b,:c),(:d,:e,:f))
  2.000 ns (0 allocations: 0 bytes)
(:a, :b, :c, :d, :e, :f)

```

_Almost_ as good. I wonder where the extra 1ns is coming from…

---

_[View the full topic](https://discourse.julialang.org/t/making-an-iterator-that-splats-keys-and-values-quickly-into-kwargs/89357)._
