roc-lang / examples

All kinds of Roc examples

Home Page:https://www.roc-lang.org/examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

add `.. as` syntax to pattern matching example

Anton-4 opened this issue · comments

for example [head, .. as tail] -> 9

Hi @Anton-4 I tried to setup a very simple code for this here

Basically used the [head, .. as tail] pattern to find length of a list or to find an element in a list

Does the code look ok ? If so I can create a PR or if you had any suggestions before I create a PR I can correct that as well. Thank you

Yeah, looks like a great start @bairymr. Thank you for looking into this. I'd say make a PR and we can make any edits/comments.

Some comments (my 2cents) 😄

I would write this

listToStr = List.map list1 Num.toStr
                |> Str.joinWith ","

# like this
listToStr = 
    list1 
    |> List.map Num.toStr
    |> Str.joinWith ","

For the length of a list we have List.len which is free, so I would rather encourage we share this with new users.

I'd format the if then else on multiple lines so it's easier to read

Thank you @lukewilliamboswell . Appreciate you taking time to explain this. I have restructured the code now to remove the function to find the length of the list. The search is modified to return a boolean rather than a string. Also the listToStr variable is formatted now.

I will create a pull request shortly.