Skip to content
Permalink
Browse files

simplify parse function with multi-clauses of anonymous function

  • Loading branch information
Maples7 committed Jun 16, 2019
1 parent 10d5cae commit 9af5145731aacfeac0e6e3826ee4c854c8550d6a
Showing with 15 additions and 19 deletions.
  1. +15 −19 lib/field_mask.ex
@@ -190,29 +190,25 @@ defmodule FieldMask do
"""
def parse(tokens) do
tokens
|> Enum.reduce({%{}, [], [], nil}, fn token, acc ->
{tree, path, stack, last_token} = acc

case token do
"," ->
if List.first(stack) === "/" do
{tree, tl(path), tl(stack), token}
else
acc
end
|> Enum.reduce({%{}, [], [], nil}, fn
",", {tree, path, stack, last_token} ->
if List.first(stack) === "/" do
{tree, tl(path), tl(stack), ","}
else
{tree, path, stack, last_token}
end

"/" ->
{tree, [last_token | path], [token | stack], token}
"/", {tree, path, stack, last_token} ->
{tree, [last_token | path], ["/" | stack], "/"}

"(" ->
{tree, [last_token | path], [token | stack], token}
"(", {tree, path, stack, last_token} ->
{tree, [last_token | path], ["(" | stack], "("}

")" ->
{tree, tl(path), [token | stack], token}
")", {tree, path, stack, _} ->
{tree, tl(path), [")" | stack], ")"}

_ ->
{put_in(tree, Enum.reverse([token | path]), %{}), path, stack, token}
end
token, {tree, path, stack, _} ->
{put_in(tree, Enum.reverse([token | path]), %{}), path, stack, token}
end)
end
end

0 comments on commit 9af5145

Please sign in to comment.
You can’t perform that action at this time.