Skip to content

Commit

Permalink
format: lib/fixed-points
Browse files Browse the repository at this point in the history
  • Loading branch information
hsjobeki committed Aug 15, 2024
1 parent b24de40 commit dea395b
Showing 1 changed file with 33 additions and 24 deletions.
57 changes: 33 additions & 24 deletions lib/fixed-points.nix
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ rec {
See [`extends`](#function-library-lib.fixedPoints.extends) for an example use case.
There `self` is also often called `final`.
# Inputs
`f`
Expand All @@ -90,7 +89,12 @@ rec {
:::
*/
fix = f: let x = f x; in x;
fix =
f:
let
x = f x;
in
x;

/**
A variant of `fix` that records the original recursive attribute set in the
Expand All @@ -99,14 +103,20 @@ rec {
This is useful in combination with the `extends` function to
implement deep overriding.
# Inputs
`f`
: 1\. Function argument
*/
fix' = f: let x = f x // { __unfix__ = f; }; in x;
fix' =
f:
let
x = f x // {
__unfix__ = f;
};
in
x;

/**
Return the fixpoint that `f` converges to when called iteratively, starting
Expand All @@ -117,7 +127,6 @@ rec {
0
```
# Inputs
`f`
Expand All @@ -134,13 +143,12 @@ rec {
(a -> a) -> a -> a
```
*/
converge = f: x:
converge =
f: x:
let
x' = f x;
in
if x' == x
then x
else converge f x';
if x' == x then x else converge f x';

/**
Extend a function using an overlay.
Expand All @@ -149,7 +157,6 @@ rec {
A fixed-point function is a function which is intended to be evaluated by passing the result of itself as the argument.
This is possible due to Nix's lazy evaluation.
A fixed-point function returning an attribute set has the form
```nix
Expand Down Expand Up @@ -257,7 +264,6 @@ rec {
```
:::
# Inputs
`overlay`
Expand Down Expand Up @@ -299,8 +305,7 @@ rec {
:::
*/
extends =
overlay:
f:
overlay: f:
# The result should be thought of as a function, the argument of that function is not an argument to `extends` itself
(
final:
Expand All @@ -316,9 +321,11 @@ rec {
*/
composeExtensions =
f: g: final: prev:
let fApplied = f final prev;
prev' = prev // fApplied;
in fApplied // g final prev';
let
fApplied = f final prev;
prev' = prev // fApplied;
in
fApplied // g final prev';

/**
Composes a list of [`overlays`](#chap-overlays) and returns a single overlay function that combines them.
Expand All @@ -342,7 +349,6 @@ rec {
- `final` is the result of the fixed-point function, with all overlays applied.
- `prev` is the result of the previous overlay function.
# Type
```
Expand Down Expand Up @@ -385,8 +391,7 @@ rec {
```
:::
*/
composeManyExtensions =
lib.foldr (x: y: composeExtensions x y) (final: prev: {});
composeManyExtensions = lib.foldr (x: y: composeExtensions x y) (final: prev: { });

/**
Create an overridable, recursive attribute set. For example:
Expand Down Expand Up @@ -414,7 +419,6 @@ rec {
Same as `makeExtensible` but the name of the extending attribute is
customized.
# Inputs
`extenderName`
Expand All @@ -425,8 +429,13 @@ rec {
: 2\. Function argument
*/
makeExtensibleWithCustomName = extenderName: rattrs:
fix' (self: (rattrs self) // {
${extenderName} = f: makeExtensibleWithCustomName extenderName (extends f rattrs);
});
makeExtensibleWithCustomName =
extenderName: rattrs:
fix' (
self:
(rattrs self)
// {
${extenderName} = f: makeExtensibleWithCustomName extenderName (extends f rattrs);
}
);
}

0 comments on commit dea395b

Please sign in to comment.