# \[ANN\] NamedTupleTools.jl

**URL:** <https://discourse.julialang.org/t/ann-namedtupletools-jl/15374>\
**Category:** Community\
**Tags:** package, announcement\
**Created:** [September 23, 2018, 11:11am UTC](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374 "2018-09-23T11:11:39Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 23, 2018, 11:11am UTC](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374/1 "2018-09-23T11:11:39Z")

</div>

[NamedTupleTools](https://github.com/JeffreySarnoff/NamedTupleTools.jl) is a small package that makes doing a few things with `NamedTuples` a bit easier.  
_(Julia v1.0)_

```julia
using NamedTupleTools

julia> namedtuple(:a, :b, :c)(1, 2.0, "three")
(a = 1, b = 2.0, c = "three")

#=
    namedtuple( name1, name2, .. )
    namedtuple( (name1, name2, ..) )
       where the `names` are all `Symbols` or all `Strings`

Generate a NamedTuple prototype by specifying or obtaining the fieldnames.
The prototype is applied to fieldvalues, giving a completed NamedTuple.
=#

julia> ntproto = namedtuple( :a, :b, :c )
NamedTuple{(:a, :b, :c),T} where T<:Tuple

julia> nt123 = ntproto(1, 2, 3)
(a = 1, b = 2, c = 3)

julia> ntAb3 = ntproto("A", "b", 3)
(a = "A", b = "b", c = 3)

julia> isprototype(ntproto)
true

julia> isprototype(nt123)
false

julia> delete!(nt123, :a) === (b = 2, c = 3)
true
julia> delete!(nt123, :a, :c) === delete!(nt123, (:a, :c)) === (b = 2,)
true
julia> delete!(ntproto, :b) === namedtuple(:a, :c)
true

julia> ntproto1 = namedtuple(:a, :b);
julia> ntproto2 = namedtuple(:b, :c);

julia> merge(ntproto1,ntproto2)
NamedTuple{(:a, :b, :c),T} where T<:Tuple
```

---

<div class="post-metadata">

**Author:** ![Christopher\_Fisher](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/christopher_fisher/32/26132_2.png) [@Christopher\_Fisher](https://discourse.julialang.org/u/Christopher_Fisher)\
**Post date:** [September 23, 2018, 12:35pm UTC](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374/2 "2018-09-23T12:35:50Z")

</div>

This looks pretty useful. Unfortunately, however, I was unable to add the package.

```julia
(v0.7) pkg> add NamedTupleTools
  Updating registry at `~/.julia/registries/General`
  Updating git-repo `https://github.com/JuliaRegistries/General.git`
 Resolving package versions...
ERROR: Unsatisfiable requirements detected for package NamedTupleTools [d9ec5142]:
 NamedTupleTools [d9ec5142] log:
 ├─possible versions are: [0.1.0, 0.2.0] or uninstalled
 ├─restricted to versions * by an explicit requirement, leaving only versions [0.1.0, 0.2.0]
 └─restricted by julia compatibility requirements to versions: uninstalled — no versions left

```

---

<div class="post-metadata">

**Author:** ![tobias.knopp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobias.knopp/32/7551_2.png) [@tobias.knopp](https://discourse.julialang.org/u/tobias.knopp)\
**Post date:** [September 23, 2018, 12:38pm UTC](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374/3 "2018-09-23T12:38:41Z")

</div>

the package has 1.0 in its `REQUIRE` file and in turn cannot be installed on Julia 0.7.

---

<div class="post-metadata">

**Author:** ![devel-chm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/devel-chm/32/3572_2.png) [@devel-chm](https://discourse.julialang.org/u/devel-chm)\
**Post date:** [September 23, 2018, 3:34pm UTC](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374/4 "2018-09-23T15:34:21Z")

</div>

Is there a technical reason that a new module written for 1.0 cannot work with version 0.7 save the information in REQUIRE?

Having Julia 0.7 as a version that is basically 1.0 + deprecation warnings is a very useful tool as the 1.0 language stabilizes and modules/packages are ported.

As a Julia 0.7 user, it would be nice to see this continue if possible.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [September 23, 2018, 5:43pm UTC](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374/5 "2018-09-23T17:43:31Z")

</div>

For the most part, a new module written for 1.0 will work with 0.7. However, they are not identical. I do not use 0.7 anymore, and so cannot see first hand what happens with that version when running new code I write. Whatever distinctions there may be in some subtle aspect of behavior, I do not represent that my new work is right for versions prior to 1.0. I appreciate your note, and – at least for this package, it is a single file that you could copy/paste into the REPL if you want. While that is not ideal for 0.7 users, I’d rather be forward focused.

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 15, 2019, 7:57am UTC](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374/6 "2019-06-15T07:57:48Z")

</div>

The [latest release](https://github.com/JeffreySarnoff/NamedTupleTools.jl) introduces an easier way to construct `NamedTuples` when the field names and the values to assign are available together as args to your function, or for direct use in the REPL.

```julia
using NamedTupleTools

namesof_values = (:instrument, :madeby)
matching_values = ("violin", "Stradivarius")

nt = namedtuple(namesof_values, matching_values)
(instrument = "violin", madeby = "Stradivarius")

```

- The `names` and the `values` may be given as a `Tuple` or as a `Vector`, independently.
- The names may be given as `Symbols` or as `Strings`. The values can be of any type or types.

* * *

These examples [convert](https://github.com/JeffreySarnoff/NamedTupleTools.jl/blob/master/README.md#struct-construction-conversion), [merge](https://github.com/JeffreySarnoff/NamedTupleTools.jl/blob/master/README.md#merge), or [prototype](https://github.com/JeffreySarnoff/NamedTupleTools.jl#use-namedtuple-prototypes) NTs, and [delete fields](https://github.com/JeffreySarnoff/NamedTupleTools.jl/blob/master/README.md#delete).

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 15, 2019, 10:43am UTC](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374/7 "2019-06-15T10:43:41Z")

</div>

> [@JeffreySarnoff](#):
>
> ```julia
> using NamedTupleTools
> 
> namesof_values = (:instrument, :madeby)
> matching_values = ("violin", "Stradivarius")
> 
> nt = namedtuple(namesof_values, matching_values)
> (instrument = "violin", madeby = "Stradivarius")
> 
> ```

Here is a way of doing it in vanilla Julia:

```julia
julia> (;zip(namesof_values, matching_values)...,)
(instrument = "violin", madeby = "Stradivarius")

```

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [June 15, 2019, 10:47am UTC](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374/8 "2019-06-15T10:47:17Z")

</div>

good to know (and news to me)  
what does the leading semi-colon do, specifically? (in addition to making it work)

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [June 15, 2019, 10:49am UTC](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374/9 "2019-06-15T10:49:18Z")

</div>

It is similar how you programatically pass keywords to functions

```julia
f(;kwargs...) = @show kwargs
d = Dict(:a => "foo", :b => "bar")
f(; d...)
# kwargs = Base.Iterators.Pairs(:a=>"foo",:b=>"bar")

```

Without `;` you would just get a normal tuple.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [June 15, 2019, 11:51am UTC](https://discourse.julialang.org/t/ann-namedtupletools-jl/15374/10 "2019-06-15T11:51:47Z")

</div>

Here is another way to do it:

```julia
julia> # Data
julia> namesof_values = (:instrument, :madeby)
(:instrument, :madeby)

julia> matching_values = ("violin", "Stradivarius")
("violin", "Stradivarius")

julia> # Another way to do it

julia> NamedTuple{namesof_values}(matching_values)
(instrument = "violin", madeby = "Stradivarius")

```
