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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:28:08+00:00 2026-06-08T19:28:08+00:00

Being somewhat new to Clojure I can’t seem to figure out how to do

  • 0

Being somewhat new to Clojure I can’t seem to figure out how to do something that seems like it should be simple. I just can’t see it. I have a seq of vectors. Let’s say each vector has two values representing customer number and invoice number and each of the vectors represents a sale of an item. So it would look something like this:

([ 100 2000 ] [ 100 2000 ] [ 101 2001 ] [ 100 2002 ])

I want to count the number of unique customers and unique invoices. So the example should produce the vector

[ 2 3 ]

In Java or another imperative language I would loop over each one of the vectors in the seq, add the customer number and invoice number to a set then count the number of values in each set and return it. I can’t see the functional way to do this.

Thanks for the help.

EDIT: I should have specified in my original question that the seq of vectors is in the 10’s of millions and actually has more than just two values. So I want to only go through the seq one time and calculate these unique counts (and some sums as well) on that one run through the seq.

  • 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-08T19:28:10+00:00Added an answer on June 8, 2026 at 7:28 pm

    In Clojure you can do it almost the same way – first call distinct to get unique values and then use count to count results:

    (def vectors (list [ 100 2000 ] [ 100 2000 ] [ 101 2001 ] [ 100 2002 ]))
    (defn count-unique [coll] 
       (count (distinct coll)))
    (def result [(count-unique (map first vectors)) (count-unique (map second vectors))])
    

    Note that here you first get list of first and second elements of vectors (map first/second vectors) and then operate on each separately, thus iterating over collection twice. If performance does matter, you may do same thing with iteration (see loop form or tail recursion) and sets, just like you would do in Java. To further improve performance you can also use transients. Though for beginner like you I would recommend first way with distinct.

    UPD. Here’s version with loop:

    (defn count-unique-vec [coll]
      (loop [coll coll, e1 (transient #{}), e2 (transient #{})]
        (cond (empty? coll) [(count (persistent! e1)) (count (persistent! e2))]
              :else (recur (rest coll)
                           (conj! e1 (first (first coll)))
                           (conj! e2 (second (first coll)))))))
    (count-unique-vec vectors)    ==> [2 3]
    

    As you can see, no need in atoms or something like that. First, you pass state to each next iteration (recur call). Second, you use transients to use temporary mutable collections (read more on transients for details) and thus avoid creation of new object each time.

    UPD2. Here’s version with reduce for extended question (with price):

    (defn count-with-price
      "Takes input of form ([customer invoice price] [customer invoice price] ...)  
       and produces vector of 3 elements, where 1st and 2nd are counts of unique    
       customers and invoices and 3rd is total sum of all prices"
      [coll]
      (let [[custs invs total]
            (reduce (fn [[custs invs total] [cust inv price]]
                      [(conj! custs cust) (conj! invs inv) (+ total price)])
                [(transient #{}) (transient #{}) 0]
                coll)]
        [(count (persistent! custs)) (count (persistent! invs)) total]))
    

    Here we hold intermediate results in a vector [custs invs total], unpack, process and pack them back to a vector each time. As you can see, implementing such nontrivial logic with reduce is harder (both to write and read) and requires even more code (in looped version it is enough to add one more parameter for price to loop args). So I agree with @ammaloy that for simpler cases reduce is better, but more complex things require more low-level constructs, such as loop/recur pair.

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

Sidebar

Related Questions

I'm fairly new to CodeIgniter, and am trying to figure out something I feel
this is somewhat tricky to figure out I think, perhaps I am missing something.
I have a question that may sound odd, but being somewhat of a newbie,
Being new to SQLAlchemy, I was wondering at what time would Session() should be
Being new to OpenCL i would like to know if the following scenario is
being somewhat new to xml, I see examples where scripts are embedded within xml.
I have a mock being created like this: var mock = new Mock<IPacket>(MockBehavior.Strict); mock.Setup(p
I'm somewhat new to Python, Django, and I'd like some advice on how to
Being somewhat new to search engines, the notions of indexes and types are not
Apologies for the somewhat vague title, I can't work out what the keywords are

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.