# Iterator with previous and current elements

**URL:** https://discourse.julialang.org/t/iterator-with-previous-and-current-elements/631
**Category:** General Usage
**Tags:** question
**Created:** [November 29, 2016, 2:43pm UTC](https://discourse.julialang.org/t/iterator-with-previous-and-current-elements/631 "2016-11-29T14:43:31Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [November 29, 2016, 2:43pm UTC](https://discourse.julialang.org/t/iterator-with-previous-and-current-elements/631/1 "2016-11-29T14:43:31Z")

</div>

I am wondering if it is possible to iterate over pairs of the previous and the current element in an iterable collection. Eg to implement

```julia
function pairs_sum(s)
    result = []
    state = start(s)
    while !done(s, state)
        (i, next_state) = next(s, state)
        push!(result, state+next_state)
        state = next_state
    end
    result
end

```

as

```julia
pairs_sum(s) = [sum(e) for e in (magic here)]

```

with seeing each element only once, so

```julia
pairs_sum(s) = [sum(e) for e in zip(s, drop(s,1))]

```

doesn’t count.

---

<div class="post-metadata">

### Author: ![juthohaegeman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/juthohaegeman/32/8620_2.png) [@juthohaegeman](https://discourse.julialang.org/u/juthohaegeman)
#### Post date: [November 29, 2016, 5:42pm UTC](https://discourse.julialang.org/t/iterator-with-previous-and-current-elements/631/2 "2016-11-29T17:42:01Z")

</div>

I think Base.(Iterators.)partition does what you ask.  
The extra Iterators middle part is if you are on julia 0.6
