rzv
rzv

Reputation: 2022

Clojure Docs: Understanding This Example of the Reduce Function

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?

Upvotes: 3

Views: 680

Answers (1)

onit
onit

Reputation: 6366

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.

Upvotes: 7

Related Questions