nix-config/lib/lists.nix

34 lines
736 B
Nix
Raw Permalink 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);
2023-10-26 20:36:32 +02:00
# Transform a nullable value into a list of zero/one element.
#
# nullableToList ::
# (nullable a) -> [ a ]
nullableToList = x: if x != null then [ x ] else [ ];
2021-08-24 22:25:12 +02:00
}