This page is no longer maintained — Please continue to the home page at www.scala-lang.org

"apply" for scala

5 replies
Himanshu
Joined: 2009-10-27,
User offline. Last seen 42 years 45 weeks ago.

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
Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
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.himanshu@gmail.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

Himanshu
Joined: 2009-10-27,
User offline. Last seen 42 years 45 weeks ago.
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@gmail.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.himanshu@gmail.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


Jason Zaugg
Joined: 2009-05-18,
User offline. Last seen 38 weeks 5 days ago.
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.himanshu@gmail.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@gmail.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.himanshu@gmail.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



Himanshu
Joined: 2009-10-27,
User offline. Last seen 42 years 45 weeks ago.
Re: "apply" for scala
Thanks Jason :)

On Wed, Jan 13, 2010 at 1:55 PM, Jason Zaugg <jzaugg@gmail.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


Paolo Losi
Joined: 2009-11-05,
User offline. Last seen 42 years 45 weeks ago.
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

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