query
On this page

foldlAttrs

lib.foldlAttrs

Docs pulled from | This Revision | about 1 hour ago


Like lib.lists.foldl' but for attribute sets. Iterates over every name-value pair in the given attribute set. The result of the callback function is often called acc for accumulator. It is passed between callbacks from left to right and the final acc is the return value of foldlAttrs.

Attention:

There is a completely different function lib.foldAttrs which has nothing to do with this function, despite the similar name.

Inputs

f

1. Function argument

init

2. Function argument

set

3. Function argument

Type

foldlAttrs :: ( a -> String -> b -> a ) -> a -> { ... :: b } -> a

Examples

lib.attrsets.foldlAttrs usage example

foldlAttrs
  (acc: name: value: {
    sum = acc.sum + value;
    names = acc.names ++ [name];
  })
  { sum = 0; names = []; }
  {
    foo = 1;
    bar = 10;
  }
->
  {
    sum = 11;
    names = ["bar" "foo"];
  }

foldlAttrs
  (throw "function not needed")
  123
  {};
->
  123

foldlAttrs
  (acc: _: _: acc)
  3
  { z = throw "value not needed"; a = throw "value not needed"; };
->
  3

The accumulator doesn't have to be an attrset.
It can be as simple as a number or string.

foldlAttrs
  (acc: _: v: acc * 10 + v)
  1
  { z = 1; a = 2; };
->
  121
(lib.attrsets.foldlAttrs)

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

foldlAttrs = f: init: set:
    foldl'
      (acc: name: f acc name set.${name})
      init
      (attrNames set);