diff options
author | Jasper Ras <jras@hostnet.nl> | 2025-03-20 11:07:49 +0100 |
---|---|---|
committer | Jasper Ras <jras@hostnet.nl> | 2025-03-20 11:07:49 +0100 |
commit | 80ccf68f55dbb70d7e5ed52ee95b3c9d1b6ce264 (patch) | |
tree | 93e28e85ab70052aa6f577998ec7dc1f413b40c0 /3 Resources/Nix/Functional programming vs imperative.md | |
parent | 9642cd7ae24f0ba79ce5647c709b35ae8f06a285 (diff) |
vault backup: 2025-03-20 11:07:48
Diffstat (limited to '3 Resources/Nix/Functional programming vs imperative.md')
-rw-r--r-- | 3 Resources/Nix/Functional programming vs imperative.md | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/3 Resources/Nix/Functional programming vs imperative.md b/3 Resources/Nix/Functional programming vs imperative.md new file mode 100644 index 0000000..8c2619a --- /dev/null +++ b/3 Resources/Nix/Functional programming vs imperative.md @@ -0,0 +1,26 @@ +--- +tags: + - programming + - nix +--- +While working on a NixOS module I made a realisation on the difference between functional and imperative programming. +I was trying to configure `systemd.tmpfiles.rules` to create directories for git repositories. After looking a bit how to iterate; i tried the following: +```nix +let + paths = ["abs_paths" ...] +in +{ + map(p: systemd.tmpfiles.rules = ["d {$p} ..."];); +} +``` +This is very much originating from the imperative mindset; we loop over a list and then do stuff like setting variables and calling other functions. However in functional programming we do not! This is the correct version in functional programming: +```nix +let + paths = [ "abs_paths" ... ]; +in +{ + systemd.tmpfiles.rules = map(p: "d ${p} ..") paths; +} +``` + +Functions are pure, we can't assign stuff inside of their bodies because that would make them impure. Instead we can just return a value; in this case an array and *then* assign it.
\ No newline at end of file |