From 0149a73d15df1f80cb46752ec3829f48c38dd230 Mon Sep 17 00:00:00 2001 From: Lukas Larsson Date: Thu, 21 Sep 2017 09:20:30 +0200 Subject: erts: Implement maps path iterator --- lib/stdlib/doc/src/maps.xml | 138 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 130 insertions(+), 8 deletions(-) (limited to 'lib/stdlib/doc') diff --git a/lib/stdlib/doc/src/maps.xml b/lib/stdlib/doc/src/maps.xml index 8c7270816b..a3afbf8e7f 100644 --- a/lib/stdlib/doc/src/maps.xml +++ b/lib/stdlib/doc/src/maps.xml @@ -33,16 +33,32 @@

This module contains functions for maps processing.

+ + + + +

An iterator representing the key value associations in a map.

+

Created using maps:iterator/1 + and maps:iterator/2.

+

Consumed by maps:next/1, + maps:filter/2, + maps:fold/3 and + maps:map/2.

+
+
+
+ Select pairs that satisfy a predicate. -

Returns a map Map2 for which predicate - Pred holds true in Map1.

+

Returns a map Map for which predicate + Pred holds true in MapOrIter.

The call fails with a {badmap,Map} exception if - Map1 is not a map, or with badarg if - Pred is not a function of arity 2.

+ MapOrIter is not a map or valid iterator, + or with badarg if Pred is not a + function of arity 2.

Example:

> M = #{a => 2, b => 3, c=> 4, "a" => 1, "b" => 2, "c" => 4}, @@ -76,12 +92,16 @@

Calls F(K, V, AccIn) for every K to value - V association in Map in + V association in MapOrIter in any order. Function fun F/3 must return a new accumulator, which is passed to the next successive call. This function returns the final value of the accumulator. The initial accumulator value Init is returned if the map is empty.

+

The call fails with a {badmap,Map} exception if + MapOrIter is not a map or valid iterator, + or with badarg if Fun is not a + function of arity 3.

Example:

> Fun = fun(K,V,AccIn) when is_list(K) -> AccIn + V end, @@ -168,6 +188,75 @@ false
+ + + Create a map iterator. + +

Returns a map iterator Iterator that can + be used by maps:next/1 + to traverse the keys and values in a map. This call is equivalent + to calling maps:iterator/2 + with an empty map as the options.

+

The call fails with a {badmap,Map} exception if + Map is not a map.

+

Example:

+ +> M = #{ a => 1, b => 2 }. +#{a => 1,b => 2} +> I = maps:iterator(M). +[{a,1},{b,2}] +> {K1, V1, I2} = maps:next(I). +{a,1,[{b,2}]} +> {K2, V2, I3} = maps:next(I2). +{b,2,[]} +> maps:next(I3). +none +
+
+ + + + Create a map iterator. + +

Returns a map iterator Iterator that can + be used by maps:next/1 + to traverse the keys and values in a map. The iterator will behave + according to the given Opts.

+ + optimize + + Configures whether the iterator should be optimized for speed + or for memory consumption. Default is speed. + + ordered + + Configures whether the iterator should return the key value + associations ordered on the keys according to erlang term order + or not. The default is false. + + +

Both options can be configured at the same time.

+

The call fails with a {badmap,Map} exception if + Map is not a map.

+

Example:

+ +> M = maps:from_list([{I,I} || I <- lists:seq(1,50)]). +#{45 => 45,6 => 6,2 => 2,49 => 49,41 => 41,33 => 33,42 => 42, + 43 => 43,10 => 10,9 => 9,19 => 19,14 => 14,5 => 5,18 => 18, + 31 => 31,22 => 22,29 => 29,21 => 21,27 => 27,24 => 24, + 47 => 47,40 => 40,30 => 30,23 => 23,28 => 28,46 => 46, + 16 => 16,38 => 38,4 => 4,...} +> MemoryIter = maps:iterator(M, #{ optimize => memory }), ok. +ok +> {K1, V1, _} = maps:next(MemoryIter), {K1, V1}. +{46,46} +> OrderedIter = maps:iterator(M, #{ ordered => true, optimize => memory }), ok. +ok +> {K2, V2, _} = maps:next(OrderedIter), {K2, V2}. +{1,1} +
+
+ @@ -188,12 +277,16 @@ false
-

Produces a new map Map2 by calling function +

Produces a new map Map by calling function fun F(K, V1) for every K to value - V1 association in Map1 in + V1 association in MapOrIter in any order. Function fun F/2 must return value V2 to be associated with key K - for the new map Map2.

+ for the new map Map.

+

The call fails with a {badmap,Map} exception if + MapOrIter is not a map or valid iterator, + or with badarg if Fun is not a + function of arity 2.

Example:

> Fun = fun(K,V1) when is_list(K) -> V1*2 end, @@ -233,6 +326,35 @@ false
+ + + Get the next key and value from an iterator. + +

Returns the next key-value association in + Iterator and a new iterator for the + remaining associations in the iterator. +

+

+ If there are no more associations in the iterator, + none is returned. +

+

Example:

+ +> Map = #{a => 1, b => 2, c => 3}. +#{a => 1,b => 2,c => 3} +> Iter = maps:iterator(Map). +[{a,1},{b,2},{c,3}] +> {_, _, Iter1} = maps:next(Iter). +{a,1,[{b,2},{c,3}]} +> {_, _, Iter2} = maps:next(Iter1). +{b,2,[{c,3}]} +> {_, _, Iter3} = maps:next(Iter2). +{c,3,[]} +> maps:next(Iter3). +none +
+
+ -- cgit v1.2.3