Skip to content

Commit b98705f

Browse files
committed
Remove list.at
1 parent f66ad56 commit b98705f

File tree

3 files changed

+11
-54
lines changed

3 files changed

+11
-54
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- `dynamic.unsafe_coerce` function has been deprecated.
1414
- Fixed `bit_array` slices of slices sometimes being incorrect on JavaScript.
1515
- The `dict` module gains the `combine` function.
16+
- The deprecated `list.at` function has been removed.
1617

1718
## v0.37.0 - 2024-04-19
1819

src/gleam/list.gleam

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,40 +1118,6 @@ pub fn intersperse(list: List(a), with elem: a) -> List(a) {
11181118
}
11191119
}
11201120

1121-
/// Returns the element in the Nth position in the list, with 0 being the first
1122-
/// position.
1123-
///
1124-
/// `Error(Nil)` is returned if the list is not long enough for the given index
1125-
/// or if the index is less than 0.
1126-
///
1127-
/// ## Examples
1128-
///
1129-
/// ```gleam
1130-
/// at([1, 2, 3], 1)
1131-
/// // -> Ok(2)
1132-
/// ```
1133-
///
1134-
/// ```gleam
1135-
/// at([1, 2, 3], 5)
1136-
/// // -> Error(Nil)
1137-
/// ```
1138-
///
1139-
@deprecated("
1140-
1141-
Gleam lists are immutable linked lists, so indexing into them is a slow operation that must traverse the list.
1142-
1143-
In functional programming it is very rare to use indexing, so if you are using indexing then a different algorithm or a different data structure is likely more appropriate.
1144-
")
1145-
pub fn at(in list: List(a), get index: Int) -> Result(a, Nil) {
1146-
case index >= 0 {
1147-
True ->
1148-
list
1149-
|> drop(index)
1150-
|> first
1151-
False -> Error(Nil)
1152-
}
1153-
}
1154-
11551121
/// Removes any duplicate elements from a given list.
11561122
///
11571123
/// This function returns in loglinear time.
@@ -1274,14 +1240,18 @@ fn sequences(
12741240
// consecutive equal items) while a descreasing sequence is strictly
12751241
// decreasing (no consecutive equal items), this is needed to make the
12761242
// algorithm stable!
1277-
order.Gt, Descending | order.Lt, Ascending | order.Eq, Ascending ->
1243+
order.Gt, Descending
1244+
| order.Lt, Ascending
1245+
| order.Eq, Ascending ->
12781246
sequences(rest, compare, growing, direction, new, acc)
12791247

12801248
// We were growing an ascending (descending) sequence and the new item
12811249
// is smaller (bigger) than the previous one, this means we have to stop
12821250
// growing this sequence and start with a new one whose first item will
12831251
// be the one we just found.
1284-
order.Gt, Ascending | order.Lt, Descending | order.Eq, Descending -> {
1252+
order.Gt, Ascending
1253+
| order.Lt, Descending
1254+
| order.Eq, Descending -> {
12851255
let acc = case direction {
12861256
Ascending -> [do_reverse(growing, []), ..acc]
12871257
Descending -> [growing, ..acc]
@@ -1404,8 +1374,8 @@ fn merge_ascendings(
14041374
[first1, ..rest1], [first2, ..rest2] ->
14051375
case compare(first1, first2) {
14061376
order.Lt -> merge_ascendings(rest1, list2, compare, [first1, ..acc])
1407-
order.Gt | order.Eq ->
1408-
merge_ascendings(list1, rest2, compare, [first2, ..acc])
1377+
order.Gt
1378+
| order.Eq -> merge_ascendings(list1, rest2, compare, [first2, ..acc])
14091379
}
14101380
}
14111381
}
@@ -1430,8 +1400,8 @@ fn merge_descendings(
14301400
[first1, ..rest1], [first2, ..rest2] ->
14311401
case compare(first1, first2) {
14321402
order.Lt -> merge_descendings(list1, rest2, compare, [first2, ..acc])
1433-
order.Gt | order.Eq ->
1434-
merge_descendings(rest1, list2, compare, [first1, ..acc])
1403+
order.Gt
1404+
| order.Eq -> merge_descendings(rest1, list2, compare, [first1, ..acc])
14351405
}
14361406
}
14371407
}

test/gleam/list_test.gleam

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -588,20 +588,6 @@ pub fn intersperse_test() {
588588
|> list.intersperse(0)
589589
}
590590

591-
pub fn at_test() {
592-
list.at([1, 2, 3], 2)
593-
|> should.equal(Ok(3))
594-
595-
list.at([1, 2, 3], 5)
596-
|> should.equal(Error(Nil))
597-
598-
list.at([], 0)
599-
|> should.equal(Error(Nil))
600-
601-
list.at([1, 2, 3, 4, 5, 6], -1)
602-
|> should.equal(Error(Nil))
603-
}
604-
605591
pub fn unique_test() {
606592
list.unique([1, 1, 2, 3, 4, 4, 4, 5, 6])
607593
|> should.equal([1, 2, 3, 4, 5, 6])

0 commit comments

Comments
 (0)