Unverified Commit a448a215 authored by Silvan Mosberger's avatar Silvan Mosberger Committed by GitHub
Browse files

Merge pull request #301556 from edef1c/lib-xor

lib: add xor
parents 53e51b19 725bb4e4
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -69,7 +69,7 @@ let
      hasAttr head isAttrs isBool isInt isList isPath isString length
      lessThan listToAttrs pathExists readFile replaceStrings seq
      stringLength sub substring tail trace;
    inherit (self.trivial) id const pipe concat or and bitAnd bitOr bitXor
    inherit (self.trivial) id const pipe concat or and xor bitAnd bitOr bitXor
      bitNot boolToString mergeAttrs flip mapNullable inNixShell isFloat min max
      importJSON importTOML warn warnIf warnIfNot throwIf throwIfNot checkListOfEnum
      info showWarnings nixpkgsVersion version isInOldestRelease
+16 −0
Original line number Diff line number Diff line
@@ -106,6 +106,7 @@ let
    types
    updateManyAttrsByPath
    versions
    xor
    ;

  testingThrow = expr: {
@@ -214,6 +215,21 @@ runTests {
    expected = false;
  };

  testXor = {
    expr = [
      (xor true false)
      (xor true true)
      (xor false false)
      (xor false true)
    ];
    expected = [
      true
      false
      false
      true
    ];
  };

  testFix = {
    expr = fix (x: {a = if x ? a then "a" else "b";});
    expected = {a = "a";};
+18 −0
Original line number Diff line number Diff line
@@ -199,6 +199,24 @@ in {
  */
  and = x: y: x && y;

  /**
    boolean “exclusive or”


    # Inputs

    `x`

    : 1\. Function argument

    `y`

    : 2\. Function argument
  */
  # We explicitly invert the arguments purely as a type assertion.
  # This is invariant under XOR, so it does not affect the result.
  xor = x: y: (!x) != (!y);

  /**
    bitwise “not”
  */
+0 −1
Original line number Diff line number Diff line
@@ -871,7 +871,6 @@ in {
          }
          {
            assertion = let
              xor = a: b: a && !b || b && !a;
              isEffectivelySystemUser = user.isSystemUser || (user.uid != null && user.uid < 1000);
            in xor isEffectivelySystemUser user.isNormalUser;
            message = ''
+0 −1
Original line number Diff line number Diff line
@@ -9,7 +9,6 @@ with lib;
let
  cfg = config.services.kea;

  xor = x: y: (!x && y) || (x && !y);
  format = pkgs.formats.json {};

  chooseNotNull = x: y: if x != null then x else y;
Loading