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 1002353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:49:12+00:00 2026-05-16T07:49:12+00:00

How does one process large binary data files in Clojure? Let’s assume data/files are

  • 0

How does one process large binary data files in Clojure? Let’s assume data/files are about 50MB – small enough to be processed in memory (but not with a naive implementation).

The following code correctly removes ^M from small files but it throws OutOfMemoryError for larger files (like 6MB):

(defn read-bin-file [file]
  (to-byte-array (as-file file)))

(defn remove-cr-from-file [file]
  (let [dirty-bytes (read-bin-file file)
        clean-bytes (filter #(not (= 13 %)) dirty-bytes)
        changed?    (< (count clean-bytes) (alength dirty-bytes))]    ; OutOfMemoryError
    (if changed?
      (write-bin-file file clean-bytes))))    ; writing works fine

It seems that Java byte arrays can’t be treated as seq as it is extremely inefficient.

On the other hand, solutions with aset, aget and areduce are bloated, ugly and imperative because you can’t really use Clojure sequence library.

What am I missing? How does one process large binary data files in Clojure?

  • 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-05-16T07:49:14+00:00Added an answer on May 16, 2026 at 7:49 am

    I would probably personally use aget / aset / areduce here – they may be imperative but they are useful tools when dealing with arrays, and I don’t find them particularly ugly. If you want to wrap them in a nice function then of course you can 🙂

    If you are determined to use sequences, then your problem will be in the construction and traversal of the seq since this will require creation and storage of a new seq object for every byte in the array. This is probably ~24 bytes for each array byte……

    So the trick is to get it to work lazily, in which case the earlier objects will be garbage collected before you get to the end of the the array. However to make this work, you’ll have to avoid holding any reference to the head of the seq when you traverse the sequence (e.g. with count).

    The following might work (untested), but will depend on write-bin-file being implemented in a lazy-friendly manner:

    (defn remove-cr-from-file [file]
      (let [dirty-bytes (read-bin-file file)
            clean-bytes (filter #(not (= 13 %)) dirty-bytes)
            changed-bytes (count (filter #(not (= 13 %)) dirty-bytes))
            changed?    (< changed-bytes (alength dirty-bytes))]   
        (if changed?
          (write-bin-file file clean-bytes))))
    

    Note this is essentially the same as your code, but constructs a separate lazy sequence to count the number of changed bytes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 502k
  • Answers 502k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There are 2 possible libraries you could use. jQuery Tools… May 16, 2026 at 2:38 pm
  • Editorial Team
    Editorial Team added an answer Use WHERE value IN ('a', 'b'): SELECT * FROM acme… May 16, 2026 at 2:38 pm
  • Editorial Team
    Editorial Team added an answer The fastest way to copy values in VBA I know… May 16, 2026 at 2:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Okay, in python one can do this: def foo(monkeys): def bar(monkey): #process and return
If I had a single server and I had two process types A(Many processes
We have a large enterprise application where projects are scoped designed and finally coded
This question is related to one I asked previously, see here . As a
We are currently using Eclipse Draw2D/GEF for an information page describing a process in
I have a DataGridView being regularly populated via data-bound objects, and the number of
The task I have is to (somewhat efficiently) read line-by-line through a very large,
Currently my application takes in a text file/files, parses them into another file type
Simple code C# winform app (visual studio 2010): A simple form with one text
I want to traverse through all the elements of an word document one by

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.