nix-config/lib/lists.nix

28 lines
561 B
Nix
Raw Normal View History

2021-08-24 22:25:12 +02:00
{ lib, ... }:
let
2021-08-24 22:26:06 +02:00
inherit (lib) filter foldl';
2021-08-24 22:25:12 +02:00
in
{
2021-08-24 22:26:06 +02:00
# Count the number of appararitions of each value in a list.
#
# countValues ::
# [ any ] -> ({ any = int; })
countValues =
let
addToCount = acc: x:
let
v = toString x;
in
acc // { ${v} = (acc.${v} or 0) + 1; };
in
foldl' addToCount { };
2021-08-24 22:25:12 +02:00
# Filter a list using a predicate function after applying a map.
#
# mapFilter ::
# (value -> bool)
# (any -> value)
# [ any ]
mapFilter = pred: f: attrs: filter pred (map f attrs);
}