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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:31:07+00:00 2026-05-26T07:31:07+00:00

Here’s an issue I keep running into in Clojure: user=> (max [3 4 5

  • 0

Here’s an issue I keep running into in Clojure:

user=> (max [3 4 5 6 7])
[3 4 5 6 7] ; expected '7'

Some functions don’t do what I expect!
Here’s one solution using apply:

user=> (apply max [3 4 5 6 7])
7

Other examples are concat, and min.
My question, as a Clojure newbie, is why are these functions variadic? I expected them to operate on sequences. Is using apply the best/idiomatic way to get what I want?


Note: I’m not trying to say that it’s bad to have variadic functions, or that there is a better way. I just want to know if there’s a rule or convention being followed, or if there are specific advantages to such an approach that I should be aware of.


Edit: I think the original question was unclear. Here’s what I meant:

In other programming languages I’ve used, there are monoid-like operations, such as adding numbers, finding the greater element, concatenating lists.

There are often two use cases for these operations:

1) combining two elements, using a function that accepts two arguments
2) combining 0 to n elements, using a function that accepts a list (or sequence) of elements

A function for the second case can be built from that for the first case (often using reduce).

However, Clojure adds a third use case:

3) combining 0 to n elements, using a variadic function

So the question is, why does Clojure add this third case?
Paul’s answer indicates that:

  • this allows code that is more flexible
  • there are historical forces at work
  • 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-26T07:31:08+00:00Added an answer on May 26, 2026 at 7:31 am

    1) Convenience. With math functions such as + it would be annoying to wrap everything in sequences when you just try to do some calculations.

    2) Efficiency. Wrapping everything with collections/sequences would also be inefficient, first the sequence needs to be created and then it needs to unpacked at runtime instead of looking up the right Java function at compile time.

    3) Expectations. This is how these functions work in other Lisps and similarly, with a somewhat different syntax, in other functional languages, so it is a reasonable expectation for people coming to Clojure. I would say that the idiomatic way in other functional languages to apply a function such as + to a sequence would be to use either reduce or foldl/foldr, so this is also in line with how Clojure handles it.

    4) Flexibility. The fact that these functions may be used with higher order functions, such as map makes them more convenient to use if they are variadic. Say you have three vectors and you want to apply a function to the elements at the same position. If your function is variadic then you can just use map with multiple collections (map also has to be variadic then 😉 ):

    (map + [1 2 3 4] [2 3 4 5] [3 4 5 6])
    ; [6 9 12 15]
    

    This is a lot more convenient than what you would have if all those functions just took collections.

    Idiomatic use: (Edited after kotarak’s excellent comment)

    It depends on the function if you should use reduce or apply.

    For math functions (+,-,*,/,etc.) that take 2 arguments in the Java world reduce makes more sense as it can directly use the 2 argument Java version. With apply they kind of do an implicit reduce (the function adds two arguments and then recurs with the result, the next argument and the rest. That’s pretty much what reduce does.)

    For str using apply is likely more efficient. When str is called with more than one argument it creates a StringBuilder, adds all the arguments to it and then creates a string. Using reduce the StringBuilder will be created n-1 times and only add one string each time. This is as in the Shlemiel the painter joke, resulting in O(n^2) complexity.

    Verdict so far: Using apply with math functions doesn’t hurt much, but using reduce with str may be pretty expensive.

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

Sidebar

Related Questions

Here is an example: I have a file 1.js, which has some functions. I
here is the scenario 1: user starts to type some word, autocomplete engine shows
Here's a problem I ran into recently. I have attributes strings of the form
Here is the issue I am having: I have a large query that needs
Here is what is supposed to happen: The moment the user chooses an option
Here's some of my production code (I had to force line breaks): task =
here is a link to how my APC is running : [removed] As you
Here's the problem....I have three components...A Page that contains a User Control and a
Here's my situation. I've noticed that code gets harder to maintain when you keep
Here's the situation, i want to have a user that can enter time on

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.