implement std functions from nixpkgs.lib
This commit is contained in:
parent
f3b09cb54f
commit
9f5827c21c
3 changed files with 95 additions and 0 deletions
80
nib/std/attrs.nix
Normal file
80
nib/std/attrs.nix
Normal file
|
|
@ -0,0 +1,80 @@
|
||||||
|
rec {
|
||||||
|
nameValuePair = name: value: {inherit name value;};
|
||||||
|
|
||||||
|
listToAttrsIdentity = values:
|
||||||
|
builtins.listToAttrs (
|
||||||
|
builtins.map
|
||||||
|
(x: nameValuePair x x)
|
||||||
|
values
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
Generate an attribute set by mapping a function over a list of
|
||||||
|
attribute names.
|
||||||
|
|
||||||
|
# Inputs
|
||||||
|
|
||||||
|
`names`
|
||||||
|
|
||||||
|
: Names of values in the resulting attribute set.
|
||||||
|
|
||||||
|
`f`
|
||||||
|
|
||||||
|
: A function, given the name of the attribute, returns the attribute's value.
|
||||||
|
|
||||||
|
# Type
|
||||||
|
|
||||||
|
```
|
||||||
|
genAttrs :: [ String ] -> (String -> Any) -> AttrSet
|
||||||
|
```
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
:::{.example}
|
||||||
|
## `lib.attrsets.genAttrs` usage example
|
||||||
|
|
||||||
|
```nix
|
||||||
|
genAttrs [ "foo" "bar" ] (name: "x_" + name)
|
||||||
|
=> { foo = "x_foo"; bar = "x_bar"; }
|
||||||
|
```
|
||||||
|
|
||||||
|
:::
|
||||||
|
*/
|
||||||
|
genAttrs = names: f: genAttrs' names (n: nameValuePair n (f n));
|
||||||
|
|
||||||
|
/**
|
||||||
|
Like `genAttrs`, but allows the name of each attribute to be specified in addition to the value.
|
||||||
|
The applied function should return both the new name and value as a `nameValuePair`.
|
||||||
|
::: {.warning}
|
||||||
|
In case of attribute name collision the first entry determines the value,
|
||||||
|
all subsequent conflicting entries for the same name are silently ignored.
|
||||||
|
:::
|
||||||
|
|
||||||
|
# Inputs
|
||||||
|
|
||||||
|
`xs`
|
||||||
|
|
||||||
|
: A list of strings `s` used as generator.
|
||||||
|
|
||||||
|
`f`
|
||||||
|
|
||||||
|
: A function, given a string `s` from the list `xs`, returns a new `nameValuePair`.
|
||||||
|
|
||||||
|
# Type
|
||||||
|
|
||||||
|
```
|
||||||
|
genAttrs' :: [ Any ] -> (Any -> { name :: String; value :: Any; }) -> AttrSet
|
||||||
|
```
|
||||||
|
|
||||||
|
# Examples
|
||||||
|
:::{.example}
|
||||||
|
## `lib.attrsets.genAttrs'` usage example
|
||||||
|
|
||||||
|
```nix
|
||||||
|
genAttrs' [ "foo" "bar" ] (s: nameValuePair ("x_" + s) ("y_" + s))
|
||||||
|
=> { x_foo = "y_foo"; x_bar = "y_bar"; }
|
||||||
|
```
|
||||||
|
|
||||||
|
:::
|
||||||
|
*/
|
||||||
|
genAttrs' = xs: f: builtins.listToAttrs (map f xs);
|
||||||
|
}
|
||||||
4
nib/std/default.nix
Normal file
4
nib/std/default.nix
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
builtins.listToAttrs [
|
||||||
|
(import ./attrs.nix)
|
||||||
|
(import ./lists.nix)
|
||||||
|
]
|
||||||
11
nib/std/lists.nix
Normal file
11
nib/std/lists.nix
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
rec {
|
||||||
|
foldl = op: nul: list: let
|
||||||
|
foldl' = n:
|
||||||
|
if n == -1
|
||||||
|
then nul
|
||||||
|
else op (foldl' (n - 1)) (builtins.elemAt list n);
|
||||||
|
in
|
||||||
|
foldl' (builtins.length list - 1);
|
||||||
|
|
||||||
|
crossLists = f: foldl (fs: args: builtins.concatMap (f: map f args) fs) [f];
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue