Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to turn an array into a list of parameters for a function?

Tags:

elixir

I have a function that accepts n number of parameters (it's the devinus/sh function) where you can execute a command line program and obtain the results:

Example: Sh.file "-b", "--mime-type", path_to_file

But I want to have the parameters in an array so they can be varying depending on how the function is called.

Example of what I want:

data = ["-b", "--mime-type", path_to_file]

# a way of going through the array and turning it into the parameters for the Sh function

Sh.file <loop array params here>

Sh doesn't take an array for a parameter.

Any ideas?

like image 347
TheStoneFox Avatar asked Jun 28 '15 06:06

TheStoneFox


People also ask

How do you pass an array as an argument in C?

Passing Arrays as Function Arguments in C. If you want to pass a single-dimension array as an argument in a function, you would have to declare a formal parameter in one of following three ways and all three declaration methods produce similar results because each tells the compiler that an integer pointer is going to be received.

How to format array of values in query parameters?

Arrays in query params. The same way there is no concensus over… | by Jonathan Stoikovitch | RAML by Example | Medium The same way there is no concensus over how objects should be represented in query parameters, there is no standardized way to format arrays of values in query parameters.

How to convert array to list in Java?

Below are methods to convert Array to List in Java: Brute Force or Naive Method: In this method, an empty List is created and all elements present of the Array are added to it one by one. Using Arrays.asList() method: In this method, the Array is passed as the parameter into the List constructor with the help of Arrays.asList() method.

How do I call a function from an array of arguments?

var args = ; function call_me (param0, param1, param2) { // ... } // Calling the function using the array with apply () call_me.apply (this, args);


1 Answers

You can use apply(Sh, :file, args), where args is an array of arguments.

like image 158
bitwalker Avatar answered Oct 15 '22 05:10

bitwalker