# Is there a way to slice an \`IOStream\`?

**URL:** <https://discourse.julialang.org/t/is-there-a-way-to-slice-an-iostream/26517>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [July 18, 2019, 8:02pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-slice-an-iostream/26517 "2019-07-18T20:02:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [July 18, 2019, 8:02pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-slice-an-iostream/26517/1 "2019-07-18T20:02:38Z")

</div>

I have an `IOStream`, and I only want to read between the `m`th line and `n`th line. How should I do?  
I have a pseudocode:

```julia
using ResumableFunctions

@resumable function iterate_io_between(io::IOStream, m::Int, n::Int)
    io = seek(io, m)
    while pos != n
        pos = position(io)
        @yield io
    end
end

```

I am not sure the `@yield` statement works, since I have not used `ResumableFunctions` before.

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 18, 2019, 8:11pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-slice-an-iostream/26517/2 "2019-07-18T20:11:14Z")

</div>

`seek` operates on byte offsets but it seems you want `m` and `n` to be lines. You can use `readline` until you get to line `m` and `n` respectively. There is no way to immediately “jump” to the correct line in a file.

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [July 18, 2019, 8:28pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-slice-an-iostream/26517/3 "2019-07-18T20:28:06Z")

</div>

Thanks. You are right. But then I am still confused about how to write the code. How should I “start” "yield"ing the `IOStream` at the `m`th line and stop it at the `n`th line? Like this?

```julia
@resumable function iterate_io_between(io::IOStream, start::Int, stop::Int)
    for i in eachindex(1:stop)
        if i == start
            @yield io
        else
            continue
        end # if-else
    end # for
end

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 18, 2019, 9:04pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-slice-an-iostream/26517/4 "2019-07-18T21:04:40Z")

</div>

Do you necessarily have to use `ResumableFunctions`? You could just compose some iterators:

```julia
shell> cat file
1
2
3
4
5

julia> iterate_io_between(io, start, stop) = Iterators.take(Iterators.drop(eachline(io), start - 1), stop - start + 1)
iterate_io_between (generic function with 1 method)

julia> iter = iterate_io_between(open("file", "r"), 2, 4);

julia> collect(iter)
3-element Array{String,1}:
 "2"
 "3"
 "4"

```

---

<div class="post-metadata">

**Author:** ![singularitti](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/singularitti/32/17678_2.png) [@singularitti](https://discourse.julialang.org/u/singularitti)\
**Post date:** [July 18, 2019, 9:59pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-slice-an-iostream/26517/5 "2019-07-18T21:59:19Z")

</div>

Thank you for your help. The reason why I want to use `ResumableFunctions` is that I want the return type of `iterate_io_between` is also an `IOStream`. If I am using your solution, then the type will be a `Base.Iterators.Take`.
