Skip to content

apply an array of arguments to a multi parameter function #1555

Answered by yannham
suimong asked this question in Q&A
Discussion options

You must be logged in to vote

I don't think there's a more idiomatic way to do this currently. In the Nix world, it's common to take a record as an argument and destructure it in the function, to emulate named parameters:

let foo = fun {a, b, c ? 0} => a + b + c in
foo {a = 1, b = 2}

This provides some structure, and the ability to build and pass one structured argument . But nothing like this for arrays and positional arguments. If you need this repeatedly, the best you can do is probably to put this code in a helper function:

let apply_packed = fun f args =>
  let rpipe = std.function.flip (|>) in
  std.array.fold_left rpipe f args
in
apply_packed foo xs

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@suimong
Comment options

Answer selected by suimong
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants