LINUX.ORG.RU

История изменений

Исправление ddidwyll, (текущая версия) :

Должно работать

defmodule Test do
  @string "211(1,2,5,8),212(9,14,36)"

  def run do
    acc = %{result: [], left: "", right: "", in?: false}

    @string
    |> String.split("")
    |> Enum.reduce(acc, fn
      "(", acc -> acc |> toggle_in()
      ")", acc -> acc |> push() |> clear() |> toggle_in()
      ",", acc -> if acc.in?, do: acc |> push() |> clear(:right), else: acc
      num, acc -> acc |> append(num)
    end)
    |> Access.get(:result)
  end

  defp push(acc) do
    entry = "#{acc.left}_#{acc.right}"

    acc.result
    |> update_in(&[entry | &1])
  end

  defp append(acc, num) do
    key = if acc.in?, do: :right, else: :left

    acc[key] |> update_in(&(&1 <> num))
  end

  defp clear(acc, dir), do: acc[dir] |> put_in("")

  defp clear(acc), do: acc |> clear(:left) |> clear(:right)

  defp toggle_in(acc), do: acc.in? |> update_in(&(!&1))
end

Исправление ddidwyll, :

Должно работать

defmodule Test do
  @string "211(1,2,5,8),212(9,14,36)"

  def run do
    acc = %{result: [], left: "", right: "", in?: false}

    @string
    |> String.split("")
    |> Enum.reduce(acc, fn
      "(", acc -> acc |> toggle_in()
      ")", acc -> push(acc) |> clear() |> toggle_in()
      ",", acc -> if acc.in?, do: push(acc) |> clear(:right), else: acc
      num, acc -> append(acc, num)
    end)
    |> Access.get(:result)
  end

  defp push(acc) do
    entry = "#{acc.left}_#{acc.right}"

    acc.result
    |> update_in(&[entry | &1])
  end

  defp append(acc, num) do
    key = if acc.in?, do: :right, else: :left

    acc[key] |> update_in(&(&1 <> num))
  end

  defp clear(acc, dir), do: acc[dir] |> put_in("")

  defp clear(acc), do: acc |> clear(:left) |> clear(:right)

  defp toggle_in(acc), do: acc.in? |> update_in(&(!&1))
end

Исправление ddidwyll, :

Должно работать

defmodule Test do
  @string "211(1,2,5,8),212(9,14,36)"

  def run do
    acc = %{result: [], left: "", right: "", in?: false}

    @string
    |> String.split("")
    |> Enum.reduce(acc, fn
      "(", acc -> acc |> toggle_in()
      ")", acc -> push(acc) |> clear() |> toggle_in()
      ",", acc -> if acc.in?, do: push(acc) |> clear(:right), else: acc
      num, acc -> append(acc, num)
    end)
    |> Access.get(:result)
  end

  defp push(acc) do
    entry = "#{acc.left}_#{acc.right}"

    acc.result
    |> update_in(&[entry | &1])
  end

  defp clear(acc, dir) do
    acc[dir] |> put_in("")
  end

  defp clear(acc) do
    acc |> clear(:left) |> clear(:right)
  end

  defp toggle_in(acc) do
    acc.in? |> update_in(&(!&1))
  end

  defp append(acc, num) do
    key = if acc.in?, do: :right, else: :left

    acc[key] |> update_in(&(&1 <> num))
  end
end

Исправление ddidwyll, :

Должно работать

defmodule Test do
  @string "211(1,2,5,8),212(9,14,36)"

  def run do
    acc = %{result: [], left: "", right: "", in?: false}

    @string
    |> String.split("")
    |> Enum.reduce(acc, fn char, acc ->
      case char do
        "(" -> acc |> toggle_in()
        ")" -> push(acc) |> clear() |> toggle_in()
        "," -> if acc.in?, do: push(acc) |> clear(:right), else: acc
        num -> append(acc, num)
      end
    end)
    |> Access.get(:result)
  end

  defp push(acc) do
    entry = "#{acc.left}_#{acc.right}"

    acc.result
    |> update_in(&[entry | &1])
  end

  defp clear(acc, dir) do
    acc[dir] |> put_in("")
  end

  defp clear(acc) do
    acc |> clear(:left) |> clear(:right)
  end

  defp toggle_in(acc) do
    acc.in? |> update_in(& !&1)
  end

  defp append(acc, num) do
    key = if acc.in?, do: :right, else: :left

    acc[key] |> update_in(& &1 <> num)
  end
end

Исходная версия ddidwyll, :

Должно работать

defmodule Test do
  @string "211(1,2,5,8),212(9,14,36)"

  def run do
    acc = %{result: [], left: "", right: "", in?: false}

    @string
    |> String.split("")
    |> Enum.reduce(acc, fn char, acc ->
      cond do
        char == "(" -> acc |> toggle_in()
        char == ")" -> push(acc) |> clear() |> toggle_in()
        char == "," && acc.in? -> push(acc) |> clear(:right)
        char == "," -> acc
        true -> append(acc, char)
      end
    end)
    |> Access.get(:result)
  end

  defp push(acc) do
    entry = "#{acc.left}_#{acc.right}"

    acc.result
    |> update_in(&[entry | &1])
  end

  defp clear(acc, dir) do
    acc[dir] |> put_in("")
  end

  defp clear(acc) do
    acc |> clear(:left) |> clear(:right)
  end

  defp toggle_in(acc) do
    acc.in? |> update_in(& !&1)
  end

  defp append(acc, char) do
    key = if acc.in?, do: :right, else: :left

    acc[key] |> update_in(& &1 <> char)
  end
end