roc-lang / examples

All kinds of Roc examples
https://www.roc-lang.org/examples
Creative Commons Zero v1.0 Universal
30 stars 17 forks source link

add `.. as` syntax to pattern matching example #159

Closed Anton-4 closed 4 months ago

Anton-4 commented 8 months ago

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

bairymr commented 4 months ago

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

lukewilliamboswell commented 4 months ago

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

bairymr commented 4 months ago

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.