Consider the following function that takes a vector [x_1,...,x_n] and returns a list of [Line(x_1,x_2), Line(x_2,x_3)...] in Julia:
p(x) = map(Line, x[1:end-1], x[2:end])
What is the "functional programming" approach for this kind of operation where we want to use pairs, triples, or multiple values in a loop? In other words, what is the FP method for a "rolling window"?
I thought of using a foldl, but this does not work properly, as it returns something like Line(1,Line (2, Line(3,4))). Similarly, the zip function does not seem to provide the correct behavior. The functiion should work for any Vector type, hence, it should not use stuff like [2:end], where one assumes that the vector has at least two values...
I imagine that p(x) should be something similar to a fold in the sense of having an init. For a singleton vector could perhaps return Line(init, x[1]), and for an empty vector it could return Line(init,init).