当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


erlang iterator(Map, Order)用法及代码示例


iterator(Map, Order) -> Iterator
OTP 26.0
类型:
Map = #{Key => Value}
Order = iterator_order(Key)
Iterator = iterator(Key, Value)

返回一个Map迭代器Iterator可以使用maps:next/1使用给定的键遍历映射中的键值关联Order.

如果 Map 不是映射或者 Order 无效,则调用失败并出现 {badmap,Map} 异常。

示例(当 Orderordered 时):

> M = #{ a => 1, b => 2 }.
#{a => 1,b => 2}
> OrdI = maps:iterator(M, ordered), ok.
ok
> {K1, V1, OrdI2} = maps:next(OrdI), {K1, V1}.
{a,1}
> {K2, V2, OrdI3} = maps:next(OrdI2),{K2, V2}.
{b,2}
> maps:next(OrdI3).
none

示例(当 Orderreversed 时):

> M = #{ a => 1, b => 2 }.
#{a => 1,b => 2}
> RevI = maps:iterator(M, reversed), ok.
ok
> {K2, V2, RevI2} = maps:next(RevI), {K2, V2}.
{b,2}
> {K1, V1, RevI3} = maps:next(RevI2),{K1, V1}.
{a,1}
> maps:next(RevI3).
none

示例(当Order是算术排序函数时):

> M = #{ -1 => a, -1.0 => b, 0 => c, 0.0 => d }.
#{-1 => a,0 => c,-1.0 => b,0.0 => d}
> ArithOrdI = maps:iterator(M, fun(A, B) -> A =< B end), ok.
ok
> maps:to_list(ArithOrdI).
[{-1,a},{-1.0,b},{0,c},{0.0,d}]
> ArithRevI = maps:iterator(M, fun(A, B) -> B < A end), ok.
ok
> maps:to_list(ArithRevI).
[{0.0,d},{0,c},{-1.0,b},{-1,a}]

相关用法


注:本文由纯净天空筛选整理自erlang.org大神的英文原创作品 iterator(Map, Order) -> Iterator。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。