Skip to content

Instantly share code, notes, and snippets.

@Profpatsch
Created April 5, 2018 20:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Profpatsch/6aed874ab7c56153eb10da8bb00c2467 to your computer and use it in GitHub Desktop.
Save Profpatsch/6aed874ab7c56153eb10da8bb00c2467 to your computer and use it in GitHub Desktop.
Next iteration of bananas & barbed wire
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