Is there any algorithm in Swift Core or Swift Algorithms Package that would allow mapping collection's elements based on the tuple (previousElement, currentElement, nextElement) ?
[1].windows(ofCount: 3) would equal []. wheres we'd like to have [nil, 1, nil]
windows will never return nil for next / previous element. So we'd need to manually add the "first" and "last" window. Something like this: [nil, array[0], array[1]] + array.windows(ofCount: 3) + [array[lastIdx-1] array[lastIdx], nil]
If you do need the empty neigbours at the start/end of the collection, it shouldn't be too much work to update the implementation for your use case. You can also look at AdjacentPairs.