Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8388939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T18:35:11+00:00 2026-06-09T18:35:11+00:00

I have a large text file I want to process in Clojure. I need

  • 0

I have a large text file I want to process in Clojure.
I need to process it 2 lines at a time.

I settled on using a for loop so I could pull 2 lines for each pass with the following binding (rdr is my reader):

[[line-a line-b] (partition 2 (line-seq rdr))]

(I would be interested in knowing other ways to get 2 lines for each loop iteration but that is not the point of my question).

When trying to get the loop to work (using a simpler binding for these tests), I am seeing the following behavior that I can’t explain:

Why does

(with-open [rdr (reader "path/to/file")]  
    (for [line (line-seq rdr)]  
          line))  

trigger a Stream closed exception

while

(with-open [rdr (reader "path/to/file")]  
    (doseq [line (line-seq rdr)]  
        (println line)))

works?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T18:35:13+00:00Added an answer on June 9, 2026 at 6:35 pm

    for is lazy and just returns the head of the sequence that will eventually read the data from the file. The file is already closed when the for’s contents are printed by your repl. you can fix this pu wrapping the for in a doall

    (with-open [rdr (reader "path/to/file")]  
        (doall (for [line (line-seq rdr)]  
          line)))  
    

    Though this unlazys the sequence.

    here is a sample of a function out of my misc.clj that lazily closes the file at it’s end:

    (defn byte-seq [rdr]
      "create a lazy seq of bytes in a file and close the file at the end"
      (let [result (. rdr read)]
        (if (= result -1)
         (do (. rdr close) nil)
         (lazy-seq (cons result (byte-seq rdr))))))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large text file I am reading from and I need to
I have a a large text file (over 70mb) and need to count the
I have a large text file. Each time my program runs, it needs to
I want to convert the text file into xml file .I have a large
I have a rather large text file (~4m lines) I'd like to parse and
iPad application I have a large text file which I want to divide into
I have a large text file in the format of. english word: spanish equivalent
I have to remove quotes from a relatively large text file. I have looked
Okay, basically, I have a large list of phone numbers in a text file
I have several large text files (30m+ lines, >1GB) which are being processed in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.