diff options
author | piegames <git@piegames.de> | 2024-07-11 10:49:15 +0200 |
---|---|---|
committer | piegames <git@piegames.de> | 2024-08-08 11:13:53 +0200 |
commit | 28ae24f3f738a50903bd9b9bf8eece84a1075c30 (patch) | |
tree | dae4696680c5acd3ada9f170976c6b7581e9be82 /doc | |
parent | 6fdb47f0b259a9ffbe4bdb349d6aaf03a0144092 (diff) |
libexpr: Add experimental pipe operator
The |> operator is a reverse function operator with low binding strength
to replace lib.pipe. Implements RFC 148, see the RFC text for more
details. Closes #438.
Change-Id: I21df66e8014e0d4dd9753dd038560a2b0b7fd805
Diffstat (limited to 'doc')
-rw-r--r-- | doc/manual/change-authors.yml | 5 | ||||
-rw-r--r-- | doc/manual/rl-next/pipe-operator.md | 10 | ||||
-rw-r--r-- | doc/manual/src/language/operators.md | 32 |
3 files changed, 47 insertions, 0 deletions
diff --git a/doc/manual/change-authors.yml b/doc/manual/change-authors.yml index 630af29ff..e18abada1 100644 --- a/doc/manual/change-authors.yml +++ b/doc/manual/change-authors.yml @@ -103,6 +103,11 @@ midnightveil: ncfavier: github: ncfavier +piegames: + display_name: piegames + forgejo: piegames + github: piegamesde + puck: display_name: puck forgejo: puck diff --git a/doc/manual/rl-next/pipe-operator.md b/doc/manual/rl-next/pipe-operator.md new file mode 100644 index 000000000..49dc01308 --- /dev/null +++ b/doc/manual/rl-next/pipe-operator.md @@ -0,0 +1,10 @@ +--- +synopsis: Pipe operator `|>` (experimental) +issues: [fj#438] +cls: [1654] +category: Features +credits: [piegames, horrors] +--- + +Implementation of the pipe operator (`|>`) in the language as described in [RFC 148](https://github.com/NixOS/rfcs/pull/148). +The feature is still marked experimental, enable `--extra-experimental-features pipe-operator` to use it. diff --git a/doc/manual/src/language/operators.md b/doc/manual/src/language/operators.md index 6dcdc6eb0..2d4707814 100644 --- a/doc/manual/src/language/operators.md +++ b/doc/manual/src/language/operators.md @@ -26,6 +26,8 @@ | Logical conjunction (`AND`) | *bool* `&&` *bool* | left | 12 | | Logical disjunction (`OR`) | *bool* <code>\|\|</code> *bool* | left | 13 | | [Logical implication] | *bool* `->` *bool* | none | 14 | +| \[Experimental\] [Function piping] | *expr* |> *func* | left | 15 | +| \[Experimental\] [Function piping] | *expr* <| *func* | right | 16 | [string]: ./values.md#type-string [path]: ./values.md#type-path @@ -215,3 +217,33 @@ nix-repl> let f = x: 1; s = { func = f; }; in [ (f == f) (s == s) ] Equivalent to `!`*b1* `||` *b2*. [Logical implication]: #logical-implication + +## \[Experimental\] Function piping + +*This language feature is still experimental and may change at any time. Enable `--extra-experimental-features pipe-operator` to use it.* + +Pipes are a dedicated operator for function application, but with reverse order and a lower binding strength. +This allows you to chain function calls together in way that is more natural to read and requires less parentheses. + +`a |> f b |> g` is equivalent to `g (f b a)`. +`g <| f b <| a` is equivalent to `g (f b a)`. + +Example code snippet: + +```nix +defaultPrefsFile = defaultPrefs + |> lib.mapAttrsToList ( + key: value: '' + // ${value.reason} + pref("${key}", ${builtins.toJSON value.value}); + '' + ) + |> lib.concatStringsSep "\n" + |> pkgs.writeText "nixos-default-prefs.js"; +``` + +Note how `mapAttrsToList` is called with two arguments (the lambda and `defaultPrefs`), +but moving the last argument in front of the rest improves the reading flow. +This is common for functions with long first argument, including all `map`-like functions. + +[Function piping]: #experimental-function-piping |