【问题标题】:Enum.reduce_while vs Enum.reduce in ElixirElixir 中的 Enum.reduce_while 与 Enum.reduce
【发布时间】:2022-03-12 04:51:02
【问题描述】:

我是 Elixir 的初学者。这是我的疑问。我在下面写了两个函数。

a) 第一个使用Enum.reduce_while

b) 第二个函数使用Enum.reduce

我正在观察以下内容:
a)在第一个函数中,计数从 0、1、2、3 开始并被重置(基本上对于每一行,计数都会被重置)。通过查看IO.puts 的输出,我可以观察到这一点。我的期望是“计数”应该不断增加。

b) 然而,在第二个函数中,计数不断增加(这是预期的)。唯一的区别是第一个是reduce_while,而第二个只是reduce

问题:

a) 为什么 Enum.reduce_whileEnum.reduce 处理累加器的方式不同?

b) 我错过了什么?

def is_power_pattern_present_in_grid(grid, pattern, max_row, max_col)
    when length(pattern) <= max_row * max_col do
  count = 0
  result = 0

  {chk, chk1} =
    Enum.reduce_while(0..(max_row - 1), {result, count}, fn row, {result, count} ->
      Enum.reduce_while(0..(max_col - 1), {result, count}, fn col, {result, count} ->
        IO.puts("looping throughat #{inspect(row)}, #{inspect(col)},#{inspect(count)}")
        count = count + 1
        result = 0
        {:cont, {result, count}}
      end)

      {:cont, {result, count}}
    end)
end

def is_power1_pattern_present_in_grid(grid, pattern, max_row, max_col)
    when length(pattern) <= max_row * max_col do
  count = 0
  result = 0

  {chk, chk1} =
    Enum.reduce(0..(max_row - 1), {result, count}, fn row, {result, count} ->
      Enum.reduce(0..(max_col - 1), {result, count}, fn col, {result, count} ->
        IO.puts("looping throughat #{inspect(row)}, #{inspect(col)},#{inspect(count)}")
        count = count + 1
        result = 0
        {result, count}
      end)
    end)
end

【问题讨论】:

  • 请将您的代码格式化为代码,去掉印刷引号和 箭头并首先缩进。
  • 是的。刚刚完成。
  • @Sudarsan 你能添加一个示例数据集来测试这些函数吗?这样就可以清楚地知道什么在里面,什么在外面(以及您对后者的期望是什么:))
  • 这是一种非常令人困惑的提问方式,所以我认为您不会收到很多回复。我建议您将其简化为问题的本质,即删除嵌套的枚举,对示例值进行硬编码以进行操作等,并考虑为您的累加器提供不同的名称,然后我想您会看到两者之间的差异这两个功能或至少使其他人能够提供有用的建议。

标签: elixir


【解决方案1】:

外部reduce返回不同​​的东西。底部reduce返回内部reduce的结果,而顶部reduce丢弃结果并返回{0, 0}

希望使用不同的变量名更容易看到:

def is_power_pattern_present_in_grid(grid, pattern, max_row, max_col)
    when length(pattern) <= max_row * max_col do
  initial_count = 0
  initial_result = 0

  {chk, chk1} =
    Enum.reduce_while(0..(max_row - 1), {initial_result, initial_count}, fn row, {outer_result, outer_count} ->
      Enum.reduce_while(0..(max_col - 1), {outer_result, outer_count}, fn col, {inner_result, inner_count} ->
        IO.puts("looping throughat #{inspect(row)}, #{inspect(col)},#{inspect(inner_count)}")
        inner_count = inner_count + 1
        inner_result = 0
        {:cont, {inner_result, inner_count}}
      end)

      {:cont, {outer_result, outer_count}}
    end)
end

def is_power1_pattern_present_in_grid(grid, pattern, max_row, max_col)
    when length(pattern) <= max_row * max_col do
  initial_count = 0
  initial_result = 0

  {chk, chk1} =
    Enum.reduce(0..(max_row - 1), {initial_result, initial_count}, fn row, {outer_result, outer_count} ->
      Enum.reduce(0..(max_col - 1), {outer_result, outer_count}, fn col, {inner_result, inner_count} ->
        IO.puts("looping throughat #{inspect(row)}, #{inspect(col)},#{inspect(inner_count)}")
        inner_count = inner_count + 1
        inner_result = 0
        {inner_result, inner_count}
      end)
    end)
end

这似乎是您的本意:

def is_power_pattern_present_in_grid(grid, pattern, max_row, max_col)
    when length(pattern) <= max_row * max_col do
  initial_count = 0
  initial_result = 0

  {chk, chk1} =
    Enum.reduce_while(0..(max_row - 1), {initial_result, initial_count}, fn row, {outer_result, outer_count} ->
      {result, count} = Enum.reduce_while(0..(max_col - 1), {outer_result, outer_count}, fn col, {inner_result, inner_count} ->
        IO.puts("looping throughat #{inspect(row)}, #{inspect(col)},#{inspect(inner_count)}")
        inner_count = inner_count + 1
        inner_result = 0
        {:cont, {inner_result, inner_count}}
      end)

      {:cont, {result, count}}
    end)
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-21
    • 2017-11-26
    • 2018-10-15
    • 2015-04-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多