git.iko.soy/typeable/nixpkgs
on master help sign in
lib/trivial: move zipIntBits to its own file

The amount of implementation detail really should not be the first thing in a prominent file called `trivial.nix`.

commit62dca7c9ab08ee5cc2043d6d374013b8041a3f21 authorProfpatsch <mail@profpatsch.de> dateThu 26 Jul 2018, 00:10 (8 years ago) parentaf10842
2 files changed+49−40
lib/trivial.nix+10−40 lib/zip-int-bits.nix+39
lib/trivial.nix+10−40
@@ -1,41 +1,5 @@
11{ lib }:
2−let
3− zipIntBits = f: x: y:
4− let
5− # (intToBits 6) -> [ 0 1 1 ]
6− intToBits = x:
7− if x == 0 || x == -1 then
8− []
9− else
10− let
11− headbit = if (x / 2) * 2 != x then 1 else 0; # x & 1
12− tailbits = if x < 0 then ((x + 1) / 2) - 1 else x / 2; # x >> 1
13− in
14− [headbit] ++ (intToBits tailbits);
15−
16− # (bitsToInt [ 0 1 1 ] 0) -> 6
17− # (bitsToInt [ 0 1 0 ] 1) -> -6
18− bitsToInt = l: signum:
19− if l == [] then
20− (if signum == 0 then 0 else -1)
21− else
22− (builtins.head l) + (2 * (bitsToInt (builtins.tail l) signum));
23−
24− xsignum = if x < 0 then 1 else 0;
25− ysignum = if y < 0 then 1 else 0;
26− zipListsWith' = fst: snd:
27− if fst==[] && snd==[] then
28− []
29− else if fst==[] then
30− [(f xsignum (builtins.head snd))] ++ (zipListsWith' [] (builtins.tail snd))
31− else if snd==[] then
32− [(f (builtins.head fst) ysignum )] ++ (zipListsWith' (builtins.tail fst) [] )
33− else
34− [(f (builtins.head fst) (builtins.head snd))] ++ (zipListsWith' (builtins.tail fst) (builtins.tail snd));
35− in
36− assert (builtins.isInt x) && (builtins.isInt y);
37− bitsToInt (zipListsWith' (intToBits x) (intToBits y)) (f xsignum ysignum);
38−in
2+
393rec {
404
415 ## Simple (higher order) functions
@@ -71,13 +35,19 @@ rec {
7135 and = x: y: x && y;
7236
7337 /* bitwise “and” */
74− bitAnd = builtins.bitAnd or zipIntBits (a: b: if a==1 && b==1 then 1 else 0);
38+ bitAnd = builtins.bitAnd
39+ or import ./zip-int-bits.nix
40+ (a: b: if a==1 && b==1 then 1 else 0);
7541
7642 /* bitwise “or” */
77− bitOr = builtins.bitOr or zipIntBits (a: b: if a==1 || b==1 then 1 else 0);
43+ bitOr = builtins.bitOr
44+ or import ./zip-int-bits.nix
45+ (a: b: if a==1 || b==1 then 1 else 0);
7846
7947 /* bitwise “xor” */
80− bitXor = builtins.bitXor or zipIntBits (a: b: if a!=b then 1 else 0);
48+ bitXor = builtins.bitXor
49+ or import ./zip-int-bits.nix
50+ (a: b: if a!=b then 1 else 0);
8151
8252 /* bitwise “not” */
8353 bitNot = builtins.sub (-1);
lib/zip-int-bits.nixnew file+39
@@ -0,0 +1,39 @@
1+/* Helper function to implement a fallback for the bit operators
2+ `bitAnd`, `bitOr` and `bitXOr` on older nix version.
3+ See ./trivial.nix
4+*/
5+f: x: y:
6+ let
7+ # (intToBits 6) -> [ 0 1 1 ]
8+ intToBits = x:
9+ if x == 0 || x == -1 then
10+ []
11+ else
12+ let
13+ headbit = if (x / 2) * 2 != x then 1 else 0; # x & 1
14+ tailbits = if x < 0 then ((x + 1) / 2) - 1 else x / 2; # x >> 1
15+ in
16+ [headbit] ++ (intToBits tailbits);
17+
18+ # (bitsToInt [ 0 1 1 ] 0) -> 6
19+ # (bitsToInt [ 0 1 0 ] 1) -> -6
20+ bitsToInt = l: signum:
21+ if l == [] then
22+ (if signum == 0 then 0 else -1)
23+ else
24+ (builtins.head l) + (2 * (bitsToInt (builtins.tail l) signum));
25+
26+ xsignum = if x < 0 then 1 else 0;
27+ ysignum = if y < 0 then 1 else 0;
28+ zipListsWith' = fst: snd:
29+ if fst==[] && snd==[] then
30+ []
31+ else if fst==[] then
32+ [(f xsignum (builtins.head snd))] ++ (zipListsWith' [] (builtins.tail snd))
33+ else if snd==[] then
34+ [(f (builtins.head fst) ysignum )] ++ (zipListsWith' (builtins.tail fst) [] )
35+ else
36+ [(f (builtins.head fst) (builtins.head snd))] ++ (zipListsWith' (builtins.tail fst) (builtins.tail snd));
37+ in
38+ assert (builtins.isInt x) && (builtins.isInt y);
39+ bitsToInt (zipListsWith' (intToBits x) (intToBits y)) (f xsignum ysignum)