Stream: roctoberfest

Topic: 2023 day 8


view this post on Zulip Elias Mulhall (Oct 12 2023 at 22:11):

At long last https://github.com/mulias/roctoberfest/tree/main/advent_2022/day_08

walk = \array, startState, options, fn ->
    {
        direction ? Forward,
        orientation ? Rows,
        start ? defaultStartIndex array direction
    } = options

but I ran into a couple different compiler issues and couldn't get the value of start to rely conditionally on direction. I went with a different API for now.

view this post on Zulip Luke Boswell (Oct 12 2023 at 22:48):

For the optional deconstruction, does it work if you do it all in the one line within the function? From memory I think there was a bug with the parser that meant it worked on one line but across multiple lines it didn't. So walk = \array, startState, {direction? Forward,orientation? Rows,start? defaultStartIndex array direction}, fn -> ?

I'm not sure I have seen optional field destructuring as a statement, i.e. within the body of a function and not the arguments, that looks like a really useful feature. I'm not sure but maybe we should raise a thread in #ideas for it? Or maybe that is already the intended use for that, in which case it would be good to add an explanation to the Tutorial.

view this post on Zulip Luke Boswell (Oct 12 2023 at 22:50):

Actually, I just realised.. I doubt you can use the array argument and call a function when providing a default value... :oops:

view this post on Zulip Elias Mulhall (Oct 12 2023 at 22:56):

I should really make an example with the different errors that came up, but yes having to be on one line was confusing. The formatter forces it which helped figure that one out.

This is currently working fine

WalkOptions : {
    direction ? [Forward, Backwards, ForwardFrom Index, BackwardsFrom Index],
    orientation ? [Rows, Cols],
}

walk : Array2D a, state, WalkOptions, (state, a, Index -> state) -> state
walk = \array, startState, options, fn ->
    { direction ? Forward, orientation ? Rows } = options

The main thing I ran into was that the default value for start couldn't use the value for default, which right now is specifically mentioned in the tutorial as something you can do.

view this post on Zulip Elias Mulhall (Oct 12 2023 at 22:57):

This section has an example where a default value is set with a function and a reference to a different value getting destructured https://www.roc-lang.org/tutorial#optional-record-fields

view this post on Zulip Elias Mulhall (Oct 12 2023 at 22:58):

{ height, width, title ? "", description ? Str.concat "A table called " title }

view this post on Zulip Anton (Oct 13 2023 at 09:19):

For reference, shadowing has been thoroughly debated.


Last updated: Jul 06 2025 at 12:14 UTC