Ruby has a handy function for enumerables called each_cons
. Which "Iterates the given block for each array of consecutive elements." This is really nice. Except that this is definitely an each
method, which returns nil upon completion and not an array of the values you've looped over like map
would.
However, if I have a situation where I need to iterate over an enumerable type, take an element and its cons, then perform some operation on them and return them back into an array what can I do? Normally, I'd use map for this sort of behavior. But map_cons
doesn't exist.
An example:
Given a list of integers, I need to see which ones of those integers repeat and return a list of just those integers
[1, 1, 4, 5, 6, 2, 2] ## I need some function that will get me [1, 2]
I can say:
[1, 1, 4, 5, 6, 2, 2].each_cons(2) {|e| e[0] if e[0] == e[1]}
But, since it each
s over the array, it will complete successfully and return nil
at the end. I need it to behave like map
and not like each
.
Is this behavior something that ruby supports? Am I coming at it from the wrong direction entirely?