I'm incredibly stuck on something that would be very easy in imperative, but I just can't figure it out with Elixir in a properly functional way. I want to turn this:
[:input, :loop_begin, :output, :loop_begin, :decrement, :loop_end, :input, :loop_end]
[:input, [:loop_begin, :output, [:loop_begin, :decrement, :loop_end], :input, :loop_end]]
Basically - any time there's a :loop_begin it makes a new list, and then closes the list with :loop_end.
This is the closest that I've gotten, but it (obviously) stops as soon as it hits the first :loop_end.
def tree(tokens), do: do_tree(tokens, )
defp do_tree([:loop_begin|rest], acc) do
[tree(rest, [:loop_begin]) | acc]
defp do_tree([:loop_end|rest], acc) do
defp do_tree([t|rest], acc), do: map(rest, [t | acc])
It's like I need to consume items sometimes but not others, and I can't figure it out. Every variation I've come up with in the last couple of hours has been a flop.
Any suggestions? This seems like something that would be obvious if I had more functional experience.