solutions/A Tour of Nix/28.nix

11 lines
299 B
Nix
Raw Normal View History

2024-04-16 19:13:00 -04:00
with import <nixpkgs> { };
let
list = ["a" "b" "a" "c" "d" "a"];
countA = lib.fold (x: y: if x != "a" then y else y + 1) 0;
in
rec {
example = lib.fold (x: y: x + y) "" ["a" "b" "c"]; #is "abc"
result = countA list; #should be 3
}
# fold f z [x_1 x_2 ... x_n] == f x_1 (f x_2 ... (f x_n z))