Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Erlang对象结构转换为Elixir映射?_Erlang_Elixir_Jiffy - Fatal编程技术网

如何将Erlang对象结构转换为Elixir映射?

如何将Erlang对象结构转换为Elixir映射?,erlang,elixir,jiffy,Erlang,Elixir,Jiffy,我用Elixir联系CouchDB 但是lib返回了旧的erlang对象表示,比如{[{“foo”,“bar”}]},而不是elixir映射,这是由于lib使用了jiffy:decode而不是return\u maps,如何将这个对象结构转换为elixir映射(反之亦然) 我找到了一种对jiffy进行编码和解码的黑客方法:使用return\u maps。。。但必须有另一种选择吗 更新: 从Hynek在erlang中的例子来看,这似乎是可行的: defmodule ToMaps do def

我用Elixir联系CouchDB

但是lib返回了旧的erlang对象表示,比如
{[{“foo”,“bar”}]}
,而不是elixir映射,这是由于lib使用了jiffy:decode而不是
return\u maps
,如何将这个对象结构转换为elixir映射(反之亦然)

我找到了一种对jiffy进行编码和解码的黑客方法:使用
return\u maps
。。。但必须有另一种选择吗

更新: 从Hynek在erlang中的例子来看,这似乎是可行的:

defmodule ToMaps do

  def convert({x}) when is_list(x) do
    Map.new(x, fn {k, v} -> {k, convert(v)} end)
  end

  def convert([head | tail]) do
    [convert(head) | convert(tail)]
  end

  def convert(x) do
    x
  end
end
似乎能胜任这项工作

iex(1)> ToMaps.convert({[{"foo",[{[{"a",1}]},3]},{"bar","baz"}]})

%{"bar" => "baz", "foo" => [%{"a" => 1}, 3]}

我不知道长生不老药,但在二郎:

-module(to_maps).

-export([to_maps/1]).

to_maps({L}) when is_list(L) ->
    maps:from_list([{K, to_maps(V)} || {K, V} <- L]);
to_maps([H|T]) ->
    [to_maps(H) | to_maps(T)];
to_maps(X) -> X.
我不推荐它,但它甚至可以是一个功能:

convert({L}) when is_list(L) ->
    maps:from_list([{K, convert(V)} || {K, V} <- L]);
convert(#{} = M) ->
    F = fun(K, V, Acc) -> [{K, convert(V)} | Acc] end,
    {maps:fold(F, [], M)};
convert([H|T]) ->
    [convert(H) | convert(T)];
convert(X) -> X.
convert({L})何时为列表(L)->
映射:从_列表([{K,convert(V)}|{K,V}
F=fun(K,V,Acc)->[K,convert(V)}| Acc]结束,
{地图:褶皱(F,[],M)};
转换([H|T])->
[转换(H)|转换(T)];
转换(X)->X。
用法:

1> jiffy:decode(<<"{\"foo\":[3, {\"a\":1}], \"bar\":\"baz\"}">>).
{[{<<"foo">>,[3,{[{<<"a">>,1}]}]},{<<"bar">>,<<"baz">>}]}
2> to_maps:to_maps(v(-1)).
#{<<"bar">> => <<"baz">>,<<"foo">> => [3,#{<<"a">> => 1}]}
3> to_maps:from_maps(v(-1)).
{[{<<"foo">>,[3,{[{<<"a">>,1}]}]},{<<"bar">>,<<"baz">>}]}
4> to_maps:convert(v(-1)).
#{<<"bar">> => <<"baz">>,<<"foo">> => [3,#{<<"a">> => 1}]}
5> to_maps:convert(v(-1)).
{[{<<"foo">>,[3,{[{<<"a">>,1}]}]},{<<"bar">>,<<"baz">>}]}
6> to_maps:convert(v(-1)).
#{<<"bar">> => <<"baz">>,<<"foo">> => [3,#{<<"a">> => 1}]}
...
1>jiffy:decode()。
{[{,[3,{[{,1}]}]},{,}]}
2> to_映射:to_映射(v(-1))。
#{ => , => [3,#{ => 1}]}
3> 到_映射:从_映射(v(-1))。
{[{,[3,{[{,1}]}]},{,}]}
4> 到_映射:转换(v(-1))。
#{ => , => [3,#{ => 1}]}
5> 到_映射:转换(v(-1))。
{[{,[3,{[{,1}]}]},{,}]}
6> 到_映射:转换(v(-1))。
#{ => , => [3,#{ => 1}]}
...

我需要查看您的所有数据结构才能确定,但您可以使用以下内容:

iex(1)> Enum.into([{"foo", "bar"}], %{})
%{"foo" => "bar"}

我在elixir中尝试了类似于此方法的代码,编辑了我的问题。@VasuMahesh我认为应该有
[convert(head)| convert(tail)]
在你的第二个函数子句中。是的,谢谢你的发现,还有,还有什么其他方法呢?从地图到这个结构?因为在couchbeam中,保存文档时,我不能给erlang/elixirmaps@VasuMahesh看编辑,但是考虑<代码>转换/ 1 <代码>作为笑话,不要走这条路。嘿!谢谢你的回答,请看海涅克-皮奇-维乔。dil的例子是完整的数据,如果你能解码的话,这将适用于一组数据,他的例子似乎涵盖了一切。
iex(1)> Enum.into([{"foo", "bar"}], %{})
%{"foo" => "bar"}