about summary refs log tree commit diff
path: root/lib/default.nix
diff options
context:
space:
mode:
authordanbst <abcz2.uprola@gmail.com>2019-07-14 13:17:49 +0300
committerdanbst <abcz2.uprola@gmail.com>2019-07-14 13:29:58 +0300
commit69920dafbffcd757acff23f659263ec4b952a017 (patch)
tree4390925c6087d284c11183193044835152f6cea5 /lib/default.nix
parent696767a9c9dc4b6b5066a3172c89138c0bb6e40d (diff)
downloadnixlib-69920dafbffcd757acff23f659263ec4b952a017.tar
nixlib-69920dafbffcd757acff23f659263ec4b952a017.tar.gz
nixlib-69920dafbffcd757acff23f659263ec4b952a017.tar.bz2
nixlib-69920dafbffcd757acff23f659263ec4b952a017.tar.lz
nixlib-69920dafbffcd757acff23f659263ec4b952a017.tar.xz
nixlib-69920dafbffcd757acff23f659263ec4b952a017.tar.zst
nixlib-69920dafbffcd757acff23f659263ec4b952a017.zip
lib: introduce `foreach` = flip map
The main purpose is to bring attention to `flip map`, which improves
code readablity. It is useful when ad-hoc anonymous function
grows two or more lines in `map` application:

```
      map (lcfg:
        let port = lcfg.port;
            portStr = if port != defaultPort then ":${toString port}" else "";
            scheme = if cfg.enableSSL then "https" else "http";
        in "${scheme}://cfg.hostName${portStr}"
      ) (getListen cfg);
```
Compare this to `foreach`-style:
```
      foreach (getListen cfg) (lcfg:
        let port = lcfg.port;
            portStr = if port != defaultPort then ":${toString port}" else "";
            scheme = if cfg.enableSSL then "https" else "http";
        in "${scheme}://cfg.hostName${portStr}"
      );
```
This is similar to Haskell's `for` (http://hackage.haskell.org/package/base-4.12.0.0/docs/Data-Traversable.html#v:for)
Diffstat (limited to 'lib/default.nix')
-rw-r--r--lib/default.nix2
1 files changed, 1 insertions, 1 deletions
diff --git a/lib/default.nix b/lib/default.nix
index 3efaaf0f8f9e..f876c57e25c8 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -71,7 +71,7 @@ let
       zipAttrsWithNames zipAttrsWith zipAttrs recursiveUpdateUntil
       recursiveUpdate matchAttrs overrideExisting getOutput getBin
       getLib getDev chooseDevOutputs zipWithNames zip;
-    inherit (lists) singleton foldr fold foldl foldl' imap0 imap1
+    inherit (lists) singleton foreach foldr fold foldl foldl' imap0 imap1
       concatMap flatten remove findSingle findFirst any all count
       optional optionals toList range partition zipListsWith zipLists
       reverseList listDfs toposort sort naturalSort compareLists take