groupBy -package:esqueleto

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.
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.
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.
Given an input stream [a,b,c,...] and a comparison function cmp, the parser assigns the element a to the first group, then if a `cmp` b is True b is also assigned to the same group. If a `cmp` c is True then c is also assigned to the same group and so on. When the comparison fails the parser is terminated. Each group is folded using the Fold f and the result of the fold is the result of the parser.
  • Stops - when the comparison fails.
  • Fails - never.
>>> :{
runGroupsBy eq =
Stream.fold Fold.toList
. Stream.parseMany (Parser.groupBy eq Fold.toList)
. Stream.fromList
:}
>>> runGroupsBy (<) []
[]
>>> runGroupsBy (<) [1]
[Right [1]]
>>> runGroupsBy (<) [3, 5, 4, 1, 2, 0]
[Right [3,5,4],Right [1,2],Right [0]]