Nix and composition - Nix

Welcome to the Functional Programming Zulip Chat Archive. You can join the chat here.

Magnus Therning

Is there some nice way of composing functions, like the dot operator in Haskell, so I can avoid things like

modifier = drv: foo (bar (baz drv))
Jack Henahan

There's pipe in lib/trivial.nix, though I've never actually used it

Jack Henahan

https://github.com/NixOS/nixpkgs/blob/8252861507ef85b45f739c63f27d4e9a80b31b31/lib/trivial.nix#L32

Nix Packages collection. Contribute to NixOS/nixpkgs development by creating an account on GitHub.
Jack Henahan

You could also just inline something like

compose = f: g: x: f (g x);
composeAll = builtins.foldl' compose id;
Magnus Therning

I can't even figure out how to pull in trivial.pipe..

Jack Henahan

@Magnus Therning Assuming you have pkgs or something in scope, I think you'd say pkgs.lib.pipe

Ben Kolera

It's pretty new. You may not have it in your nixpkgs: https://github.com/NixOS/nixpkgs/commit/8252861507ef85b45f739c63f27d4e9a80b31b31

`pipe` is a useful operator for creating pipelines of functions. It works around the usual problem of e.g. string operations becoming deeply nested functions. In principle, there are four differe...
Magnus Therning

I managed to find it under pkgs.lib.trivial.