# Interacting with current and next iterator

**URL:** <https://discourse.julialang.org/t/interacting-with-current-and-next-iterator/102487>\
**Category:** General Usage\
**Tags:** iterators\
**Created:** [August 4, 2023, 2:03pm UTC](https://discourse.julialang.org/t/interacting-with-current-and-next-iterator/102487 "2023-08-04T14:03:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![astro-kevin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/astro-kevin/32/43342_2.png) [@astro-kevin](https://discourse.julialang.org/u/astro-kevin)\
**Post date:** [August 4, 2023, 2:03pm UTC](https://discourse.julialang.org/t/interacting-with-current-and-next-iterator/102487/1 "2023-08-04T14:03:25Z")

</div>

Say `a` is an iterator (not a vector or anything that can be indexed). I have function `f(x,y) = x+y`, and I want to have the function act on successive values of a.

Say for our example `a` is an iterator for all odd numbers starting from 1. The expected behavior is for f to return f(3,1)=4, then f(5,3)=8, etc. Thus far I could not find a way to preserve state through iterations like this without using a global variable then storing the value of the iterator there.

Is there a way to do what I’m looking for?

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [August 4, 2023, 2:10pm UTC](https://discourse.julialang.org/t/interacting-with-current-and-next-iterator/102487/2 "2023-08-04T14:10:36Z")

</div>

IterTools.partition(a,2,1)?

---

<div class="post-metadata">

**Author:** ![sbuercklin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sbuercklin/32/15728_2.png) [@sbuercklin](https://discourse.julialang.org/u/sbuercklin)\
**Post date:** [August 4, 2023, 2:10pm UTC](https://discourse.julialang.org/t/interacting-with-current-and-next-iterator/102487/3 "2023-08-04T14:10:38Z")

</div>

Does `partition(iterator_of_odds, 2, 1)` from `IterTools.jl` do what you need?

[https://juliacollections.github.io/IterTools.jl/stable/#partition(xs,-n,-[step])](https://juliacollections.github.io/IterTools.jl/stable/#partition(xs,-n,-%5Bstep%5D))

```julia
In [3]: using IterTools

In [4]: v = 1:2:21
Out[4]: 1:2:21

In [5]: collect(partition(v, 2, 1))
Out[5]: 10-element Vector{Tuple{Int64, Int64}}:
 (1, 3)
 (3, 5)
 (5, 7)
 (7, 9)
 (9, 11)
 (11, 13)
 (13, 15)
 (15, 17)
 (17, 19)
 (19, 21)

```
