In Clojure, one of the most useful functions for working with collections is group-by. This function allows you to group elements of a collection based on a specified criterion, returning a map where the keys represent the grouped criteria and the values are vectors of the corresponding elements.

How It Works

The group-by function takes two arguments:

  1. A function that determines the grouping criterion.
  2. A collection of items to be grouped.

It applies the grouping function to each item in the collection and then organizes the items into groups based on the function’s return values.

Here’s the general form:

(group-by f coll)
  • f is the function that computes a key for each element in coll.
  • coll is the collection being grouped.

Suppose you want to separate a list of numbers into even and odd groups:

(group-by even? [1 2 3 4 5 6 7 8 9])

This will return:

{false [1 3 5 7 9], true [2 4 6 8]}

Here, even? is the function that determines the grouping criterion. Numbers are divided into two groups: those that are true for even? (even numbers) and those that are false (odd numbers).

Let’s say you have a collection of words, and you want to group them by their length:

(def words ["apple" "banana" "cherry" "date" "elderberry"])
(group-by count words)

In this case, count is used as the grouping function, and the words are grouped based on their length.

Practical Use Cases

  • Data classification: You can use group-by to classify data based on any property, such as categorizing products by type, transactions by date, or people by age group.
  • Organizing complex datasets: When working with large or nested collections, group-by helps break down data into manageable chunks based on specific criteria.