Skip to content

Commit cd1622b

Browse files
committed
more cases
1 parent ac81889 commit cd1622b

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

misc_docs/syntax/language_spreads.mdx

+46-2
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,48 @@ let isPet = (animal: animals) => {
7373

7474
Read more about [variant type spreads in pattern matching](pattern-matching-destructuring.md#match-on-subtype-variants).
7575

76+
## List immutable update
77+
Spreads can be used for immutable updates of lists:
78+
79+
```res
80+
let prepended = list{1, ...someOtherList}
81+
82+
// You can spread several lists, but it's O(n) so avoid it if possible
83+
let multiple = list{1, ...prepended, ...anotherList}
84+
```
85+
86+
Read more about [immutable list updates](array-and-list.md#immutable-prepend) here.
87+
88+
## List pattern matching
89+
Spreads can be used when pattern matching on lists:
90+
91+
```res
92+
let rec printStudents = (students) => {
93+
switch students {
94+
| list{} => () // done
95+
| list{student} => Console.log("Last student: " ++ student)
96+
| list{student1, ...otherStudents} =>
97+
Console.log(student1)
98+
printStudents(otherStudents)
99+
}
100+
}
101+
printStudents(list{"Jane", "Harvey", "Patrick"})
102+
```
103+
104+
Read more about [pattern matching on lists](pattern-matching-destructuring.md#match-on-list).
105+
106+
## Array immutable update
107+
> Available in v11+
108+
109+
You can use spreads to add the contents of one array to another, just like in JavaScript:
110+
111+
```res
112+
let firstArray = [1, 2, 3]
113+
let secondArray = [...firstArray, 4, 5]
114+
```
115+
116+
Read more about [array spreads](array-and-list.md#array-spreads).
117+
76118
## Partial application of functions
77119
> Available in v11+ (uncurried mode)
78120
@@ -91,5 +133,7 @@ Read more about [partial application of functions](function.md#partial-applicati
91133
* [Record immutable updates](record.md#immutable-update)
92134
* [Variant type spreads](variant.md#variant-type-spreads)
93135
* [Variant type spreads in pattern matching](pattern-matching-destructuring.md#match-on-subtype-variants)
94-
* [Partial application of functions](function.md#partial-application)
95-
136+
* [List immutable updates](array-and-list.md#immutable-prepend)
137+
* [List pattern matching](pattern-matching-destructuring.md#match-on-list)
138+
* [Array spreads](array-and-list.md#array-spreads)
139+
* [Partial application of functions](function.md#partial-application)

0 commit comments

Comments
 (0)