Update lib overlay

This commit is contained in:
Oystein Kristoffer Tveit 2021-12-23 04:34:47 +01:00
parent ba81aca285
commit 60158b4c81
6 changed files with 68 additions and 21 deletions

View File

@ -1,12 +1,49 @@
self: super: final: prev:
let let
inherit (super.lib.attrsets) listToAttrs nameValuePair; inherit (prev.lib.attrsets) mapAttrs isAttrs filterAttrs listToAttrs nameValuePair attrNames mapAttrsToList;
inherit (super.lib.lists) foldr; inherit (prev.lib.lists) foldr imap0 imap1;
in super.lib.attrsets // { in prev.lib.attrsets // rec {
# a -> [String] -> AttrSet{a} # a -> [String] -> AttrSet{a}
mapToAttrsWithConst = constant: items: mapToAttrsWithConst = constant: items:
listToAttrs (map (name: nameValuePair name constant) items); listToAttrs (map (name: nameValuePair name constant) items);
# [AttrSet] -> AttrSet # [AttrSet] -> AttrSet
concatAttrs = foldr (a: b: a // b) {}; concatAttrs = foldr (a: b: a // b) {};
# (Int -> String -> a -> a) -> AttrSet -> AttrSet
imap0Attrs = f: set:
listToAttrs (imap0 (i: attr: nameValuePair attr (f i attr set.${attr})) (attrNames set));
# (Int -> String -> a -> a) -> AttrSet -> AttrSet
imap1Attrs = f: set:
listToAttrs (imap1 (i: attr: nameValuePair attr (f i attr set.${attr})) (attrNames set));
# (Int -> String -> a -> nameValuePair) -> AttrSet -> AttrSet
imap0Attrs' = f: set:
listToAttrs (imap0 (i: attr: f i attr set.${attr}) (attrNames set));
# (Int -> String -> a -> nameValuePair) -> AttrSet -> AttrSet
imap1Attrs' = f: set:
listToAttrs (imap1 (i: attr: f i attr set.${attr}) (attrNames set));
# AttrSet -> AttrSet
recursivelyFlatten = set: let
shouldRecurse = filterAttrs (n: v: isAttrs v) set;
shouldNotRecurse = filterAttrs (n: v: !(isAttrs v)) set;
recursedAttrs = mapAttrsToList (n: v: recursivelyFlatten v) shouldRecurse;
in
concatAttrs ([shouldNotRecurse] ++ recursedAttrs);
# Takes in a predicate which decides whether or not to recurse further. (true -> recurse)
# This will let you recurse until you recurse until you hit attrsets with a special meaning
# that you would like to handle after flattening.
# It will also stop at everything other than an attribute set.
#
# (a -> Bool) -> AttrSet -> AttrSet
recursivelyFlattenUntil = pred: set: let
shouldRecurse = filterAttrs (n: v: isAttrs v && !(pred v)) set;
shouldNotRecurse = filterAttrs (n: v: !(isAttrs v) || pred v) set;
recursedAttrs = mapAttrsToList (n: v: recursivelyFlattenUntil pred v) shouldRecurse;
in
concatAttrs ([shouldNotRecurse] ++ recursedAttrs);
} }

View File

@ -1,9 +1,10 @@
self: super: final: prev:
{ {
lib = super.lib // { lib = prev.lib // {
attrsets = import ./attrsets.nix self super; attrsets = (import ./attrsets.nix) final prev;
lists = import ./lists.nix self super; lists = (import ./lists.nix) final prev;
strings = import ./strings.nix self super; strings = (import ./strings.nix) final prev;
termColors = import ./termColors.nix self super; termColors = (import ./termColors.nix) final prev;
trivial = (import ./trivial.nix) final prev;
}; };
} }

View File

@ -1,8 +1,8 @@
self: super: final: prev:
let let
inherit (super.lib.trivial) const; inherit (prev.lib.trivial) const;
inherit (super.lib.lists) range any all; inherit (prev.lib.lists) range any all;
in super.lib.lists // { in prev.lib.lists // {
# a -> Int -> [a] # a -> Int -> [a]
repeat = item: times: map (const item) (range 1 times); repeat = item: times: map (const item) (range 1 times);

View File

@ -1,8 +1,9 @@
self: super: final: prev:
let let
inherit (self.lib.lists) repeat length; inherit (final.lib.lists) repeat length;
inherit (super.lib.strings) concatStringsSep replaceStrings splitString; inherit (prev.lib.strings) concatStringsSep replaceStrings splitString;
in super.lib.strings // rec { # inherit (final.lib.strings) wrap;
in prev.lib.strings // rec {
# String -> [String] # String -> [String]
lines = splitString "\n"; lines = splitString "\n";

View File

@ -1,7 +1,8 @@
self: super: final: prev:
let let
inherit (self.lib.strings) wrap; inherit (final.lib.strings) wrap;
inherit (super.lib.attrsets) mapAttrs' nameValuePair; inherit (prev.lib.attrsets) mapAttrs' nameValuePair;
# inherit (final.lib.myStuff.termColors) escapeCharacter escapeColor resetCharacter wrapWithColor' colorMappings;
in rec { in rec {
# String # String
escapeCharacter = ""; escapeCharacter = "";

View File

@ -0,0 +1,7 @@
final: prev:
let
in prev.lib.trivial // {
# a -> b -> Either (a b)
withDefault = default: value:
if (value == null) then default else value;
}