It turns out that I think my assumption about how the BF interpreter works
is wrong, but I want to continue this discussion because while my
interpreter may be bad, I still don't know the answer to my Elixir
question. :)
Torben - thanks for the idea! I did try it with function pattern matching.
However, the actual command is dataptr - 1 , so that needs to run first.
*Then* if the dataptr is less than 0, the other check runs. The way you've
suggested it in the first command assumes that the < command has already
been run. :)
Here's what I tried with pattern matching, which is even less
comprehensible than that simple little if statement at the top.
def command("<", state = %{data: data, dataptr: dataptr}) when is_list(
data) and is_integer(dataptr) and dataptr >= 0 do
do_trim(%{state | dataptr: dataptr - 1})
end
def do_trim(state = %{data: data, dataptr: dataptr}) when dataptr < 0 do
%{state | dataptr: 0, data: tl(data) }
end
def do_trim(state = %{data: data, dataptr: dataptr}), do: state
Having been a programmer for 23 years, I'm a huge fan of writing code that
my tired, burned out, working-on-the-weekend-because-release-date-is-Monday
future self won't have to think about too hard to understand.
Nothing I've come up with so far is as clear as that simple little if
statement.
On Tuesday, August 23, 2016 at 8:24:51 AM UTC-5, Torben Hoffmann wrote:
>
> 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] <javascript:>>
> 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] <javascript:>.
>> 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/743c6c3e-0def-4cf6-8dee-2338a19c0ef6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.