Merge pull request #296272 from hsjobeki/lib/foldl-prime
lib.foldl': avoid unnecessary function call
This commit is contained in:
commit
8437d13b56
1 changed files with 10 additions and 4 deletions
|
@ -228,7 +228,15 @@ rec {
|
||||||
|
|
||||||
`acc`
|
`acc`
|
||||||
|
|
||||||
: The initial accumulator value
|
: The initial accumulator value.
|
||||||
|
|
||||||
|
The accumulator value is evaluated in any case before the first iteration starts.
|
||||||
|
|
||||||
|
To avoid evaluation even before the `list` argument is given an eta expansion can be used:
|
||||||
|
|
||||||
|
```nix
|
||||||
|
list: lib.foldl' op acc list
|
||||||
|
```
|
||||||
|
|
||||||
`list`
|
`list`
|
||||||
|
|
||||||
|
@ -254,13 +262,11 @@ rec {
|
||||||
foldl' =
|
foldl' =
|
||||||
op:
|
op:
|
||||||
acc:
|
acc:
|
||||||
list:
|
|
||||||
|
|
||||||
# The builtin `foldl'` is a bit lazier than one might expect.
|
# The builtin `foldl'` is a bit lazier than one might expect.
|
||||||
# See https://github.com/NixOS/nix/pull/7158.
|
# See https://github.com/NixOS/nix/pull/7158.
|
||||||
# In particular, the initial accumulator value is not forced before the first iteration starts.
|
# In particular, the initial accumulator value is not forced before the first iteration starts.
|
||||||
builtins.seq acc
|
builtins.seq acc
|
||||||
(builtins.foldl' op acc list);
|
(builtins.foldl' op acc);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Map with index starting from 0
|
Map with index starting from 0
|
||||||
|
|
Loading…
Reference in a new issue