Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A couple of simple primops for easier generic manipulation of n-ary functions #1213

Open
copumpkin opened this issue Feb 2, 2017 · 2 comments
Assignees
Labels
feature Feature request or proposal language The Nix expression language; parser, interpreter, primops, evaluation, etc stale

Comments

@copumpkin
Copy link
Member

copumpkin commented Feb 2, 2017

I'm writing some code right now that looks like this:

{
  abstract = n: fn:
    let
      helper = m: accum: if m == 0 then fn accum else x: helper (m - 1) (accum ++ [x]);
    in helper n [];

  apply = fn: args: if args == [] then fn else apply (fn (builtins.head args)) (builtins.tail args);
}

Basically, abstract lets me take a function that expects an n-ary array as input and gives me n nested lambdas that otherwise behave the same way. On the other end, apply lets me take an n-ary function and apply it to an array.

They seem like they'd both be pretty simple to implement as primops and would eliminate much of the overhead building up or breaking down the accumulator array.

For context, I'm reviving the deterministic evaluation stuff we were doing in #553, but instead trying out @fkz's idea to do as much of as possible in pure Nix and replacing impure primops with scopedImport. As such, I do a few generic operations over the impure primops, and use the functions above to create generic wrapper functions around primops of various arities. I care about performance because I'm injecting this pretty deeply across an entire nixpkgs evaluation and don't want to slow it to a halt.

Even without my use case, these seem like they could allow for easy and cheap generic manipulation of functions in Nix, which I'm sure someone will find good uses for somewhere. If people agree that they could be useful, I'd be happy to make a PR adding them.

cc @edolstra @shlevy @domenkozar

Edit: in practice for my deterministic evaluator, I could take advantage of the fact that all primops I care about have 0, 1, or 2 operands, and not try to be overly generic 😄 but still this would be more fun.

Edit 2: I suppose I could write apply more efficiently than what I have above by using an index and not repeatedly tailing the array.

@shlevy
Copy link
Member

shlevy commented Feb 2, 2017

👍 !

@stale
Copy link

stale bot commented Feb 15, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Feb 15, 2021
@fricklerhandwerk fricklerhandwerk added feature Feature request or proposal language The Nix expression language; parser, interpreter, primops, evaluation, etc labels Sep 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Feature request or proposal language The Nix expression language; parser, interpreter, primops, evaluation, etc stale
Projects
None yet
Development

No branches or pull requests

4 participants