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

  • Home
  • SEARCH
  • 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 6780691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T16:32:08+00:00 2026-05-26T16:32:08+00:00

I’m learning Clojure and as an exercise I wanted to write something like the

  • 0

I’m learning Clojure and as an exercise I wanted to write something like the unix “comm” command.

To do this, I read the contents of each file into a set, then use difference/intersection to show exclusive/common files.

After a lot of repl-time I came up with something like this for the set creation part:

(def contents (ref #{}))
(doseq [line (read-lines "/tmp/a.txt")]
  (dosync (ref-set contents (conj @contents line))))

(I’m using duck-streams/read-lines to seq the contents of the file).

This is my first stab at any kind of functional programming or lisp/Clojure. For instance, I couldn’t understand why, when I did a conj on the set, the set was still empty. This lead me to learning about refs.

  1. Is there a better Clojure/functional way to do this? By using ref-set, am I just twisting the code to a non-functional mindset or is my code along the lines of how it should be done?
  2. Is there a a library that already does this? This seems like a relatively ordinary thing to want to do but I couldn’t find anything like it.
  • 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-26T16:32:08+00:00Added an answer on May 26, 2026 at 4:32 pm

    Clojure 1.3:

    user> (require '[clojure.java [io :as io]])
    nil
    user> (line-seq (io/reader "foo.txt"))
    ("foo" "bar" "baz")
    user> (into #{} (line-seq (io/reader "foo.txt")))
    #{"foo" "bar" "baz"}
    

    line-seq gives you a lazy sequence where each item in the sequence is a line in the file.

    into dumps it all into a set. To do what you were trying to do (add each item one by one into a set), rather than doseq and refs, you could do:

    user> (reduce conj #{} (line-seq (io/reader "foo.txt")))
    #{"foo" "bar" "baz"}
    

    Note that the Unix comm compares two sorted files, which is likely a more efficient way to compare files than doing set intersection.

    Edit: Dave Ray is right, to avoid leaking open file handles it’s better to do this:

    user> (with-open [f (io/reader "foo.txt")]
            (into #{} (line-seq f)))
    #{"foo" "bar" "baz"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string

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.