mirror of
https://github.com/roc-lang/roc.git
synced 2024-09-11 10:55:31 +03:00
isCapitalized > startsWith
This commit is contained in:
parent
5d09479fd3
commit
cb5782b522
@ -698,7 +698,7 @@ We can use tags with payloads to make a list that contains a mixture of differen
|
||||
List.map [StrElem "A", StrElem "b", NumElem 1, StrElem "c", NumElem -3] \elem ->
|
||||
when elem is
|
||||
NumElem num -> Num.isNegative num
|
||||
StrElem str -> Str.isCapitalized str
|
||||
StrElem str -> Str.startsWith str "A"
|
||||
# returns [Bool.true, Bool.false, Bool.false, Bool.false, Bool.true]
|
||||
```
|
||||
|
||||
@ -708,7 +708,7 @@ Compare this with the example from earlier, which caused a compile-time error:
|
||||
List.map ["A", "B", "C", 1, 2, 3] Num.isNegative
|
||||
```
|
||||
|
||||
The version that uses tags works because we aren't trying to call `Num.isNegative` on each element. Instead, we're using a `when` to tell when we've got a string or a number, and then calling either `Num.isNegative` or `Str.isCapitalized` depending on which type we have.
|
||||
The version that uses tags works because we aren't trying to call `Num.isNegative` on each element. Instead, we're using a `when` to tell when we've got a string or a number, and then calling either `Num.isNegative` or `Str.startsWith` depending on which type we have.
|
||||
|
||||
We could take this as far as we like, adding more different tags (e.g. `BoolElem Bool.true`) and then adding more branches to the `when` to handle them appropriately.
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user