Commit a1631990 authored by Yueh-Shun Li's avatar Yueh-Shun Li
Browse files

lib.mirrorFunctionArgs: init

parent 32ea236e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -74,7 +74,7 @@ let
      importJSON importTOML warn warnIf warnIfNot throwIf throwIfNot checkListOfEnum
      info showWarnings nixpkgsVersion version isInOldestRelease
      mod compare splitByAndCompare
      functionArgs setFunctionArgs isFunction toFunction
      functionArgs setFunctionArgs isFunction toFunction mirrorFunctionArgs
      toHexString toBaseDigits inPureEvalMode;
    inherit (self.fixedPoints) fix fix' converge extends composeExtensions
      composeManyExtensions makeExtensible makeExtensibleWithCustomName;
+34 −0
Original line number Diff line number Diff line
@@ -448,6 +448,40 @@ rec {
  isFunction = f: builtins.isFunction f ||
    (f ? __functor && isFunction (f.__functor f));

  /*
    `mirrorFunctionArgs f g` creates a new function `g'` with the same behavior as `g` (`g' x == g x`)
    but its function arguments mirroring `f` (`lib.functionArgs g' == lib.functionArgs f`).

    Type:
      mirrorFunctionArgs :: (a -> b) -> (a -> c) -> (a -> c)

    Example:
      addab = {a, b}: a + b
      addab { a = 2; b = 4; }
      => 6
      lib.functionArgs addab
      => { a = false; b = false; }
      addab1 = attrs: addab attrs + 1
      addab1 { a = 2; b = 4; }
      => 7
      lib.functionArgs addab1
      => { }
      addab1' = lib.mirrorFunctionArgs addab addab1
      addab1' { a = 2; b = 4; }
      => 7
      lib.functionArgs addab1'
      => { a = false; b = false; }
  */
  mirrorFunctionArgs =
    # Function to provide the argument metadata
    f:
    let
      fArgs = functionArgs f;
    in
    # Function to set the argument metadata to
    g:
    setFunctionArgs g fArgs;

  /*
    Turns any non-callable values into constant functions.
    Returns callable values as is.