Skip to content

Commit

Permalink
Remove list.at
Browse files Browse the repository at this point in the history
  • Loading branch information
lpil committed May 29, 2024
1 parent 701ecae commit ca8bceb
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions exercises/practice/matrix/.meta/example.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import gleam/string
pub fn row(index: Int, matrix: String) -> Result(List(Int), Nil) {
matrix
|> parse_rows()
|> result.then(fn(rows) { list.at(rows, index - 1) })
|> result.then(fn(rows) { index_into(rows, index - 1) })
}

pub fn column(index: Int, matrix: String) -> Result(List(Int), Nil) {
Expand All @@ -15,7 +15,7 @@ pub fn column(index: Int, matrix: String) -> Result(List(Int), Nil) {
|> result.then(fn(rows) {
rows
|> list.transpose()
|> list.at(index - 1)
|> index_into(index - 1)
})
}

Expand All @@ -28,3 +28,11 @@ fn parse_rows(input: String) -> Result(List(List(Int)), Nil) {
|> list.try_map(int.parse)
})
}

fn index_into(list: List(a), index: Int) -> Result(a, Nil) {
case list {
[] -> Error(Nil)
[first, ..] if index == 0 -> Ok(first)
[_, ..rest] -> index_into(list, index - 1)
}
}

0 comments on commit ca8bceb

Please sign in to comment.