Hi Brian,
How about this?
def command("<", state= %{dataprt: 0}) do
{:ok, %{state | data: tl(data)}}
end
def command("<", state) do
{:ok, %{state | dataprt: state.dataptr - 1}}
end
By using the pattern matching in the function clauses you get code that
reads like your description of the problem.
I.e., if the dataptr is zero pop the data stack otherwise decrement the
dataptr.
Cheers,
Torben
On 23 August 2016 at 15:08, Brian Bugh <[email protected]> wrote:
> Here's another ugly one I came up with (with the complete function for
> reference) that is even worse.
>
> def command("<", state = %{data: data, dataptr: dataptr}) when is_list(
> data) and is_integer(dataptr) and dataptr >= 0 do
> dataptr = dataptr - 1
>
> state = case state do
> %{dataptr: dataptr} when dataptr < 0 -> %{state | dataptr: 0, data: tl
> (data) }
> _ -> state
> end
>
> {:ok, %{state | dataptr: dataptr, data: data}}
> end
>
> Usually when I get stuck like this it means I'm overthinking something.
>
> Any suggestions?
>
>
> On Tuesday, August 23, 2016 at 7:51:22 AM UTC-5, Brian Bugh wrote:
>>
>> For fun and profit, I am writing a Brainf**k interpreter in Elixir.
>>
>> In one particular case, a data pointer should be decremented, and if it's
>> less than 0, it should be set to 0 and the top of the data stack should be
>> popped off.
>>
>> I assumed that I should write something like this (which passes my :
>>
>> dataptr = dataptr - 1
>>
>> if dataptr < 0 do
>> dataptr = 0
>> data = tl data
>> end
>>
>> but I get a compiler warning when I do this. It suggested that I use the
>> assignment form of *if*, which is fine, but I can't find an elegant way
>> to write the code now. This is what I came up with, which seems uglier and
>> unnecessarily verbose for future readers. The first form above is much more
>> readable.
>>
>> [dataptr | data] = if dataptr < 0 do
>> [0 | tl data]
>> else
>> [dataptr | data]
>> end
>>
>> Is there a better Elixir-way to write this?
>>
> --
> You received this message because you are subscribed to the Google Groups
> "elixir-lang-talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/elixir-lang-talk/bfcb8bdf-aede-4955-ac50-
> a6ac6dde3e5a%40googlegroups.com
> <https://groups.google.com/d/msgid/elixir-lang-talk/bfcb8bdf-aede-4955-ac50-a6ac6dde3e5a%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>
--
http://www.linkedin.com/in/torbenhoffmann
@LeHoff
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-talk/CABf3pC%3DKM70ORDqsd3xxwPh2bpV6AXH1KZj815DO%2B%3DcL2xu%3DXQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.