Skip to content

Commit

Permalink
update wording pt 2
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-snezhko committed Feb 11, 2023
1 parent aef7fc9 commit ead99e1
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/guide/lists.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ let oneTwoThree = [1, ...twoThree]
print(oneTwoThree) // [1, 2, 3]
```

We can also write functions that process data in lists, but we'll save that fun for the section on Pattern Matching.

## More On Spreads (`...`)

Although spreads are recommended to only be put at the end of a list, for convenience it is also possible to use the spread syntax at any position in a list:
Expand All @@ -35,9 +37,9 @@ let result = [...oneTwo, ...threeFour, 5]
print(result) // [1, 2, 3, 4, 5]
```

However, it is important to be aware of the **performance implications of arbitrary-position spreads**. Grain lists are implemented as singly-linked lists, and therefore prepending new elements to the beginning of one is a very efficient operation (the last new element could then simply point to the list being extended). On the other hand, we do not get this same benefit if a spread appears somewhere other than at the end of a list expression. In this case, the entire list being spread will have to be copied one element at a time in order to create the new list while also not mutating the old list.
However, it is important to be aware of the **performance implications of arbitrary-position spreads**. Grain lists are immutable linked lists, and therefore prepending new elements to the beginning of one is a very efficient operationthe new element simply points to the list being extended! On the other hand, we do not get this same benefit if a spread appears somewhere other than at the end of a list expression. In this case, a brand new list must be created, copying one element at a time, pointing each one at the next, to point to the new elements at the end.

We can also write functions that process data in lists, but we'll save that fun for the section on Pattern Matching.
Use arbitrary-position spreads sparingly, only when they make your code more expressive and where performance isn't a major concern.

## The List Standard Library

Expand Down

0 comments on commit ead99e1

Please sign in to comment.