How can you assert that a map in a variable matches another map that is a superset?

Erlang -- Pattern Matching

In a pattern matching, a left-hand side pattern is matched against a right-hand side term.

The phrasing suggests to me that patterns are very different from terms - patterns aren’t listed under Data Types.

Pinning ‘pins’ the binding - it expresses that no re-binding of that name should take place; at that point matching succeeds if both values are equal otherwise there will be a :badmatch error.

Erlang doesn’t need pinning because it doesn’t allow re-binding. But a simple name to the left of the match operator (=) means:

  • if the name is unbound, bind the name to the right hand side (and return the bound/matched value)
  • if the name is bound and the name is bound to a value equal to the right hand side, succeed (and return the matched value)
  • if the name is bound and the name is bound to a value not equal to the right hand side, fail (and throw a badmatch error)

FYI: Map is an Enumerable so Enum.all?(map1, &(&1 in map2)) should work - I just like to be explicit.

2 Likes