Imagine you have a vector
[1,2,3,4] and would like to have easily access adjacent elements:
I used to do it with a
for loop and then just access the
i+1-th element while looping over
length()-1. This seems rather ugly so I made a pairwise iterator
pairwise(x) = zip(x, last(Iterators.peel(x)))
for (x,y) in pairwise("abcd")
x * y = "ab"
x * y = "bc"
x * y = "cd"
Am I reinventing the wheel? Is there anything better?
Would this work?
pair2(x,i) = (x[i], x[i+1])
Use broadcasting to spit out the complete set:
That would not work with e.g. OffsetArrays.jl. It will also not work with stuff like dictionaries that are not indexable. That is why I said I don’t like the
i-th element solutions.
Introduction · IterTools
for i in partition(1:9, 2, 1)
# i = (1, 2)
# i = (2, 3)
# i = (3, 4)
# i = (4, 5)
# i = (5, 6)
# i = (6, 7)
# i = (7, 8)
# i = (8, 9)
d = Dict(:a => 1, :b => 2, :c => 3)
for i in partition(d, 2, 1)
# i = (:a => 1, :b => 2)
# i = (:b => 2, :c => 3)
@owiecc, sorry for not having read you well.
I discovered today that
pairwise(xs) = IterTools.partition(xs, 2, 1)
works correctly on iterators that can only be consumed once – such as eachline(“somefile”) – while the above solution
pairwise(xs) = zip(xs, last(Iterators.peel(xs)))
and the solution I had in my own code
pairwise(xs) = zip(xs, Iterators.drop(xs, 1))
both produce the wrong result. Thanks for pointing it out!