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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:27:59+00:00 2026-05-30T04:27:59+00:00

In clojure, I want to aggregate this data: (def data [[:morning :pear][:morning :mango][:evening :mango][:evening

  • 0

In clojure, I want to aggregate this data:

(def data [[:morning :pear][:morning :mango][:evening :mango][:evening :pear]])
(group-by first data)
;{:morning [[:morning :pear][:morning :mango]],:evening [[:evening :mango][:evening :pear]]}

My problem is that :evening and :morning are redundant.
Instead, I would like to create the following collection:

([:morning (:pear :mango)] [:evening (:mango :pear)])

I came up with:

(for [[moment moment-fruit-vec] (group-by first data)] [moment (map second moment-fruit-vec)])

Is there a more idiomatic solution?

  • 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-30T04:28:00+00:00Added an answer on May 30, 2026 at 4:28 am

    I’ve come across similar grouping problems. Usually I end up plugging merge-with or update-in into some seq processing step:

    (apply merge-with list (map (partial apply hash-map) data))
    

    You get a map, but this is just a seq of key-value pairs:

    user> (apply merge-with list (map (partial apply hash-map) data))
    {:morning (:pear :mango), :evening (:mango :pear)}
    user> (seq *1)
    ([:morning (:pear :mango)] [:evening (:mango :pear)])
    

    This solution only gets what you want if each key appears twice, however. This might be better:

    (reduce (fn [map [x y]] (update-in map [x] #(cons y %))) {} data)
    

    Both of these feel “more functional” but also feel a little convoluted. Don’t be too quick to dismiss your solution, it’s easy-to-understand and functional enough.

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

Sidebar

Related Questions

I want to automate filling in data on a website using clojure. For this
I have a group of numeric functions in Clojure that I want to validate
I want to index clojure files, using etags so that i can use Emacs's
I want to create a Clojure macro that can read a file, when the
Say I want to make a Clojure macro that does the following: If x
I have a map in Clojure something like this: (def stuff #{ {:a help
I have a problem constructing a DSL in Clojure. This is the concrete problem
I am writing my first clojure program, and want to read lines from stdin.
I have a sql exception from clojure that goes like this: java.lang.Exception: transaction rolled
I want to use JOGL (for Clojure, not Java). There seems to be quite

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.