fn.meetsConDo: improve performance and add documentation.
This commit is contained in:
parent
c1c5ff299e
commit
1812effbd7
1 changed files with 36 additions and 9 deletions
39
fn.nix
39
fn.nix
|
@ -65,17 +65,44 @@ rec {
|
|||
|
||||
# Not sure how list operations are implemented in Nix
|
||||
# This might be a tad bit inefficient.
|
||||
# TODO: look for better implementation (map is a builtin function so checking that probably won't help)
|
||||
# Sequentially checks elements of list (l) for condition (cond) and executes do on first match.
|
||||
/**
|
||||
Run lambda "(do element)" on the first element of list "l" where (cond element) returns true otherwise return false.
|
||||
|
||||
# Type
|
||||
```
|
||||
meetsConDo :: (any -> bool) -> (any -> any) -> [any] -> any
|
||||
```
|
||||
|
||||
# Arguments:
|
||||
- [cond] Condition function
|
||||
- [do] Function to run on element if cond returns true
|
||||
- [list] List of elements
|
||||
|
||||
# Example:
|
||||
```nix
|
||||
meetsConDo (element: element >= 5) (element: element + 1) [ 4 2 0 5 1 ]
|
||||
=> 6
|
||||
|
||||
meetsConDo (element: element >= 5) (element: element + 1) [ 4 2 0 1 ]
|
||||
=> false
|
||||
```
|
||||
*/
|
||||
meetsConDo =
|
||||
cond: do: l:
|
||||
ifelse (l == [ ]) false (
|
||||
cond: do: list:
|
||||
let
|
||||
h = head l;
|
||||
t = tail l;
|
||||
listLen = length list;
|
||||
meetsConDo' =
|
||||
currentIndex:
|
||||
ifelse (currentIndex == listLen) false (
|
||||
let
|
||||
head = elemAt list currentIndex;
|
||||
in
|
||||
ifelse (cond h) (do h) (meetsConDo cond do t)
|
||||
ifelse (cond head) (do head) (meetsConDo' (currentIndex + 1))
|
||||
);
|
||||
in
|
||||
meetsConDo' 0;
|
||||
|
||||
deps =
|
||||
p:
|
||||
ifelse (isAttrs p) (filter isAttrs (
|
||||
|
|
Loading…
Reference in a new issue