Skip to content

Complete maps.erl take/2 etc.#2355

Open
petermm wants to merge 3 commits into
atomvm:release-0.7from
petermm:add-maps-take-update-with-with-without
Open

Complete maps.erl take/2 etc.#2355
petermm wants to merge 3 commits into
atomvm:release-0.7from
petermm:add-maps-take-update-with-with-without

Conversation

@petermm

@petermm petermm commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

petermm added 3 commits July 4, 2026 09:17
OTP-compatible implementations of maps:take/2,
maps:update_with/3, maps:update_with/4, maps:with/2 and
maps:without/2 to the estdlib maps module.

Each function preserves OTP error semantics:

- {badmap, Map} when the map argument is not a map (taking
  precedence over badarg when multiple arguments are wrong)
- badarg when Keys is not a list (with/2, without/2) or Fun
  is not a function of arity 1 (update_with/3,4)
- {badkey, Key} from update_with/3 only when Map is a valid
  map, Fun is arity-1 and Key is missing
- update_with/4 inserts Init verbatim without invoking Fun
  when the key is absent

Tests cover the happy paths plus error precedence, duplicate
keys in with/without, a value of the atom 'error' in take/2
(must not be confused with the missing-key result), and the
guarantee that update_with/4 does not call Fun on insert.

Signed-off-by: Peter M <petermm@gmail.com>
…iterator_valid

Add six pure Erlang functions to the estdlib maps module for
improved OTP compatibility:

- filtermap/2: Combined filter and map operation
- intersect/2: Map intersection (values from second map)
- intersect_with/3: Map intersection with value combiner
- groups_from_list/2: Group list elements by key function
- groups_from_list/3: Group with value transformation
- is_iterator_valid/1: Iterator validation (internal, exported)

All functions follow OTP semantics exactly, include comprehensive
error handling ({badmap, Map}, badarg), work with both maps and
iterators where applicable, and have full test coverage.

Signed-off-by: Peter M <petermm@gmail.com>
Signed-off-by: Peter M <petermm@gmail.com>
Comment thread libs/estdlib/src/maps.erl
%% An iterator becomes invalid if it has been exhausted or if the underlying
%% map has been modified.
%%
%% This is an internal function, primarily used by other functions in this module.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This comment seems wrong.

Comment thread libs/estdlib/src/maps.erl
%% `Combiner(Key, Value1, Value2)' where `Value1' is from `Map1' and `Value2'
%% is from `Map2'.
%%
%% This function raises a `badmap' error if either `Map1' or `Map2' is not a map,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

It seems error precedence diverges from OTP. This could be asserted with testing maps:intersect_with(BadCombiner, NotAMap, #{})

Comment thread CHANGELOG.md
- Added USB CDC port drivers for ESP32, RP2, and STM32 platforms
- Added `maps:take/2`, `maps:update_with/3`, `maps:update_with/4`, `maps:with/2`,
`maps:without/2`, `maps:filtermap/2`, `maps:intersect/2`, `maps:intersect_with/3`,
`maps:groups_from_list/2` and `maps:groups_from_list/3` to the estdlib `maps` module

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This should be shortened (llm verbosity it seems) or it should mention is_iterator_valid/1

),
#{b => 20, d => 40}
),
%% Works with iterators

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We need to test ordered iterators as well.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants