isCapitalized > startsWith

This commit is contained in:
Anton-4 2024-06-15 13:08:19 +02:00 committed by GitHub
parent 5d09479fd3
commit cb5782b522
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -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.