Skip to content

Add Map.intersect/2 and Map.intersect/3 #12336

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 13, 2023

Conversation

ed-flanagan
Copy link
Contributor

Following up from https://elixirforum.com/t/map-intersection-2/53121/12. I didn't see any duplicates, hopefully it's okay I pushed up.

My first PR, so tried to follow https://github.com/elixir-lang/elixir#contributing & https://github.com/elixir-lang/elixir/blob/main/CODE_OF_CONDUCT.md as best as I could.


  • Add corresponding functions for Erlang's :maps.intersect/2 & :maps.intersect_with/3.
  • Largely cargo-culted from Map.merge/3, including the tests & docs. More than happy to extend these.
  • I extended the diff list in KeywordTest since I wasn't sure if extending Keyword would be out-of-scope. Happy to add a Keyword.intersect function as well, if desired.
  • make test_stdlib resulted in 1948 doctests, 4187 tests, 0 failures, 13 excluded
  • Test from running ./bin/iex
    ❯ ./bin/iex
    Erlang/OTP 25 [erts-13.1.3] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit] [dtrace]
    
    Interactive Elixir (1.15.0-dev) - press Ctrl+C to exit (type h() ENTER for help)
    iex(1)> Map.intersect(%{a: 1, b: 2}, %{b: 2, c: 3})
    %{b: 2}
    iex(2)> Map.intersect(%{a: 1, b: 2}, %{b: 2, c: 3}, fn _k, v1, v2 -> v1 + v2 end)
    %{b: 4}
    

`map1`), and `value2` (the value of `key` in `map2`). The value returned by
`fun` is used as the value under `key` in the resulting map.

Inlined by the compiler.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the case, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly?

map1), and value2 (the value of key in map2). The value returned by
fun is used as the value under key in the resulting map.

I tried to adapt the same wording used in Map.merge, akin to https://www.erlang.org/doc/man/maps.html#intersect_with-3.

Inlined by the compiler.

I cargo-culted this since other functions that just pass to Erlang functions included it (even without defdelegate). E.g.

def has_key?(map, key), do: :maps.is_key(key, map)
or
:maps.update(key, value, map)
. But I'm not entirely sure, happy to remove

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AFAIK you'd have to add the inline rules at https://github.com/elixir-lang/elixir/blob/main/lib/elixir/src/elixir_rewrite.erl, which I believe is expected in this case.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer documentation use language based on the Elixir MapSet documentation rather than the Erlang :maps docs. For example MapSet.intersection/2 docs include

Returns a set containing only members that map_set1 and map_set2 have in common.

This is much nicer than

Intersects two sets into one.

@whatyouhide whatyouhide changed the title Add Map.intersect Add Map.intersect/2 and Map.intersect/3 Jan 13, 2023
Copy link
Member

@whatyouhide whatyouhide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fantastic!

@josevalim josevalim merged commit 2410811 into elixir-lang:main Jan 13, 2023
@josevalim
Copy link
Member

💚 💙 💜 💛 ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

7 participants