dictionary - Elixir 将结构转换为映射

标签 dictionary struct elixir telegram-bot

我正在尝试将结构转换为映射,以便能够清除所有 nil 值

我目前正在使用此代码

  case Nadia.get_updates  do
    {:ok, results} ->
      Map.from_struct(results)
      |> Enum.filter(fn {_, v} -> v != nil end)
      |> Enum.into(%{})

注意:Nadia.get_updates 返回以下结构: https://hexdocs.pm/nadia/Nadia.Model.Update.html#t:t/0

但是我总是收到以下错误:Map.from_struct/1 中没有匹配的函数子句

最佳答案

v0.15我们有 Map.from_struct/1 正是这样做的。

defmodule User do
  defstruct [:name]
end

Map.from_struct(%User{name: "valim"})
#=> %{name: "valim"}

关于dictionary - Elixir 将结构转换为映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36512627/

相关文章:

dictionary - Haskell:使用 Data.Map.elems 的函数

Haskell 无法将类型 `[]' 与 `IO' 匹配

elixir - Phoenix Code_Reloader 是否只重新加载 Elixir 代码?

erlang - 如何获取Elixir中变量的存储位置?

postgresql - (UndefinedFunctionError) 未定义函数 List.Chars.to_charlist/1

python - 给定一组字符串,创建一个字典字典,使用它们作为具有默认值的条目的键

c# - 使用哈希表仅存储键?

C - 线程之间使用的结构体中的字段

c++ - 字符数组和指针的区别

c - 前向声明错误我无法理解