some calculi

nix-refac
pegasust 2022-12-16 13:57:32 +00:00
parent b8f640a051
commit 2e341a2409
8 changed files with 137 additions and 0 deletions

29
nix/calculus/default.nix Normal file
View File

@ -0,0 +1,29 @@
{ pkgs ? import <nixpkgs> { }
, lib ? inputs.pkgs.lib
, ...
}@inputs:
builtins // rec {
mod = base: int: base - (int * (builtins.div base int));
map_ = list: fn: (builtins.map fn list);
mapAttrs_ = attrset: fn: (builtins.mapAttrs fn attrset);
# :: [T] -> (T -> null | V) -> [V]
# Filters if return null, otherwise, remap to V
filterMap_ = list: fn: (builtins.filter (e: e != null) (builtins.map fn list));
filterMap = fn: list: (filterMap_ list fn);
filter_ = list: fn: (builtins.filter fn list);
# :: [T] -> (T -> V) -> {T[int] = V;}
list2Attrs_ = list: fn_k_v (builtins.foldl' (acc: k: acc // { k = (fn_k_v k); }) { } list);
list2Attrs = fn_k_v: list (list2Attrs_ list fn_k_v);
# range :: int -> int -> [int]
rangeIn = lib.range;
rangeEx = start: stop: (lib.range start stop-1);
# [T] -> int
len = builtins.length;
zip = lib.zipLists;
# [T] -> [{idx: int, val: T}]
enumerate = list: lib.zipListsWith (idx: val: { inherit idx val; }) (rangeEx 0 (len list)) list;
}

63
nix/calculus/std.nix Normal file
View File

@ -0,0 +1,63 @@
let
pkgs = import <nixpkgs> { };
inherit (pkgs) lib;
inherit (lib) runTests;
c_ = import ./default.nix;
bulkExprTest = { name, fn, list_case, ... }@test: (c_.foldl'
(acc: { name, args, expected, ... }@case: acc // {
"test_${test.name}_${case.name}" = {
expr = c_.foldl' (f: x: f x) fn args;
inherit expected;
};
})
{ }
list_case);
tests = (
bulkExprTest {
name = "filterMap_";
fn = c_.filterMap_;
list_case = [
{
name = "odd";
args = [ [ 1 2 3 4 5 6 ] (e: if (c_.mod e 2) == 0 then null else e) ];
expected = [ 1 3 5 ];
}
{
name = "even";
args = [ [ 1 2 3 4 5 6 ] (e: if (c_.mod e 2) == 1 then null else e) ];
expected = [ 2 4 6 ];
}
];
} // bulkExprTest {
name = "filterMap";
fn = c_.filterMap;
list_case = [
{
name = "odd";
args = [ (e: if (c_.mod e 2) == 0 then null else e) [ 1 2 3 4 5 6 ] ];
expected = [ 1 3 5 ];
}
{
name = "even";
args = [ (e: if (c_.mod e 2) == 1 then null else e) [ 1 2 3 4 5 6 ] ];
expected = [ 2 4 6 ];
}
];
} // bulkExprTest {
name = "list2Attrs_";
fn = c_.list2Attrs_;
list_case = [
{
name = "attach";
args = [["123" "abc" "1414"]];
}
];
}
);
in
{
inherit pkgs lib runTests c_;
_tests = tests;
tests = runTests tests;
}

38
nix/flake.nix Normal file
View File

@ -0,0 +1,38 @@
{
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
home-manager = {
url = "github:nix-community/home-manager";
inputs.nixpkgs.follows = "nixpkgs";
};
flake-utils-plus.url = "github:gytis-ivaskevicius/flake-utils-plus";
# for OpenGL support on Nix
nixgl.url = "github:guibou/nixGL";
# S-tier Rust overlay for faster nightly updates
rust-overlay.url = "github:oxalica/rust-overlay";
# Allows default.nix to call onto flake.nix. Useful for nix eval and automations
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
naersk.url = "github:nix-community/naersk";
};
outputs =
{ nixpkgs
, home-manager
, flake-utils-plus
, nixgl
, rust-overlay
# , flake-compat # This is only a placeholder for version control by flake.lock
, naersk
, ...
}:
let
# fundamental functions that should only take 2 keystrokes instead of builtins (8)
c_ = import ./calculus;
overlays = [ rust-overlay.overlays.default nixgl.overlay ];
in c_
{ };
}

0
nix/hosts/default.nix Normal file
View File

0
nix/lib/default.nix Normal file
View File

0
nix/pkgs/default.nix Normal file
View File

7
nix/types/default.nix Normal file
View File

@ -0,0 +1,7 @@
{ pkgs ? import <nixpkgs> { }
, lib ? inputs.pkgs.lib
, ...
}@inputs:
{
}

0
nix/users/default.nix Normal file
View File