aboutsummaryrefslogtreecommitdiff
path: root/aoc-2020-gleam/src/ext/listx.gleam
blob: 2d4f4b6f3e0708f8ae6bc5377ff4ecdeeb1c1d75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import gleam/iterator as iter
import ext/iteratorx as iterx

pub fn count(list: List(a), satisfying predicate: fn(a) -> Bool) -> Int {
  list
  |> iter.from_list
  |> iterx.count(satisfying: predicate)
}

fn set_helper(list: List(a), value: a, index: Int, counter: Int) -> List(a) {
  case list {
    [] -> []
    [_, ..t] if counter == index -> [value, ..t]
    [h, ..t] -> [h, ..set_helper(t, value, index, counter + 1)]
  }
}

pub fn set(list: List(a), value: a, at index: Int) -> List(a) {
  set_helper(list, value, index, 0)
}