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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:46:21+00:00 2026-06-17T00:46:21+00:00

Continuing my journey of using Udacity’s python-based CS212 and translating to Clojure, I am

  • 0

Continuing my journey of using Udacity’s python-based CS212 and translating to Clojure, I am trying to write a function that:

  1. Returns all items in a sequence,
  2. that are equal to the maximum of the sequence,
  3. using a “key” function that returns a vector

Clojure’s max-key function does almost this, except that it requires my key to return an integer, which in this case I don’t–I return a vector. With some modification, I can use the compare function to compare the vectors returned by my keying function:

(defn all-max-key
  "Returns a vector of x for which (k x), arbitrated by compare, are greatest."
  ([k x] x)
  ([k x y] (if (= 1 (compare (k x) (k y))) x y))
  ([k x y & more]
     (reduce #(all-max-key k %1 %2) (all-max-key k x y) more)))

Now, the problem is that this doesn’t account for ties (where compare returns 0). In other words, all elements in my list are unique, but some may have equivalent values per my keying function. In imperative world, I might loop through the list, keeping track of my max value, comparing each element to it, and then appending / replacing a mutable list as I go.

But I feel like there has to be an idiomatic, elegant, functional way of doing this without resorting to loops. My attempts to use reduce have all resulted in nonsensical comparisons of sequences to member elements. Can anyone shine light on this?

  • 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-17T00:46:22+00:00Added an answer on June 17, 2026 at 12:46 am

    This returns a vector of the maximal items:

    (defn maximal-key [k x & xs]
      (reduce (fn [ys x]
                (let [c (compare (k x) (k (peek ys)))]
                  (cond
                    (pos? c) [x]
                    (neg? c) ys
                    :else    (conj ys x))))
              [x]
              xs))
    

    Testing at the REPL:

    ;; convoluted key function to demonstrate that vector keys are fine
    user> (maximal-key #(vector (first %))
                        (list 1 2 3) (list 4 5 6) (list 3 6 8)
                        (list 4 7 9) (list 1 5 9))
    [(4 5 6) (4 7 9)]
    

    Note that compare may return an arbitrary positive integer to indicate its first argument is greater than the second — hence the pos? in place of == 1.

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

Sidebar

Related Questions

Continuing to learn WCF, I'm trying to write a small program that would with
Continuing off this post: Performance hit incurred using NSMutableDictionary vs. NSMutableArray> I am trying
Continuing my journey into more advanced OO, I'm trying to come up with the
Continuing on my attempt to create a DateTime class , I am trying to
Continuing from this question : When I am trying to do fopen on Windows,
Continuing my quest to perform a dropbox query I am using http-conduit to try
Continuing with the spirit of using the Stack Exchange Data Explorer to learn SQL,
Continuing from this question With each system call, the function constructs a set of
Continuing investigation on a embedded WindowsMediaPlayer problem , i am trying to do simple
Continuing on my previous question , I'm trying to initialize a session-scoped JSF bean

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.