"apply" for scala


In a language like Scheme, where there is a method that accepts variable number of arguments... We can use "apply" to provide the arguments stored in a list as follow...

#;> (define (m . args) args)
#;> (m 1 2 3)
(1 2 3)
;use apply to provide same arguments stored in a list
#;> (apply m (list 1 2 3))
(1 2 3)

I wonder if scala has a similar feature? So far I've not seen counterparts of things like read/eval/apply  in Scheme. I don't know if its even possible to have them without a lisp-like syntax.

Thanks,
Himanshu

Re: "apply" for scala

Is this what you're looking for?

-jason

scala> def m(as: Int*) = as
m: (as: Int*)Int*

scala> m(1, 2, 3)
res0: Int* = Array(1, 2, 3)

scala> m(List(1, 2, 3): _*)
res1: Int* = List(1, 2, 3)

scala> m(Seq(1, 2, 3): _*)
res2: Int* = List(1, 2, 3)

On Wed, Jan 13, 2010 at 9:01 AM, Himanshu <g [dot] himanshu [at] gmail [dot] com> wrote:

In a language like Scheme, where there is a method that accepts variable number of arguments... We can use "apply" to provide the arguments stored in a list as follow...

#;> (define (m . args) args)
#;> (m 1 2 3)
(1 2 3)
;use apply to provide same arguments stored in a list
#;> (apply m (list 1 2 3))
(1 2 3)

I wonder if scala has a similar feature? So far I've not seen counterparts of things like read/eval/apply  in Scheme. I don't know if its even possible to have them without a lisp-like syntax.

Thanks,
Himanshu

Re: "apply" for scala

Exactly.

But How does it work, is this part of function call syntax... I seem to have missed it in "Programming in Scala".

Thanks,
Himanshu

On Wed, Jan 13, 2010 at 1:41 PM, Jason Zaugg <jzaugg [at] gmail [dot] com> wrote:
Is this what you're looking for?

-jason

scala> def m(as: Int*) = as
m: (as: Int*)Int*

scala> m(1, 2, 3)
res0: Int* = Array(1, 2, 3)

scala> m(List(1, 2, 3): _*)
res1: Int* = List(1, 2, 3)

scala> m(Seq(1, 2, 3): _*)
res2: Int* = List(1, 2, 3)

On Wed, Jan 13, 2010 at 9:01 AM, Himanshu <g [dot] himanshu [at] gmail [dot] com> wrote:

In a language like Scheme, where there is a method that accepts variable number of arguments... We can use "apply" to provide the arguments stored in a list as follow...

#;> (define (m . args) args)
#;> (m 1 2 3)
(1 2 3)
;use apply to provide same arguments stored in a list
#;> (apply m (list 1 2 3))
(1 2 3)

I wonder if scala has a similar feature? So far I've not seen counterparts of things like read/eval/apply  in Scheme. I don't know if its even possible to have them without a lisp-like syntax.

Thanks,
Himanshu


Re: "apply" for scala

Himanshu wrote:
> But How does it work, is this part of function call syntax... I seem to
> have missed it in "Programming in Scala".

You can find more info on
Chapter "Function and Closures" / Section "Repeated parameters"

Ciao
Paolo

Re: "apply" for scala

See §6.6 of the Scala Reference [1]. (I linked to the 2.8 version, but the same applies in 2.7).

"The last argument in an application may be marked as a sequence argument, e.g.
e: _*. Such an argument must correspond to a repeated parameter (§4.6.2) of type
S* and it must be the only argument matching this parameter (i.e. the number of
formal parameters and actual arguments must be the same). Furthermore, the type
of e must conform to scala.Seq[T ], for some type T which conforms to S. In this
case, the argument list is transformed by replacing the sequence e with its elements.
When the application uses named arguments, the vararg parameter has to be specified
exactly once."

[1] http://www.scala-lang.org/archives/downloads/distrib/files/nightly/pdfs/ScalaReference.pdf

On Wed, Jan 13, 2010 at 9:18 AM, Himanshu <g [dot] himanshu [at] gmail [dot] com> wrote:
Exactly.

But How does it work, is this part of function call syntax... I seem to have missed it in "Programming in Scala".

Thanks,
Himanshu

On Wed, Jan 13, 2010 at 1:41 PM, Jason Zaugg <jzaugg [at] gmail [dot] com> wrote:
Is this what you're looking for?

-jason

scala> def m(as: Int*) = as
m: (as: Int*)Int*

scala> m(1, 2, 3)
res0: Int* = Array(1, 2, 3)

scala> m(List(1, 2, 3): _*)
res1: Int* = List(1, 2, 3)

scala> m(Seq(1, 2, 3): _*)
res2: Int* = List(1, 2, 3)

On Wed, Jan 13, 2010 at 9:01 AM, Himanshu <g [dot] himanshu [at] gmail [dot] com> wrote:

In a language like Scheme, where there is a method that accepts variable number of arguments... We can use "apply" to provide the arguments stored in a list as follow...

#;> (define (m . args) args)
#;> (m 1 2 3)
(1 2 3)
;use apply to provide same arguments stored in a list
#;> (apply m (list 1 2 3))
(1 2 3)

I wonder if scala has a similar feature? So far I've not seen counterparts of things like read/eval/apply  in Scheme. I don't know if its even possible to have them without a lisp-like syntax.

Thanks,
Himanshu



Re: "apply" for scala

Thanks Jason :)

On Wed, Jan 13, 2010 at 1:55 PM, Jason Zaugg <jzaugg [at] gmail [dot] com> wrote:
See §6.6 of the Scala Reference [1]. (I linked to the 2.8 version, but the same applies in 2.7).

"The last argument in an application may be marked as a sequence argument, e.g.
e: _*. Such an argument must correspond to a repeated parameter (§4.6.2) of type
S* and it must be the only argument matching this parameter (i.e. the number of
formal parameters and actual arguments must be the same). Furthermore, the type
of e must conform to scala.Seq[T ], for some type T which conforms to S. In this
case, the argument list is transformed by replacing the sequence e with its elements.
When the application uses named arguments, the vararg parameter has to be specified
exactly once."

[1] http://www.scala-lang.org/archives/downloads/distrib/files/nightly/pdfs/ScalaReference.pdf


Copyright © 2013 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland