From b99ec496f77d3c20b256a2da1922ed308d83fcbe Mon Sep 17 00:00:00 2001 From: Marcin Puc Date: Wed, 13 Oct 2021 10:32:31 +0200 Subject: Simplify list.at implementation --- src/gleam/list.gleam | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index 6278c6e..07a6953 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -882,6 +882,8 @@ pub fn intersperse(list: List(a), with elem: a) -> List(a) { /// /// 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. +/// /// ## Examples /// /// > at([1, 2, 3], 1) @@ -891,18 +893,9 @@ pub fn intersperse(list: List(a), with elem: a) -> List(a) { /// Error(Nil) /// pub fn at(in list: List(a), get index: Int) -> Result(a, Nil) { - case index < 0 { - True -> Error(Nil) - False -> - case list { - [] -> Error(Nil) - [x, ..rest] -> - case index == 0 { - True -> Ok(x) - False -> at(rest, index - 1) - } - } - } + list + |> drop(index) + |> first } /// Removes any duplicate elements from a given list. -- cgit v1.2.3