aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMathias Jean Johansen <mathias@mjj.io>2022-05-02 18:01:13 +0200
committerLouis Pilfold <louis@lpil.uk>2022-05-04 22:08:15 +0100
commit0f9e6e6306290400ea031db921fad4dd68ba3564 (patch)
treee95a064075bf7e55413ec53790efbc4aa3baec5c /src
parentbcea3cf65a63f948784d70425d6fc46ae648b1e4 (diff)
downloadgleam_stdlib-0f9e6e6306290400ea031db921fad4dd68ba3564.tar.gz
gleam_stdlib-0f9e6e6306290400ea031db921fad4dd68ba3564.zip
Refactor `first` to return a `Result`.
Diffstat (limited to 'src')
-rw-r--r--src/gleam/string.gleam18
1 files changed, 9 insertions, 9 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index 2440e83..89cdc6c 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -740,26 +740,26 @@ pub fn to_option(s: String) -> Option(String) {
}
}
-/// Returns the first element in a grapheme and wraps it in an `Option(String)`.
-/// If the `String` is empty, it returns `None`. Otherwise, it returns
-/// `Some(String)`.
+/// Returns the first grapheme cluster in a given `String` and wraps it in a
+/// `Result(String, Nil)`. If the `String` is empty, it returns `Error(Nil)`.
+/// Otherwise, it returns `Ok(String)`.
///
/// ## Examples
///
/// ```gleam
/// > first("")
-/// None
+/// Error(Nil)
/// ```
///
/// ```gleam
/// > first("icecream")
-/// Some("i")
+/// Ok("i")
/// ```
///
-pub fn first(s: String) -> Option(String) {
- case length(s) {
- 0 -> None
- _ -> Some(slice(s, 0, 1))
+pub fn first(s: String) -> Result(String, Nil) {
+ case pop_grapheme(s) {
+ Ok(#(first, _)) -> Ok(first)
+ Error(e) -> Error(e)
}
}