summaryrefslogtreecommitdiff
path: root/misc
diff options
context:
space:
mode:
authorseth <[email protected]>2023-08-16 08:58:40 -0400
committerseth <[email protected]>2023-08-16 08:58:40 -0400
commite47de8355dca82953a77618af636524feef361dd (patch)
treef33d37754807f418a2ebc710bcc829bfeb4d9faa /misc
parent79322f3d0370ca25670d96b5053cde964f081684 (diff)
misc: add fizzbuzz
Diffstat (limited to 'misc')
-rw-r--r--misc/fizzbuzz.nix27
1 files changed, 27 insertions, 0 deletions
diff --git a/misc/fizzbuzz.nix b/misc/fizzbuzz.nix
new file mode 100644
index 0000000..8306bd1
--- /dev/null
+++ b/misc/fizzbuzz.nix
@@ -0,0 +1,27 @@
+max: let
+ inherit (builtins) concatStringsSep filter isString map toString;
+
+ mod = num: denom: num / denom * denom == num;
+
+ results = {
+ "15" = "fizzbuzz";
+ "3" = "fizz";
+ "5" = "buzz";
+ };
+
+ fizzbuzz = num: let
+ a = filter isString (map (d:
+ if mod num d
+ then results.${toString d}
+ else d) [15 3 5]);
+ in
+ if a == []
+ then toString num
+ else builtins.elemAt a 0;
+
+ generate = i: max:
+ if i == max
+ then ["${fizzbuzz i}\n"]
+ else [(fizzbuzz i)] ++ (generate (i + 1) max);
+in
+ concatStringsSep "\n" (generate 1 max)