Created
August 26, 2022 23:39
-
-
Save fricklerhandwerk/a49d826bac0d35b3c1c9578191def7b2 to your computer and use it in GitHub Desktop.
missing standard library functions for the Nix language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with builtins; | |
{ | |
every = n: xs: | |
# take every `n`-th element from the list `xs` | |
if n < 2 then xs else | |
if xs == [] then [] else | |
if length (tail xs) < n - 1 then [ (head xs) ] else | |
[ (head xs) ] ++ every (apply-n-times tail xs n) n | |
; | |
apply-n-times = n: f: arg: | |
if n < 1 | |
then arg | |
else apply-n-times (n - 1) f (f arg) | |
; | |
# the following does not work: | |
# | |
# iterate = f: x: | |
# [ x ] ++ iterate f (f x) | |
# ; | |
# | |
# it produces a segmentation fault when evaluating a finite subset: | |
# | |
# head (iterate (x: x + 1) 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment