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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:19:39+00:00 2026-06-14T21:19:39+00:00

Reading through the Clojure docs and I’m confused by one example of the reduce

  • 0

Reading through the Clojure docs and I’m confused by one example of the reduce function. I understand what reduce does, but there’s a lot going on in this example and I’m not sure how it’s all working together.

(reduce
  (fn [primes number]
    (if (some zero? (map (partial mod number) primes))
      primes
      (conj primes number)))
  [2]
  (take 1000 (iterate inc 3)))

=> [2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997]

From what I understand, reduce takes a function, in this case an anonymous function. That function takes two arguments, a collection and a number. Then we have a conditional statement that checks if the number zero appears in the collection.

(map (partial mod number) primes) confuses me. Doesn’t mod take two arguments and return the remainder of dividing the first by the second?

It appears that if this conditional returns true, it returns the collection of primes. If not, add the number to the collection of primes. Is that correct?

And the final line, it’s a collection of 1,000 numbers starting at 3. Would someone be able to walk through this function?

  • 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-14T21:19:40+00:00Added an answer on June 14, 2026 at 9:19 pm

    You probably already know this but the first thing to note is the following property: A number is prime if it is indivisible by any prime number smaller than it.

    So the anonymous function starts with a vector of previous primes, and then checks if number is divisible by any of the previous primes. If it is divisible by any of the previous primes, it will just pass on the vector of previous primes, otherwise it will add the current number to the vector of primes and then return the new vector.

    (partial mod number) is equivalent to (fn [x] (mod number x)) in this case.

    To step through a few cases:

    ;Give a name to the anonymous function
    (defn prime-checker [primes number]
      (if (some zero? (map (partial mod number) primes))
          primes
          (conj primes number)))
    
    ;This is how reduce will call the anonymous function
    (prime-checker [2] 3) 
    

    -> ((map (partial mod number) primes) = [1]

    -> will return [2 3]

    (prime-checker [2 3] 4) 
    

    -> ((map (partial mod number) primes) = [0 1]

    -> some zero? finds a zero value here so the function will return [2 3]

    (prime-checker [2 3] 5)
    

    -> ((map (partial mod number) primes) = [1 2]

    -> will return [2 3 5]

    Hopefully you can see from this how reduce with this function returns a list of primes.

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

Sidebar

Related Questions

I'm reading through some MSDN example code on Composite controls and finding that there
I've been reading through Programming Clojure, and I've been having some trouble understanding Stuarts
Reading through the Wikipedia article on First-Class functions, there is a nice table of
It's clear from reading through threads that I can call a PHP function using
I'm currently reading through this jquery masking plugin to try and understand how it
Reading through the C specs I found this function: double remquo(double x, double y,
I am trying to modify the example http://www.html5rocks.com/en/tutorials/file/dndfiles/#toc-reading-files to make function handleFileSelect(evt) return reader.result;
Reading through the javase api docs, I noticed that pretty much all of the
Reading through the pthread tutorial from LLNL I hit the following example code /******************************************************************************
When reading through this Async Sockets example , I find this code: // Get

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.