mkdir nib/std && replace --all 'with' 'let ... in'
This commit is contained in:
parent
f9bb6ad937
commit
fd008cd2e1
11 changed files with 108 additions and 104 deletions
|
|
@ -1,55 +1,51 @@
|
||||||
{systems, ...}: let
|
{systems, ...}: let
|
||||||
mergeAttrsList = types.mergeAttrsList;
|
|
||||||
|
|
||||||
submodArgs = {inherit nib;};
|
|
||||||
|
|
||||||
# TODO: move this to a new module
|
# TODO: move this to a new module
|
||||||
mkMod' = args: mod: import mod args;
|
mkMod' = args: mod: import mod args;
|
||||||
mkMod = mkMod' submodArgs;
|
mkMod = mkMod' {inherit nib;};
|
||||||
|
|
||||||
parse = mkMod ./parse;
|
std = mkMod ./std;
|
||||||
types = mkMod ./types;
|
types = mkMod ./types;
|
||||||
|
parse = mkMod ./parse;
|
||||||
|
|
||||||
nib = with types;
|
nib = std.mergeAttrsList [
|
||||||
mergeAttrsList [
|
# submodule content is accessible first by submodule name
|
||||||
# submodule content is accessible first by submodule name
|
# then by the name of the content (ie self.submodule.myFunc)
|
||||||
# then by the name of the content (ie self.submodule.myFunc)
|
{inherit std types parse;}
|
||||||
{inherit parse types;}
|
|
||||||
|
|
||||||
# submodule is included directly to this module (ie self.myFunc)
|
# submodule is included directly to this module (ie self.myFunc)
|
||||||
|
|
||||||
# this module
|
# this module
|
||||||
{
|
{
|
||||||
# === External Functions ===
|
# === External Functions ===
|
||||||
withPkgs = repo: config: system:
|
withPkgs = repo: config: system:
|
||||||
import repo {
|
import repo {
|
||||||
inherit system;
|
inherit system;
|
||||||
}
|
}
|
||||||
// config;
|
// config;
|
||||||
|
|
||||||
mkSys = input: let
|
mkSys = input: let
|
||||||
# function taking a system as argument
|
# function taking a system as argument
|
||||||
pkgsFor = input.pkgs;
|
pkgsFor = input.pkgs;
|
||||||
in {
|
in {
|
||||||
inherit pkgsFor;
|
inherit pkgsFor;
|
||||||
forAllSystems = f:
|
forAllSystems = f:
|
||||||
genAttrs systems (
|
std.genAttrs systems (
|
||||||
system: f system (pkgsFor system)
|
system: f system (pkgsFor system)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
mkUSys = input: let
|
mkUSys = input: let
|
||||||
# functions taking a system as argument
|
# functions taking a system as argument
|
||||||
pkgsFor = input.pkgs;
|
pkgsFor = input.pkgs;
|
||||||
upkgsFor = input.upkgs;
|
upkgsFor = input.upkgs;
|
||||||
in {
|
in {
|
||||||
inherit pkgsFor upkgsFor;
|
inherit pkgsFor upkgsFor;
|
||||||
forAllSystems = f:
|
forAllSystems = f:
|
||||||
genAttrs systems (
|
std.genAttrs systems (
|
||||||
system: f system (pkgsFor system) (upkgsFor system)
|
system: f system (pkgsFor system) (upkgsFor system)
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
in
|
in
|
||||||
nib
|
nib
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,7 @@
|
||||||
{nib, ...} @ args: let
|
{nib, ...} @ args: let
|
||||||
struct = import ./struct.nix args;
|
struct = import ./struct.nix args;
|
||||||
in
|
in
|
||||||
with nib.types;
|
nib.std.mergeAttrsList [
|
||||||
mergeAttrsList [
|
# submodule is included directly to this module (ie self.myFunc)
|
||||||
# submodule is included directly to this module (ie self.myFunc)
|
struct
|
||||||
struct
|
]
|
||||||
|
|
||||||
# submodule content is accessible first by submodule name
|
|
||||||
# then by the name of the content (ie self.submodule.myFunc)
|
|
||||||
]
|
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,21 @@
|
||||||
{nib, ...}:
|
{nib, ...}: let
|
||||||
with builtins;
|
Err = nib.types.Err;
|
||||||
with nib.types; rec {
|
Ok' = nib.types.Ok';
|
||||||
|
firstErr = nib.types.firstErr;
|
||||||
|
|
||||||
|
unwrapSome = nib.types.unwrapSome;
|
||||||
|
|
||||||
|
isTerminal = nib.types.isTerminal;
|
||||||
|
unwrapTerminal = nib.types.unwrapTerminal;
|
||||||
|
|
||||||
|
mapAttrsRecursiveCond = nib.std.mapAttrsRecursiveCond;
|
||||||
|
attrValueAt = nib.std.attrValueAt;
|
||||||
|
in rec {
|
||||||
cmpStructErr' = errBadKeys: errBadValues: path: S: T:
|
cmpStructErr' = errBadKeys: errBadValues: path: S: T:
|
||||||
if isAttrs S && isAttrs T
|
if builtins.isAttrs S && builtins.isAttrs T
|
||||||
then let
|
then let
|
||||||
keysS = attrNames S;
|
keysS = builtins.attrNames S;
|
||||||
keysT = attrNames T;
|
keysT = builtins.attrNames T;
|
||||||
in
|
in
|
||||||
# ensure all key names match, then recurse
|
# ensure all key names match, then recurse
|
||||||
if !(keysS == keysT)
|
if !(keysS == keysT)
|
||||||
|
|
@ -18,7 +28,7 @@ with nib.types; rec {
|
||||||
else
|
else
|
||||||
# terminating leaf in recursion tree reached
|
# terminating leaf in recursion tree reached
|
||||||
# ensure values' types match
|
# ensure values' types match
|
||||||
(typeOf S == typeOf T)
|
(builtins.typeOf S == builtins.typeOf T)
|
||||||
|| errBadValues path S T;
|
|| errBadValues path S T;
|
||||||
|
|
||||||
cmpStructErr = errBadKeys: errBadValues: cmpStructErr' errBadKeys errBadValues [];
|
cmpStructErr = errBadKeys: errBadValues: cmpStructErr' errBadKeys errBadValues [];
|
||||||
|
|
@ -73,14 +83,14 @@ with nib.types; rec {
|
||||||
(value:
|
(value:
|
||||||
if isTerminal value
|
if isTerminal value
|
||||||
then unwrapTerminal value
|
then unwrapTerminal value
|
||||||
else valueS);
|
else value);
|
||||||
|
|
||||||
# TODO: Define:
|
# TODO: Define:
|
||||||
# TODO: throwUnreachable = throw "Unreachable code was evaluated..";
|
# TODO: throwUnreachable = throw "Unreachable code was evaluated..";
|
||||||
# TODO: abortUnreachable = abort "Unreachable code was evaluated...";
|
# TODO: abortUnreachable = abort "Unreachable code was evaluated...";
|
||||||
mergeStruct = mergeStruct' (_: _: Ok');
|
mergeStruct = mergeStructs' (_: _: Ok');
|
||||||
|
|
||||||
# mergeTypedPartialStruct must evaluate properties (not lazy)
|
# mergeTypedPartialStruct must evaluate properties (not lazy)
|
||||||
# for lazy evaluation use mergeStruct instead!
|
# for lazy evaluation use mergeStruct instead!
|
||||||
mergeTypedPartialStruct = mergeStruct' cmpTypedPartialStruct;
|
mergeTypedPartialStruct = mergeStructs' cmpTypedPartialStruct;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
{nib, ...}:
|
{nib, ...}: let
|
||||||
with builtins;
|
foldl = nib.std.foldl;
|
||||||
with nib.types; rec {
|
nullableToMaybe = nib.types.nullableToMany;
|
||||||
|
in rec {
|
||||||
nameValuePair = name: value: {inherit name value;};
|
nameValuePair = name: value: {inherit name value;};
|
||||||
|
|
||||||
identityAttrs = value: {${value} = value;};
|
identityAttrs = value: {${value} = value;};
|
||||||
|
|
||||||
identityAttrsList = values: map (v: identityAttrs v) values;
|
identityAttrsMany = values: map (v: identityAttrs v) values;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Generate an attribute set by mapping a function over a list of
|
Generate an attribute set by mapping a function over a list of
|
||||||
|
|
@ -75,15 +76,15 @@ with nib.types; rec {
|
||||||
|
|
||||||
:::
|
:::
|
||||||
*/
|
*/
|
||||||
genAttrs' = xs: f: listToAttrs (map f xs);
|
genAttrs' = xs: f: builtins.listToAttrs (map f xs);
|
||||||
|
|
||||||
mapAttrsRecursiveCond = cond: f: set: let
|
mapAttrsRecursiveCond = cond: f: set: let
|
||||||
recurse = path:
|
recurse = path:
|
||||||
mapAttrs (
|
builtins.mapAttrs (
|
||||||
name: value: let
|
name: value: let
|
||||||
next = path ++ [name];
|
next = path ++ [name];
|
||||||
in
|
in
|
||||||
if isAttrs value && cond value
|
if builtins.isAttrs value && cond value
|
||||||
then recurse next value
|
then recurse next value
|
||||||
else f next value
|
else f next value
|
||||||
);
|
);
|
||||||
|
|
@ -97,7 +98,7 @@ with nib.types; rec {
|
||||||
# attribute set at that path
|
# attribute set at that path
|
||||||
attrValueAt = let
|
attrValueAt = let
|
||||||
value = foldl (l: r:
|
value = foldl (l: r:
|
||||||
if isAttrs l && hasAttr r l
|
if builtins.isAttrs l && builtins.hasAttr r l
|
||||||
then l.${r}
|
then l.${r}
|
||||||
else null);
|
else null);
|
||||||
in
|
in
|
||||||
|
|
@ -115,11 +116,11 @@ with nib.types; rec {
|
||||||
binaryMerge start (start + (end - start) / 2) // binaryMerge (start + (end - start) / 2) end
|
binaryMerge start (start + (end - start) / 2) // binaryMerge (start + (end - start) / 2) end
|
||||||
else
|
else
|
||||||
# Otherwise there will be exactly 1 element due to the invariant, in which case we just return it directly
|
# Otherwise there will be exactly 1 element due to the invariant, in which case we just return it directly
|
||||||
elemAt list start;
|
builtins.elemAt list start;
|
||||||
in
|
in
|
||||||
if list == []
|
if list == []
|
||||||
then
|
then
|
||||||
# Calling binaryMerge as below would not satisfy its invariant
|
# Calling binaryMerge as below would not satisfy its invariant
|
||||||
{}
|
{}
|
||||||
else binaryMerge 0 (length list);
|
else binaryMerge 0 (builtins.length list);
|
||||||
}
|
}
|
||||||
9
nib/std/default.nix
Normal file
9
nib/std/default.nix
Normal file
|
|
@ -0,0 +1,9 @@
|
||||||
|
{...} @ args: let
|
||||||
|
attrs = import ./attrs.nix args;
|
||||||
|
lists = import ./lists.nix args;
|
||||||
|
in
|
||||||
|
attrs.mergeAttrsList [
|
||||||
|
# submodule is included directly to this module (ie self.myFunc)
|
||||||
|
attrs
|
||||||
|
lists
|
||||||
|
]
|
||||||
|
|
@ -1,14 +1,13 @@
|
||||||
{...}:
|
{...}: rec {
|
||||||
with builtins; rec {
|
|
||||||
foldl = op: nul: list: let
|
foldl = op: nul: list: let
|
||||||
foldl' = n:
|
foldl' = n:
|
||||||
if n == -1
|
if n == -1
|
||||||
then nul
|
then nul
|
||||||
else op (foldl' (n - 1)) (elemAt list n);
|
else op (foldl' (n - 1)) (builtins.elemAt list n);
|
||||||
in
|
in
|
||||||
foldl' (length list - 1);
|
foldl' (builtins.length list - 1);
|
||||||
|
|
||||||
crossLists = f: foldl (fs: args: concatMap (f: map f args) fs) [f];
|
crossLists = f: foldl (fs: args: builtins.concatMap (f: map f args) fs) [f];
|
||||||
|
|
||||||
findFirstIndex = pred: default: list: let
|
findFirstIndex = pred: default: list: let
|
||||||
# A naive recursive implementation would be much simpler, but
|
# A naive recursive implementation would be much simpler, but
|
||||||
|
|
@ -24,7 +23,7 @@ with builtins; rec {
|
||||||
#
|
#
|
||||||
# We start with index -1 and the 0'th element of the list, which satisfies the invariant
|
# We start with index -1 and the 0'th element of the list, which satisfies the invariant
|
||||||
resultIndex =
|
resultIndex =
|
||||||
foldl' (
|
builtins.foldl' (
|
||||||
index: el:
|
index: el:
|
||||||
if index < 0
|
if index < 0
|
||||||
then
|
then
|
||||||
|
|
@ -51,5 +50,5 @@ with builtins; rec {
|
||||||
in
|
in
|
||||||
if index == null
|
if index == null
|
||||||
then default
|
then default
|
||||||
else elemAt list index;
|
else builtins.elemAt list index;
|
||||||
}
|
}
|
||||||
|
|
@ -1,24 +1,25 @@
|
||||||
{nib, ...}:
|
{nib, ...}: let
|
||||||
with builtins;
|
crossLists = nib.std.crossLists;
|
||||||
with nib.types; let
|
identityAttrsMany = nib.std.identityAttrsMany;
|
||||||
|
|
||||||
# === Internal Helper Functions ===
|
# === Internal Helper Functions ===
|
||||||
toSystemName = arch: platform: "${arch}-${platform}";
|
toSystemName = arch: platform: "${arch}-${platform}";
|
||||||
listsToSystemNames = archs: platforms:
|
listsToSystemNames = archs: platforms:
|
||||||
crossLists (arch: platform: toSystemName arch platform)
|
crossLists (arch: platform: toSystemName arch platform)
|
||||||
[
|
[
|
||||||
(attrValues archs)
|
(builtins.attrValues archs)
|
||||||
(attrValues platforms)
|
(builtins.attrValues platforms)
|
||||||
];
|
];
|
||||||
in rec {
|
in rec {
|
||||||
# REF: https://github.com/nix-systems/nix-systems
|
# REF: https://github.com/nix-systems/nix-systems
|
||||||
archs = identityAttrsList [
|
archs = identityAttrsMany [
|
||||||
"x86_64"
|
"x86_64"
|
||||||
"aarch64"
|
"aarch64"
|
||||||
"riscv64"
|
"riscv64"
|
||||||
];
|
];
|
||||||
|
|
||||||
# REF: https://github.com/nix-systems/nix-systems
|
# REF: https://github.com/nix-systems/nix-systems
|
||||||
platforms = identityAttrsList [
|
platforms = identityAttrsMany [
|
||||||
"linux"
|
"linux"
|
||||||
"darwin"
|
"darwin"
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -1,18 +1,11 @@
|
||||||
{nib, ...} @ args: let
|
{nib, ...} @ args: let
|
||||||
attrs = import ./attrs.nix args;
|
|
||||||
fault = import ./fault.nix args;
|
fault = import ./fault.nix args;
|
||||||
lists = import ./lists.nix args;
|
|
||||||
maybe = import ./maybe.nix args;
|
maybe = import ./maybe.nix args;
|
||||||
res = import ./res.nix args;
|
res = import ./res.nix args;
|
||||||
in
|
in
|
||||||
attrs.mergeAttrsList [
|
nib.std.mergeAttrsList [
|
||||||
# submodule is included directly to this module (ie self.myFunc)
|
# submodule is included directly to this module (ie self.myFunc)
|
||||||
attrs
|
|
||||||
fault
|
fault
|
||||||
lists
|
|
||||||
maybe
|
maybe
|
||||||
res
|
res
|
||||||
|
|
||||||
# submodule content is accessible first by submodule name
|
|
||||||
# then by the name of the content (ie self.submodule.myFunc)
|
|
||||||
]
|
]
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
{...}:
|
{...}: rec {
|
||||||
with builtins; rec {
|
|
||||||
# Fault Monad
|
# Fault Monad
|
||||||
# Wrapper around an error (ie builtins.abort)
|
# Wrapper around an error (ie builtins.abort)
|
||||||
Fault = error: {
|
Fault = error: {
|
||||||
|
|
@ -7,7 +6,7 @@ with builtins; rec {
|
||||||
};
|
};
|
||||||
|
|
||||||
# Pattern Matching
|
# Pattern Matching
|
||||||
isFault = F: attrNames F == ["_error_"];
|
isFault = F: builtins.attrNames F == ["_error_"];
|
||||||
|
|
||||||
# Unwrap (Monadic Return Operation)
|
# Unwrap (Monadic Return Operation)
|
||||||
unwrapFault = F: F._error_;
|
unwrapFault = F: F._error_;
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{nib, ...}:
|
{nib, ...}: let
|
||||||
with builtins;
|
Res = nib.types.Res;
|
||||||
with nib.types; let
|
findFirst = nib.std.findFirst;
|
||||||
# TODO: try get enum generation working (and other type constructors)
|
# TODO: try get enum generation working (and other type constructors)
|
||||||
# Maybe = mkEnum "nib::Maybe" {
|
# Maybe = mkEnum "nib::Maybe" {
|
||||||
# Some = mkEnumVariant {value = "nix::String";};
|
# Some = mkEnumVariant {value = "nix::String";};
|
||||||
|
|
@ -66,7 +66,7 @@ in rec {
|
||||||
None = Maybe false null;
|
None = Maybe false null;
|
||||||
|
|
||||||
# Pattern Matching
|
# Pattern Matching
|
||||||
isMaybe = T: attrNames T == ["_some_" "_value_"];
|
isMaybe = T: builtins.attrNames T == ["_some_" "_value_"];
|
||||||
isSome = T: isMaybe T && T._some_;
|
isSome = T: isMaybe T && T._some_;
|
||||||
isNone = T: isMaybe T && !T._some_;
|
isNone = T: isMaybe T && !T._some_;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
{nib, ...}:
|
{nib, ...}: let
|
||||||
with builtins;
|
findFirst = nib.std.findFirst;
|
||||||
with nib.types; rec {
|
in rec {
|
||||||
# Res (Result) Monad
|
# Res (Result) Monad
|
||||||
Res = success: value: {
|
Res = success: value: {
|
||||||
_success_ = success;
|
_success_ = success;
|
||||||
|
|
@ -12,7 +12,7 @@ with nib.types; rec {
|
||||||
Err' = Err "err";
|
Err' = Err "err";
|
||||||
|
|
||||||
# Pattern Matching
|
# Pattern Matching
|
||||||
isRes = R: attrNames R == ["_success_" "_value_"];
|
isRes = R: builtins.attrNames R == ["_success_" "_value_"];
|
||||||
isOk = R: isRes R && R._success_;
|
isOk = R: isRes R && R._success_;
|
||||||
isErr = R: isRes R && !R._success_;
|
isErr = R: isRes R && !R._success_;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue