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

Related Questions

I have a large amount of data files to process and to be stored
How does one implement a multithreaded single process model in linux fedora under c
I have a large-ish Visual Studio 2010 solution (~110 C# projects). One project (let's
I have one very large, un-normalized table which I am in the process of
does one perform better over the other in terms of indexing/quering etc ? e.g.
How does one wait until all of the Javascript is loaded before curling a
How does one use rm to delete a file named '--help'? When I try,
How does one write a (Intel) F90 function that converts a string into lowercase
How does one optimize if the parameter space is only integers (or is otherwise
How does one determine what is the trigger of an event (close browser, close

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.