gpt4 book ai didi

recursion - Elixir map_reduce 引用最后一个元素

转载 作者:行者123 更新时间:2023-12-05 04:07:12 25 4
gpt4 key购买 nike

我如何循环(递归地或使用 reduce)MapList,同时与列表中的最后一个元素进行比较?

例如,假设我有一个这样的 map 列表:

datetime = Timex.beginning_of_day(Timex.now)
data = [
%{a: 0, cluster: 0, time: datetime},
%{a: 1, cluster: 0, time: Timex.shift(datetime, minutes: 3)},
%{a: 2, cluster: 0, time: Timex.shift(datetime, minutes: 6)},
%{a: 3, cluster: 0, time: Timex.shift(datetime, minutes: 9)},
%{a: 4, cluster: 1, time: Timex.shift(datetime, minutes: 12)},
%{a: 5, cluster: 1, time: Timex.shift(datetime, minutes: 15)},
%{a: 6, cluster: 0, time: Timex.shift(datetime, minutes: 18)},
%{a: 7, cluster: 0, time: Timex.shift(datetime, minutes: 21)},
%{a: 8, cluster: 0, time: Timex.shift(datetime, minutes: 23)},
%{a: 9, cluster: 2, time: Timex.shift(datetime, minutes: 26)},
%{a: 10, cluster: 2, time: Timex.shift(datetime, minutes: 29)},
%{a: 11, cluster: 2, time: Timex.shift(datetime, minutes: 32)},
%{a: 12, cluster: 1, time: Timex.shift(datetime, minutes: 35)},
%{a: 13, cluster: 1, time: Timex.shift(datetime, minutes: 38)},
]

我想修改每个 map 的 cluster 属性,以根据其所在的顺序来表示它所在的组。

如果 cluster id 不重复,则使用 group_by 非常好。

我想在他们改变组时对他们进行分组,结果是这样的:

[
%{a: 0, cluster: 0, time: datetime},
%{a: 1, cluster: 0, time: Timex.shift(datetime, minutes: 3)},
%{a: 2, cluster: 0, time: Timex.shift(datetime, minutes: 6)},
%{a: 3, cluster: 0, time: Timex.shift(datetime, minutes: 9)},
%{a: 4, cluster: 1, time: Timex.shift(datetime, minutes: 12)},
%{a: 5, cluster: 1, time: Timex.shift(datetime, minutes: 15)},
%{a: 6, cluster: 2, time: Timex.shift(datetime, minutes: 18)},
%{a: 7, cluster: 2, time: Timex.shift(datetime, minutes: 21)},
%{a: 8, cluster: 2, time: Timex.shift(datetime, minutes: 23)},
%{a: 9, cluster: 3, time: Timex.shift(datetime, minutes: 26)},
%{a: 10, cluster: 3, time: Timex.shift(datetime, minutes: 29)},
%{a: 11, cluster: 3, time: Timex.shift(datetime, minutes: 32)},
%{a: 12, cluster: 4, time: Timex.shift(datetime, minutes: 35)},
%{a: 13, cluster: 4, time: Timex.shift(datetime, minutes: 38)},
]

为此,我需要将列表中的当前项与前一项进行比较。我从这样的东西开始(如下)并停止了,因为我知道它不会引用前一个项目来将当前项目与前一个项目进行比较:

Enum.map_reduce(data, 0, fn(x, acc) -> cluster_grouping(x, acc) end)

def cluster_grouping(x, acc) do
cond do
x.cluster == acc -> {Map.put(x, :cluster, acc), acc}
x.cluster > acc -> {Map.put(x, :cluster, acc), acc + 1}
end
end

最佳答案

您需要在累加器中保留两个整数:当前生成的簇(每次更改都会递增 1)和簇的最后一个原始值。

datetime = Timex.beginning_of_day(Timex.now)
data = [
%{a: 0, cluster: 0, time: datetime},
%{a: 1, cluster: 0, time: Timex.shift(datetime, minutes: 3)},
%{a: 2, cluster: 0, time: Timex.shift(datetime, minutes: 6)},
%{a: 3, cluster: 0, time: Timex.shift(datetime, minutes: 9)},
%{a: 4, cluster: 1, time: Timex.shift(datetime, minutes: 12)},
%{a: 5, cluster: 1, time: Timex.shift(datetime, minutes: 15)},
%{a: 6, cluster: 0, time: Timex.shift(datetime, minutes: 18)},
%{a: 7, cluster: 0, time: Timex.shift(datetime, minutes: 21)},
%{a: 8, cluster: 0, time: Timex.shift(datetime, minutes: 23)},
%{a: 9, cluster: 2, time: Timex.shift(datetime, minutes: 26)},
%{a: 10, cluster: 2, time: Timex.shift(datetime, minutes: 29)},
%{a: 11, cluster: 2, time: Timex.shift(datetime, minutes: 32)},
%{a: 12, cluster: 1, time: Timex.shift(datetime, minutes: 35)},
%{a: 13, cluster: 1, time: Timex.shift(datetime, minutes: 38)},
]

Enum.map_reduce(data, {0, 0}, fn x, {i, last} ->
i = if x.cluster == last, do: i, else: i + 1
{Map.put(x, :cluster, i), {i, x.cluster}}
end)
|> elem(0)
|> IO.inspect

输出:

[
%{a: 0, cluster: 0, time: #DateTime<2018-02-18 00:00:00Z>},
%{a: 1, cluster: 0, time: #DateTime<2018-02-18 00:03:00Z>},
%{a: 2, cluster: 0, time: #DateTime<2018-02-18 00:06:00Z>},
%{a: 3, cluster: 0, time: #DateTime<2018-02-18 00:09:00Z>},
%{a: 4, cluster: 1, time: #DateTime<2018-02-18 00:12:00Z>},
%{a: 5, cluster: 1, time: #DateTime<2018-02-18 00:15:00Z>},
%{a: 6, cluster: 2, time: #DateTime<2018-02-18 00:18:00Z>},
%{a: 7, cluster: 2, time: #DateTime<2018-02-18 00:21:00Z>},
%{a: 8, cluster: 2, time: #DateTime<2018-02-18 00:23:00Z>},
%{a: 9, cluster: 3, time: #DateTime<2018-02-18 00:26:00Z>},
%{a: 10, cluster: 3, time: #DateTime<2018-02-18 00:29:00Z>},
%{a: 11, cluster: 3, time: #DateTime<2018-02-18 00:32:00Z>},
%{a: 12, cluster: 4, time: #DateTime<2018-02-18 00:35:00Z>},
%{a: 13, cluster: 4, time: #DateTime<2018-02-18 00:38:00Z>}
]

关于recursion - Elixir map_reduce 引用最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48853215/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com