groupBy

The groupBy function is the non-overloaded version of group. When a supplied relation is not transitive, it is important to remember that equality is checked against the first element in the group, not against the nearest neighbour:
>>> groupBy (\a b -> b - a < 5) [0..19]
[[0,1,2,3,4],[5,6,7,8,9],[10,11,12,13,14],[15,16,17,18,19]]
It's often preferable to use Data.List.NonEmpty.groupBy, which provides type-level guarantees of non-emptiness of inner lists.

Examples

>>> groupBy (/=) [1, 1, 1, 2, 3, 1, 4, 4, 5]
[[1],[1],[1,2,3],[1,4,4,5]]
>>> groupBy (>) [1, 3, 5, 1, 4, 2, 6, 5, 4]
[[1],[3],[5,1,4,2],[6,5,4]]
>>> groupBy (const not) [True, False, True, False, False, False, True]
[[True,False],[True,False,False,False],[True]]
groupBy operates like group, but uses the provided equality predicate instead of ==.
The groupBy function is the non-overloaded version of group.
The groupBy function is the non-overloaded version of group.
O(n) Group characters in a string according to a predicate.
The groupBy function is the non-overloaded version of group.
Grouping input according to an equality function. Subject to fusion Since 0.3.0
This function compares adjacent elements of a list. If two adjacent elements satisfy a relation then they are put into the same sublist. Example:
>>> groupBy (<) "abcdebcdef"
["abcde","bcdef"]
In contrast to that groupBy compares the head of each sublist with each candidate for this sublist. This yields
>>> List.groupBy (<) "abcdebcdef"
["abcdebcdef"]
The second b is compared with the leading a. Thus it is put into the same sublist as a. The sublists are never empty. Thus the more precise result type would be [(a,[a])].
Perform a Fold while grouping the data according to a specified group projection function. Returns the folded result grouped as a map keyed by the group.
The groupBy function is the non-overloaded version of group. When a supplied relation is not transitive, it is important to remember that equality is checked against the first element in the group, not against the nearest neighbour:
>>> groupBy (\a b -> b - a < 5) [0..19]
[[0,1,2,3,4],[5,6,7,8,9],[10,11,12,13,14],[15,16,17,18,19]]
It's often preferable to use Data.List.NonEmpty.groupBy, which provides type-level guarantees of non-emptiness of inner lists.
Groups elements using results of the given function as keys.
>>> groupBy even [1..6] :: HashMap Bool (NonEmpty Int)
fromList [(False,5 :| [3,1]),(True,6 :| [4,2])]
Group elements of a stream in accordance with the supplied comparison.
>>> S.print $ mapped S.toList $ S.groupBy (>=) $ each [1,2,3,1,2,3,4,3,2,4,5,6,7,6,5]
[1]
[2]
[3,1,2,3]
[4,3,2,4]
[5]
[6]
[7,6,5]
The groupBy function is the non-overloaded version of group.
group takes a sequence and returns a list of sequences such that the concatenation of the result is equal to the argument. Each subsequence in the result contains only equal elements, using the supplied equality test.
> groupBy (==) Mississippi
[M,"i","ss","i","ss","i","pp","i"]
Improper lens that splits after the first group of matching bytes, as defined by the given equality predicate
The groupBy function is a generalized version of group.
The groupBy function is a generalized version of group.
Generic version of group.
O(n) Group characters in a string according to a predicate.
GROUP BY clause. You can enclose multiple columns in a tuple.
select $ from \(foo `InnerJoin` bar) -> do
on (foo ^. FooBarId ==. bar ^. BarId)
groupBy (bar ^. BarId, bar ^. BarName)
return (bar ^. BarId, bar ^. BarName, countRows)
With groupBy you can sort by aggregate functions, like so (we used let to restrict the more general countRows to SqlSqlExpr (Value Int) to avoid ambiguity---the second use of countRows has its type restricted by the :: Int below):
r <- select $ from \(foo `InnerJoin` bar) -> do
on (foo ^. FooBarId ==. bar ^. BarId)
groupBy $ bar ^. BarName
let countRows' = countRows
orderBy [asc countRows']
return (bar ^. BarName, countRows')
forM_ r $ \(Value name, Value count) -> do
print name
print (count :: Int)

Need more columns?

The ToSomeValues class is defined for SqlExpr and tuples of SqlExprs. We only have definitions for up to 8 elements in a tuple right now, so it's possible that you may need to have more than 8 elements. For example, consider a query with a groupBy call like this:
groupBy (e0, e1, e2, e3, e4, e5, e6, e7)
This is the biggest you can get with a single tuple. However, you can easily nest the tuples to add more:
groupBy ((e0, e1, e2, e3, e4, e5, e6, e7), e8, e9)
groupBy splits a Producer into two Producers after the first group of elements that are equal according to the equality predicate
The groupBy function is the non-overloaded version of group.
O(n log n). Group using a user supplied function.