【问题标题】:How to iterate through the list of maps in Elixir?如何遍历 Elixir 中的地图列表?
【发布时间】:2019-11-13 10:49:43
【问题描述】:

我不明白如何在 Elixir 中使用索引进行迭代。

比如我有这个来自java的sn-p,我想把它翻译成Elixir:

for(int i = 1; i < list.size(); i++) {
   list.order = i;
}

假设list 是来自 Elixir 的地图列表。 我无法理解如何以 Elixir 方式执行此操作,或者只是使用一些索引变量进行迭代。

【问题讨论】:

    标签: elixir


    【解决方案1】:

    虽然贾斯汀的回答完全有效,但惯用的 Elixir 解决方案是使用 Enum.with_index/2

    list = ~w|a b c d e|
    list
    |> Enum.with_index()
    |> Enum.each(fn {e, idx} -> IO.puts "Elem: #{e}, Idx: #{idx}" end)
    
    #⇒ Elem: a, Idx: 0
    #⇒ Elem: b, Idx: 1
    #⇒ Elem: c, Idx: 2
    #⇒ Elem: d, Idx: 3
    #⇒ Elem: e, Idx: 4
    

    【讨论】:

    • 我认为这两种解决方案都很棒,但我似乎更容易理解这个解决方案。
    • Enum.with_index 是否允许更新 Map 上的值(这是我认为 OP 想要的,而不仅仅是一种简单迭代的方法)?
    • @JustinNiessner Enum.with_indexallowdisallow 任何东西,它传递元素,附加一个索引并返回一个元组。如果输入是要更新的map,则应在最后一行管道中使用Enum.mapEnum.reduce 而不是Enum.each。 Elixir 中没有这样的“地图更新”。结果仍然是新地图。
    • 对不起,我解释的问题不太清楚。我的终点是更新 ecto 模式元素列表。我想重新分配 :order 键以使这个模式列表排序。我是这样做的:Repo.all(query) |&gt; Enum.with_index(1) |&gt; Enum.each(fn {l, idx} -&gt; update_lesson(l, %{order: idx}) end)update_lesson 只是带​​有变更集的 Repo.update()。
    【解决方案2】:

    使用不允许数据变异的语言时,它不像迭代集合和设置值那么简单。相反,您需要使用已设置字段的新对象创建一个新集合。

    在 Elixir 中,您可以使用 foldl

    List.foldl(
      list, 
      (1, map), 
      fn(l, (i, map)) -> (i+1, Map.update(map, :some_key, $(i)))
    )
    

    【讨论】:

      【解决方案3】:

      或者使用for理解

      [debug] localized_titles %{attributes: [%{"en" =&gt; "The English Title of a Playlist"}, %{"fr" =&gt; "Le French Title of a Playlist"}]}

            maps =
              for title <- localized_titles,
                  _ = Logger.debug("title #{inspect(%{attributes: title})}"),
                  {k, v} <- title do
                IO.puts "#{k} --> #{v}"
      
                Repo.insert(%PlaylistTitle{language_id: k, localizedname: v, uuid: Ecto.UUID.generate(), playlist_id: playlist.id})
              end
            Logger.debug("maps #{inspect(%{attributes: maps})}")
            {:ok, maps}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-13
        • 2019-08-24
        • 2017-07-23
        • 2020-05-18
        • 2021-08-29
        • 2022-01-23
        • 1970-01-01
        相关资源
        最近更新 更多