Created
April 5, 2018 20:55
-
-
Save Profpatsch/6aed874ab7c56153eb10da8bb00c2467 to your computer and use it in GitHub Desktop.
Next iteration of bananas & barbed wire
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with import <nixpkgs> {}; | |
let | |
Recursive = { fmap, project }: { | |
cata = alg: | |
let c = val: alg (fmap c (project val)); | |
in c; | |
}; | |
matchDef = def: matcher: v: | |
let case = builtins.head (builtins.attrNames v); | |
in (matcher.${case} or (def case)) v.${case}; | |
matchDefMap = def: matcher: v: | |
let case = builtins.head (builtins.attrNames v); | |
in { ${case} = (matcher.${case} or def) v.${case}; }; | |
nixRec = Recursive { | |
project = v: { "${builtins.typeOf v}" = v; }; | |
fmap = f: matchDefMap lib.id { | |
"set" = lib.mapAttrs (lib.const f); | |
"list" = map f; | |
}; | |
}; | |
in with lib; | |
nixRec.cata (matchDef | |
(n: _: "(${n}?)!") | |
{ | |
int = const "INT"; | |
set = a: lib.concatStringsSep ", " (mapAttrsToList (const id) a); | |
list = concatStringsSep ", "; | |
}) | |
{ a = "bla"; b = [ 4 5 "foo" lib.id ]; } | |
# nix-instantiate ./test.nix --eval --strict --show-trace | |
# "(string?)!, INT, INT, (string?)!, (lambda?)!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment