Skip to content

Commit

Permalink
Use built-in list.partition
Browse files Browse the repository at this point in the history
  • Loading branch information
Janiczek committed Dec 5, 2024
1 parent bdb47a7 commit 5da48e5
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 13 deletions.
2 changes: 1 addition & 1 deletion src/aoc_2024/day_5.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn fix_order_rec(todos: List(#(Int, Set(Int))), acc: List(Int)) -> List(Int) {
True -> acc
False -> {
let #(usable, rest) =
extra.list_partition(todos, fn(todo_) { set.is_empty(todo_.1) })
list.partition(todos, fn(todo_) { set.is_empty(todo_.1) })
let usable_pages: List(Int) = usable |> list.map(fn(todo_) { todo_.0 })
let cleaned_rest: List(#(Int, Set(Int))) =
rest
Expand Down
12 changes: 0 additions & 12 deletions src/extra.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,3 @@ pub fn strip_left(from input: String, remove prefix: String) -> String {
False -> input
}
}

/// Splits list into a #(good, bad) based on a predicate (true, false)
pub fn list_partition(xs: List(a), pred: fn(a) -> Bool) -> #(List(a), List(a)) {
let #(good_rev, bad_rev) =
list.fold(xs, #([], []), fn(acc, x) {
case pred(x) {
True -> #([x, ..acc.0], acc.1)
False -> #(acc.0, [x, ..acc.1])
}
})
#(list.reverse(good_rev), list.reverse(bad_rev))
}

0 comments on commit 5da48e5

Please sign in to comment.