>>> 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.
>>> 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 (<) "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])].
>>> 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.
>>> groupBy even [1..6] :: HashMap Bool (NonEmpty Int) fromList [(False,5 :| [3,1]),(True,6 :| [4,2])]
>>> 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]
> groupBy (==) Mississippi [M,"i","ss","i","ss","i","pp","i"]
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)
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)