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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T22:58:05+00:00 2026-06-02T22:58:05+00:00

I read a lot of documentation about Clojure (and shall need to read it

  • 0

I read a lot of documentation about Clojure (and shall need to read it again) and read several Clojure questions here on SO to get a “feel” of the language. Besides a few tiny functions in elisp I’ve never written in any Lisp language before. I wrote my first project Euler solution in Clojure and before going further I’d like to better understand something about map and reduce.

Using a lambda, I ended up with the following (to sum all multiple of either 3 or 5 or both between 1 and 1000 inclusive):

(reduce + (map #(if (or (= 0 (mod %1 3)) (= 0 (mod %1 5))) %1 0) (range 1 1000)))

I put it on one line because I wrote it on the REPL (and it gives the correct solution).

Without the lambda, I wrote this:

(defn val [x] (if (or (= 0 (mod x 3)) (= 0 (mod x 5))) x 0))

And then I compute the solution doing this:

(reduce + (map val (range 1 1000)))

In both cases, my question concerns what the map should return, before doing the reduce. After doing the map I noticed I ended up with a list looking like this: (0 0 3 0 5 6 …).

I tried removing the ‘0’ at the end of the val definition but then I received a list made of (nil nil 3 nil 5 6 etc.). I don’t know if the nil are an issue or not. I figured out that I was going to sum while doing a fold-left anyway so that the zero weren’t really an issue.

But still: what’s a sensible map to return? (0 0 3 0 5 6 …) or (nil nil 3 nil 5 6…) or (3 5 6 …) (how would I go about this last one?) or something else?

Should I “filter out” the zeroes / nils and if so how?

I know I’m asking a basic question but map/reduce is obviously something I’ll be using a lot so any help is welcome.

  • 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-02T22:58:06+00:00Added an answer on June 2, 2026 at 10:58 pm

    It sounds like you already have an intuative undestanding of the need to seperate mapping concerns form the reducing It’s perfectly natural to have data produced by map that is not used by the reduce. infact using the fact that zero is the identity value for addition make this even more elegant.

    • mappings job is to produce the new data (in this case 3 5 or “ignore”)
    • reduces job is to decide what to include and to produce the final result.

    what you started with is idiomatic clojure and there is no need to complicate it any more,
    so this next example is just to illustrate the point of having map decide what to include:

    (reduce #(if-not (zero? %1) (+ %1 %2) %2) (map val (range 10)))
    

    in this contrived example the reduce function ignores the zeros. In typical real world code if the idea was as simple as filtering out some value then people tend to just use the filter function

    (reduce + (filter #(not (zero? %)) (map val (range 10))))
    

    you can also just start with filter and skip the map:

    (reduce + (filter #(or (zero? (rem % 3)) (zero? (rem % 5))) (range 10)))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read all the documentation about hooks , similar questions and a lot of
I read a lot of documentation about this before i decided to ask. So
I read a lot of documentation about custom module creation for Magento. For my
I've read a lot of documentation about FQL (awesome!) and finally I've found two
I've read a lot about how I need to rethink implementing entities and joins
I like a lot of what I've read about D. Unified Documentation (That would
I've read through a lot of the documentation and for some reason this still
I read the Sun documentation and a lot of posts on Stack Overflow, but
I got question about log4net is it working on IIS 7,5? I read lot
I read a lot, also here, but couldn't find a simple way to do

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.