Name this python/ruby language construct (using array values to satisfy function parameters) Name this python/ruby language construct (using array values to satisfy function parameters) python python

Name this python/ruby language construct (using array values to satisfy function parameters)


The Python docs call this Unpacking Argument Lists. It's a pretty handy feature. In Python, you can also use a double asterisk (**) to unpack a dictionary (hash) into keyword arguments. They also work in reverse. I can define a function like this:

def sum(*args):    result = 0    for a in args:        result += a    return resultsum(1,2)sum(9,5,7,8)sum(1.7,2.3,8.9,3.4)

To pack all arguments into an arbitrarily sized list.


In ruby, it is often called "splat".

Also in ruby, you can use it to mean 'all of the other elements in the list'.

a, *rest = [1,2,3,4,5,6]a     # => 1rest  # => [2, 3, 4, 5, 6]

It can also appear on either side of the assignment operator:

a  = d, *e

In this usage, it is a bit like scheme's cdr, although it needn't be all but the head of the list.


The typical terminology for this is called "applying a function to a list",or "apply" for short.

See http://en.wikipedia.org/wiki/Apply

It has been in LISP since pretty much its inception back in 1960 odd.Glad python rediscovered it :-}

Apply is typically on a list or a representation of a list suchas an array. However, one can apply functions to arguments thatcome from other palces, such as structs. Our PARLANSE languagehas fixed types (int, float, string, ...) and structures.Oddly enough, a function argument list looks a lot like a structuredefinintion, and in PARLANSE, it is a structure definition,and you can "apply" a PARLANSE function to a compatible structure.You can "make" structure instances, too, thus:

 (define S    (structure [t integer]               [f float]               [b (array boolean 1 3)]    )structure )define s  (= A (array boolean 1 3 ~f ~F ~f))  (= s (make S -3 19.2 (make (array boolean 1 3) ~f ~t ~f))  (define foo (function string S) ...)  (foo +17 3e-2 A) ; standard function call  (foo s) ; here's the "apply"

PARLANSE looks like lisp but isn't.