地图中的 Erlang 提取
Erlang Extract from Map
下面是
类型记录中的一个对象
requirements = [] :: [term()],
我们像下面这样提取 Requirements 对象
Requirements = Records#record.requirements
我需要独立于 any.latlong_radius 元组的纬度、经度、半径。我们怎样才能把它提取出来?
"requirements": {
"supply_type": {
"$tuple": [1, {
"$tuple": ["mobile_app", "nil", "nil"]
}]
},
"has_app_name": true,
"any": {
"latlong_radius": {
"$tuple": [33.042334, -96.734884, 16093.4]
}
},
},
只需用双引号 ''
分配变量,如下所示:
Latlong_radius = Any#any.latlong_radius
Res = Latlong_radius#latlong_radius.'$tuple'
-module(my).
-compile(export_all).
-record(requirements, {supply_type,
has_app_name,
any} ).
get_requirements() ->
#requirements{
supply_type = #{"$tuple" => [1, 2, 3]},
has_app_name = true,
any = #{"latlong_radius" =>
#{"$tuple" => [33.042334, -96.734884, 16093.4]}
}
}.
go() ->
Requirements = get_requirements(),
io:format("requirements: ~p~n", [Requirements]),
Any = Requirements#requirements.any,
#{"latlong_radius" :=
#{"$tuple" := [Lat, Lon, Rad]}
} = Any,
io:format("Lat: ~w, Lon: ~w, Rad: ~w~n", [Lat, Lon, Rad]).
在shell中:
51> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}
52> my:go().
requirements: {requirements,#{"$tuple" => [1,2,3]},
true,
#{"latlong_radius" =>
#{"$tuple" =>
[33.042334,-96.734884,16093.4]}}}
Lat: 33.042334, Lon: -96.734884, Rad: 16093.4
ok
另一方面,如果您的数据结构一直向下映射(这使得您发布的有关记录和列表规范的所有内容都无关紧要):
-module(my).
-compile(export_all).
get_requirements() ->
#{"requirements" =>
#{
"supply_type" => #{"$tuple" => [1, 2, 3]},
"has_app_name" => true,
"any" => #{"latlong_radius" =>
#{"$tuple" => [33.042334, -96.734884, 16093.4]}
}
}
}.
go() ->
Requirements = get_requirements(),
io:format("requirements: ~p~n", [Requirements]),
#{"requirements" :=
#{
"any" :=
#{
"latlong_radius" :=
#{
"$tuple" := [Lat, Lon, Rad]
}
}
}
} = Requirements,
io:format("Lat: ~w, Lon: ~w, Rad: ~w~n", [Lat, Lon, Rad]).
在shell中:
70> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}
71> my:go().
requirements: #{"requirements" =>
#{"any" =>
#{"latlong_radius" =>
#{"$tuple" => [33.042334,-96.734884,16093.4]}},
"has_app_name" => true,
"supply_type" => #{"$tuple" => [1,2,3]}}}
Lat: 33.042334, Lon: -96.734884, Rad: 16093.4
ok
然而,对于深度嵌套的映射,试图使模式匹配语法正确是太容易出错了,所以我会使用 maps:get/2
代替:
-module(my).
-compile(export_all).
get_requirements() ->
#{"requirements" =>
#{
"supply_type" => #{"$tuple" => [1, 2, 3]},
"has_app_name" => true,
"any" => #{"latlong_radius" =>
#{"$tuple" => [33.042334, -96.734884, 16093.4]}
}
}
}.
go() ->
Map = get_requirements(),
io:format("Map: ~p~n", [Map]),
Requirements = maps:get("requirements", Map),
Any = maps:get("any", Requirements),
LLR = maps:get("latlong_radius", Any),
#{"$tuple" := [Lat, Long, Radius]} = LLR,
io:format("Lat: ~w, Lon: ~w, Rad: ~w~n", [Lat, Long, Radius]).
简单多了。
下面是
类型记录中的一个对象requirements = [] :: [term()],
我们像下面这样提取 Requirements 对象
Requirements = Records#record.requirements
我需要独立于 any.latlong_radius 元组的纬度、经度、半径。我们怎样才能把它提取出来?
"requirements": {
"supply_type": {
"$tuple": [1, {
"$tuple": ["mobile_app", "nil", "nil"]
}]
},
"has_app_name": true,
"any": {
"latlong_radius": {
"$tuple": [33.042334, -96.734884, 16093.4]
}
},
},
只需用双引号 ''
分配变量,如下所示:
Latlong_radius = Any#any.latlong_radius
Res = Latlong_radius#latlong_radius.'$tuple'
-module(my).
-compile(export_all).
-record(requirements, {supply_type,
has_app_name,
any} ).
get_requirements() ->
#requirements{
supply_type = #{"$tuple" => [1, 2, 3]},
has_app_name = true,
any = #{"latlong_radius" =>
#{"$tuple" => [33.042334, -96.734884, 16093.4]}
}
}.
go() ->
Requirements = get_requirements(),
io:format("requirements: ~p~n", [Requirements]),
Any = Requirements#requirements.any,
#{"latlong_radius" :=
#{"$tuple" := [Lat, Lon, Rad]}
} = Any,
io:format("Lat: ~w, Lon: ~w, Rad: ~w~n", [Lat, Lon, Rad]).
在shell中:
51> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}
52> my:go().
requirements: {requirements,#{"$tuple" => [1,2,3]},
true,
#{"latlong_radius" =>
#{"$tuple" =>
[33.042334,-96.734884,16093.4]}}}
Lat: 33.042334, Lon: -96.734884, Rad: 16093.4
ok
另一方面,如果您的数据结构一直向下映射(这使得您发布的有关记录和列表规范的所有内容都无关紧要):
-module(my).
-compile(export_all).
get_requirements() ->
#{"requirements" =>
#{
"supply_type" => #{"$tuple" => [1, 2, 3]},
"has_app_name" => true,
"any" => #{"latlong_radius" =>
#{"$tuple" => [33.042334, -96.734884, 16093.4]}
}
}
}.
go() ->
Requirements = get_requirements(),
io:format("requirements: ~p~n", [Requirements]),
#{"requirements" :=
#{
"any" :=
#{
"latlong_radius" :=
#{
"$tuple" := [Lat, Lon, Rad]
}
}
}
} = Requirements,
io:format("Lat: ~w, Lon: ~w, Rad: ~w~n", [Lat, Lon, Rad]).
在shell中:
70> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}
71> my:go().
requirements: #{"requirements" =>
#{"any" =>
#{"latlong_radius" =>
#{"$tuple" => [33.042334,-96.734884,16093.4]}},
"has_app_name" => true,
"supply_type" => #{"$tuple" => [1,2,3]}}}
Lat: 33.042334, Lon: -96.734884, Rad: 16093.4
ok
然而,对于深度嵌套的映射,试图使模式匹配语法正确是太容易出错了,所以我会使用 maps:get/2
代替:
-module(my).
-compile(export_all).
get_requirements() ->
#{"requirements" =>
#{
"supply_type" => #{"$tuple" => [1, 2, 3]},
"has_app_name" => true,
"any" => #{"latlong_radius" =>
#{"$tuple" => [33.042334, -96.734884, 16093.4]}
}
}
}.
go() ->
Map = get_requirements(),
io:format("Map: ~p~n", [Map]),
Requirements = maps:get("requirements", Map),
Any = maps:get("any", Requirements),
LLR = maps:get("latlong_radius", Any),
#{"$tuple" := [Lat, Long, Radius]} = LLR,
io:format("Lat: ~w, Lon: ~w, Rad: ~w~n", [Lat, Long, Radius]).
简单多了。