mirror of
https://github.com/NixOS/nix
synced 2025-07-09 03:43:54 +02:00
* Builtin function `add' to add integers.
* Put common test functions in tests/lang/lib.nix.
This commit is contained in:
parent
d315210612
commit
2ab4bc44c7
11 changed files with 68 additions and 26 deletions
1
tests/lang/eval-okay-arithmetic.exp
Normal file
1
tests/lang/eval-okay-arithmetic.exp
Normal file
|
@ -0,0 +1 @@
|
|||
Int(1275)
|
18
tests/lang/eval-okay-arithmetic.nix
Normal file
18
tests/lang/eval-okay-arithmetic.nix
Normal file
|
@ -0,0 +1,18 @@
|
|||
with import ./lib.nix;
|
||||
|
||||
let {
|
||||
|
||||
range = first: last: [first] ++ (if first == last then [] else range (builtins.add first 1) last);
|
||||
|
||||
/* Supposedly tail recursive version:
|
||||
|
||||
range_ = accum: first: last:
|
||||
if first == last then ([first] ++ accum)
|
||||
else range_ ([first] ++ accum) (builtins.add first 1) last;
|
||||
|
||||
range = range_ [];
|
||||
*/
|
||||
|
||||
body = sum (range 1 50);
|
||||
|
||||
}
|
|
@ -1,18 +1,7 @@
|
|||
with import ./lib.nix;
|
||||
|
||||
let {
|
||||
|
||||
fold = op: nul: list:
|
||||
if list == []
|
||||
then nul
|
||||
else op (builtins.head list) (fold op nul (builtins.tail list));
|
||||
|
||||
concat =
|
||||
fold (x: y: x + y) "";
|
||||
|
||||
flatten = x:
|
||||
if builtins.isList x
|
||||
then fold (x: y: (flatten x) ++ y) [] x
|
||||
else [x];
|
||||
|
||||
l = ["1" "2" ["3" ["4"] ["5" "6"]] "7"];
|
||||
|
||||
body = concat (flatten l);
|
||||
|
|
|
@ -1,13 +1,7 @@
|
|||
with import ./lib.nix;
|
||||
|
||||
let {
|
||||
|
||||
fold = op: nul: list:
|
||||
if list == []
|
||||
then nul
|
||||
else op (builtins.head list) (fold op nul (builtins.tail list));
|
||||
|
||||
concat =
|
||||
fold (x: y: x + y) "";
|
||||
|
||||
body = concat ["foo" "bar" "bla" "test"];
|
||||
|
||||
}
|
|
@ -1 +1 @@
|
|||
List([Call(Function1("x",OpPlus(Var("x"),Str("bar")),Pos("(string)",1,7)),Str("foo")),Call(Function1("x",OpPlus(Var("x"),Str("bar")),Pos("(string)",1,7)),Str("bla")),Call(Function1("x",OpPlus(Var("x"),Str("bar")),Pos("(string)",1,7)),Str("xyzzy"))])
|
||||
Str("foobarblabarxyzzybar")
|
||||
|
|
|
@ -1 +1,3 @@
|
|||
map (x: x + "bar") [ "foo" "bla" "xyzzy" ]
|
||||
with import ./lib.nix;
|
||||
|
||||
concat (map (x: x + "bar") [ "foo" "bla" "xyzzy" ])
|
18
tests/lang/lib.nix
Normal file
18
tests/lang/lib.nix
Normal file
|
@ -0,0 +1,18 @@
|
|||
rec {
|
||||
|
||||
fold = op: nul: list:
|
||||
if list == []
|
||||
then nul
|
||||
else op (builtins.head list) (fold op nul (builtins.tail list));
|
||||
|
||||
concat =
|
||||
fold (x: y: x + y) "";
|
||||
|
||||
flatten = x:
|
||||
if builtins.isList x
|
||||
then fold (x: y: (flatten x) ++ y) [] x
|
||||
else [x];
|
||||
|
||||
sum = fold (x: y: builtins.add x y) 0;
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue