find_neighbours.gleam
edited
1fn impl(rest: List(a), first: a, prev: a) -> #(List(#(a, #(a, a))), a) {
2 case rest {
3 [] -> #([], first)
4 [x] -> #([#(x, #(prev, first))], x)
5 [x, y, ..xs] -> {
6 let #(next_list, last) = impl([y, ..xs], first, x)
7 #([#(x, #(prev, y)), ..next_list], last)
8 }
9 }
10}
11
12pub fn find_neighbours(sites: List(a)) -> List(#(a, #(a, a))) {
13 case sites {
14 [] -> []
15 [x] -> [#(x, #(x, x))]
16 [x, y] -> [#(x, #(y, y)), #(y, #(x, x))]
17 [x, y, ..xs] -> {
18 let #(final, last) = impl([y, ..xs], x, x)
19 [#(x, #(last, y)), ..final]
20 }
21 }
22}