mirror of
https://github.com/nix-community/nixvim.git
synced 2025-06-21 00:25:42 +02:00
lib/util: add groupListBySize
Splits up a list into many sub-lists based on the given max-size. e.g. ```nix groupListBySize 2 [ 1 2 3 4 5 ] => [ [ 1 2 ] [ 3 4 ] [ 5 ] ] ```
This commit is contained in:
parent
693e749edb
commit
8f99c3953c
3 changed files with 61 additions and 0 deletions
|
@ -147,4 +147,23 @@ rec {
|
|||
${string}
|
||||
EOF
|
||||
'';
|
||||
|
||||
# Split a list into a several sub-list, each with a max-size of `size`
|
||||
groupListBySize =
|
||||
size: list:
|
||||
reverseList (
|
||||
foldl' (
|
||||
lists: item:
|
||||
let
|
||||
first = head lists;
|
||||
rest = drop 1 lists;
|
||||
in
|
||||
if lists == [ ] then
|
||||
[ [ item ] ]
|
||||
else if length first < size then
|
||||
[ (first ++ [ item ]) ] ++ rest
|
||||
else
|
||||
[ [ item ] ] ++ lists
|
||||
) [ ] list
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue