Skip to content

Instantly share code, notes, and snippets.

@fricklerhandwerk
Created August 26, 2022 23:39
Show Gist options
  • Save fricklerhandwerk/a49d826bac0d35b3c1c9578191def7b2 to your computer and use it in GitHub Desktop.
Save fricklerhandwerk/a49d826bac0d35b3c1c9578191def7b2 to your computer and use it in GitHub Desktop.
missing standard library functions for the Nix language
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