# Get iterator "from:to"

**URL:** <https://discourse.julialang.org/t/get-iterator-from-to/103259>\
**Category:** New to Julia\
**Created:** [August 27, 2023, 3:01pm UTC](https://discourse.julialang.org/t/get-iterator-from-to/103259 "2023-08-27T15:01:01Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Denis\_Ivanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denis_ivanov/32/52607_2.png) [@Denis\_Ivanov](https://discourse.julialang.org/u/Denis_Ivanov)\
**Post date:** [August 27, 2023, 3:01pm UTC](https://discourse.julialang.org/t/get-iterator-from-to/103259/1 "2023-08-27T15:01:01Z")

</div>

Hi, sorry for naive question. I’ve read carefully about iterators ,  
but is there a straightforward method to get “from n to m” elements of iterator,  
without such as `rest(take(iter, m), n)`?  
_NB_ I use complicated _generic_ iterator, with `Combinatorics: multiset_permutation` and `IterTools: imap`, so `rest(take(iter, m), n)` works but `[n:m]` not works

---

<div class="post-metadata">

**Author:** ![jakobnissen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jakobnissen/32/13477_2.png) [@jakobnissen](https://discourse.julialang.org/u/jakobnissen)\
**Post date:** [August 27, 2023, 3:06pm UTC](https://discourse.julialang.org/t/get-iterator-from-to/103259/2 "2023-08-27T15:06:15Z")

</div>

You can still use the `n:m` syntax, even when neither `n` nor `m` is known at compile time:

```julia
julia> f(n, m) = n:m
f (generic function with 1 method)

julia> collect(f(7, 9))
3-element Vector{Int64}:
 7
 8
 9

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 27, 2023, 3:17pm UTC](https://discourse.julialang.org/t/get-iterator-from-to/103259/3 "2023-08-27T15:17:10Z")

</div>

I think the approach you mentioned is the only approach that works with generic iterators. However, you should use [`drop`](https://docs.julialang.org/en/v1/base/iterators/#Base.Iterators.drop) instead of `rest`.

```julia
julia> using .Iterators: drop, take

julia> m_to_n(itr, m, n) = drop(take(itr, n), m-1);

julia> itr = m_to_n(1:10, 3, 8);

julia> collect(itr)
6-element Vector{Int64}:
 3
 4
 5
 6
 7
 8

```

---

<div class="post-metadata">

**Author:** ![Denis\_Ivanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/denis_ivanov/32/52607_2.png) [@Denis\_Ivanov](https://discourse.julialang.org/u/Denis_Ivanov)\
**Post date:** [August 27, 2023, 3:25pm UTC](https://discourse.julialang.org/t/get-iterator-from-to/103259/4 "2023-08-27T15:25:59Z")

</div>

Well, thank you for evidence, sorry for my bad English! “Generic” 😅  
How do you think, which combination of `drop`, `take`, `rest` be most efficient for big (with ~10^6 members) iterator?

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 27, 2023, 3:32pm UTC](https://discourse.julialang.org/t/get-iterator-from-to/103259/5 "2023-08-27T15:32:05Z")

</div>

The `drop` and `take` combination should be reasonably efficient. You should generally avoid using `Iterators.rest` since it relies on understanding how the internal `state` variable of the iterator is used, which is normally an implementation detail that is not exposed as part of the interface for a given iterator.

---

<div class="post-metadata">

**Author:** ![maxkapur](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maxkapur/32/21208_2.png) [@maxkapur](https://discourse.julialang.org/u/maxkapur)\
**Post date:** [August 27, 2023, 5:17pm UTC](https://discourse.julialang.org/t/get-iterator-from-to/103259/6 "2023-08-27T17:17:54Z")

</div>

Off-topic: Is there a reason to prefer `.Iterators` over `Base.Iterators`?

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 27, 2023, 7:17pm UTC](https://discourse.julialang.org/t/get-iterator-from-to/103259/7 "2023-08-27T19:17:18Z")

</div>

Not really. I guess it’s just a matter of taste.
