aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIvan Habunek <ivan@habunek.com>2022-12-08 17:48:20 +0100
committerLouis Pilfold <louis@lpil.uk>2022-12-08 16:55:46 +0000
commit39d5b221f7c28e76262fc7e8b0746152a46c97c5 (patch)
tree16711c48b676acbdcebe8288d542731807c92e02 /src
parentccd0f0dd912e15b3a4b73c2eb218c9e18c94b353 (diff)
downloadgleam_stdlib-39d5b221f7c28e76262fc7e8b0746152a46c97c5.tar.gz
gleam_stdlib-39d5b221f7c28e76262fc7e8b0746152a46c97c5.zip
Make list.at return an error given sub-zero index
Diffstat (limited to 'src')
-rw-r--r--src/gleam/list.gleam15
1 files changed, 9 insertions, 6 deletions
diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam
index 3f5f566..0fad104 100644
--- a/src/gleam/list.gleam
+++ b/src/gleam/list.gleam
@@ -1036,9 +1036,8 @@ pub fn intersperse(list: List(a), with elem: a) -> List(a) {
/// Returns the element in the Nth position in the list, with 0 being the first
/// position.
///
-/// `Error(Nil)` is returned if the list is not long enough for the given index.
-///
-/// For any `index` less than 0 this function behaves as if it was set to 0.
+/// `Error(Nil)` is returned if the list is not long enough for the given index
+/// or if the index is less than 0.
///
/// ## Examples
///
@@ -1053,9 +1052,13 @@ pub fn intersperse(list: List(a), with elem: a) -> List(a) {
/// ```
///
pub fn at(in list: List(a), get index: Int) -> Result(a, Nil) {
- list
- |> drop(index)
- |> first
+ case index >= 0 {
+ True ->
+ list
+ |> drop(index)
+ |> first
+ False -> Error(Nil)
+ }
}
/// Removes any duplicate elements from a given list.