blob: 8c2619a2c2a19ccb277eff0f61340abc094c7226 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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.
|