# Conditional NamedTuple merge

**URL:** <https://forum.crystal-lang.org/t/conditional-namedtuple-merge/5158>\
**Category:** Help & Support\
**Created:** [December 7, 2022, 5:35pm UTC](https://forum.crystal-lang.org/t/conditional-namedtuple-merge/5158 "2022-12-07T17:35:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [December 7, 2022, 5:35pm UTC](https://forum.crystal-lang.org/t/conditional-namedtuple-merge/5158/1 "2022-12-07T17:35:10Z")

</div>

Say I have the following code:

```crystal
opts = {title: "abc", header: "test", value: 42}
popts = {title: "xyz", width: 10}

new_opts = opts.merge(popts)
p! new_opts # => {header: "test", value: 42, title: "xyz", width: 10}

```

How can I merge `opts` and `popts` while excluding `popts` keys missing in `opts` ?  
Expected result :

```auto
# => {header: "test", value: 42, title: "xyz"}

```

EDIT: Solved by creating an `update_from` method in `struct NamedTuple`, adapted from the `merge` method, but as monkey-patching is not recommanded, is there another solution ?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [December 7, 2022, 10:30pm UTC](https://forum.crystal-lang.org/t/conditional-namedtuple-merge/5158/2 "2022-12-07T22:30:28Z")

</div>

> [@hutou](#):
>
> is there another solution

Define your own struct to represent this data and define a custom `#merge` method on it.

---

<div class="post-metadata">

**Author:** ![HertzDevil](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hertzdevil/32/1023_2.png) [@HertzDevil](https://forum.crystal-lang.org/u/HertzDevil)\
**Post date:** [December 8, 2022, 2:24am UTC](https://forum.crystal-lang.org/t/conditional-namedtuple-merge/5158/3 "2022-12-08T02:24:01Z")

</div>

You could define it as a non-member method:

```crystal
def update_from(dst : NamedTuple, from src : NamedTuple)
  update_from_impl(dst, src)
end

def update_from_impl(dst : T, from src : U) forall T, U
  {% begin %}
    {% u_keys = U.keys %}
    NamedTuple(
      {% for k in T.keys %}
        {{ k.stringify }}: typeof(
          dst[{{ k.stringify }}],
          {% if u_keys.includes?(k) %}
            src[{{ k.stringify }}],
          {% end %}
        ),
      {% end %}
    ).new(
      {% for k in T.keys %}
        {% receiver = u_keys.includes?(k) ? "src" : "dst" %}
        {{ k.stringify }}: {{ receiver.id }}[{{ k.stringify }}],
      {% end %}
    )
  {% end %}
end

p update_from(
  {title: "abc", header: "test", value: 42},
  {title: "xyz", width: 10},
)

```

Unfortunately you cannot infer the `NamedTuple` instantiation in one step via `NamedTuple(**T) forall T`, in contrast to the working `Tuple(*T) forall T`.

---

<div class="post-metadata">

**Author:** ![hutou](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/hutou/32/1337_2.png) [@hutou](https://forum.crystal-lang.org/u/hutou)\
**Post date:** [December 8, 2022, 11:17am UTC](https://forum.crystal-lang.org/t/conditional-namedtuple-merge/5158/4 "2022-12-08T11:17:04Z")

</div>

Thanks a lot, @HertzDevil, that’s exactly what I was looking for (and besides, I now have some non-trivial code to study 😉)!

---

<div class="post-metadata">

**Author:** ![zw963](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/zw963/32/1623_2.png) [@zw963](https://forum.crystal-lang.org/u/zw963)\
**Post date:** [December 8, 2022, 5:26pm UTC](https://forum.crystal-lang.org/t/conditional-namedtuple-merge/5158/5 "2022-12-08T17:26:31Z")

</div>

If use ruby, you can get expected result use

```ruby
opts = {title: "abc", header: "test", value: 42}
popts = {title: "xyz", width: 10}
result = opts.merge(popts.slice(*opts.keys))

```

Unfortunately, Crystal no NamedTuple#slice equivalent, you can create a feature request for this.

```crystal
struct NamedTuple
  def slice(*keys : *U) forall U
    h = self.to_h.select { |x| keys.map(&.to_s).includes? x.to_s }

    h
  end
end

x = {x: 100, y: 200, z: 300}
p x.slice("x", "y")

# => {:x => 100, :y => 200}

```

Above method return a Hash, i thought it is hard to convert above h back to a tuple, i don’t know how to do that.
