- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Pattern match / deconstruct in method parameter list
Tue, 2011-12-20, 06:32
How do I write a method which takes a Tuple (Int, String), and
deconstructs it in the parameter list itself?
Thanks.
Tue, 2011-12-20, 07:51
#2
Re: Pattern match / deconstruct in method parameter list
On Mon, Dec 19, 2011 at 11:32:13PM -0600, Sophie wrote:
> How do I write a method which takes a Tuple (Int, String), and
> deconstructs it in the parameter list itself?
I guess you're looking for something like this?
def foo(a:Int, (b, c):(String, Int)) = ...
Unfortunately as far as I know this isn't possible. You'll have to use
a val declaration to destructure the tuple.
def foo(a:Int, tpl:(String, Int)) = {
val (b, c) = tpl
...
}
Or you could use a match.
def foo(a:Int, tpl:(String, Int)) = tpl match {
case (b, c) => ...
}
Hope this helps.
Tue, 2011-12-20, 16:01
#3
Re: Pattern match / deconstruct in method parameter list
On 2011-12-20 00:41:02 -0600, Erik Osheim said:
> On Mon, Dec 19, 2011 at 11:32:13PM -0600, Sophie wrote:
>> How do I write a method which takes a Tuple (Int, String), and
>> deconstructs it in the parameter list itself?
>
> I guess you're looking for something like this?
>
> def foo(a:Int, (b, c):(String, Int)) = ...
Yes, that is what I meant. I use tuples anytime I just want a logical
group with no meaningful name (Compound + #OfMolecules, product +
quantity, etc.) and it feels artificial to use "tpl" in that context.
Love how Clojure does this.
Thanks.
>
> Unfortunately as far as I know this isn't possible. You'll have to use
> a val declaration to destructure the tuple.
>
> def foo(a:Int, tpl:(String, Int)) = {
> val (b, c) = tpl
> ...
> }
>
> Or you could use a match.
>
> def foo(a:Int, tpl:(String, Int)) = tpl match {
> case (b, c) => ...
> }
>
> Hope this helps.
>
Tue, 2011-12-20, 17:11
#4
Re: Re: Pattern match / deconstruct in method parameter list
As long as the items aren't actually bundled into tuples that you're trying to pass in, you can use multiple parameter blocks to achieve the same sort of grouping:
def foo(a: Int)(b: String, c: Int) = ...
Additionally, later blocks can refer to earlier ones for default values, e.g.:
def foo(a: Int)(b: String, c: Int = a/2) = ...
And if you really want to you can use this to take apart tuples:
def foo(a: Int)(x: (String,Int))(b: String = x._1, c: Int = x._2)
foo(1)(("hi",2))() // Note--need extra empty parens to get defaults
but there's not much reason to do that since you can just
def foo(a: Int)(x: (String, Int)) = {
val (b,c) = x
...
}
to take apart your tuple if that's what you've got.
(Scala hasn't unified tuples and function arguments; it's a difficult thing to do well, because the JVM makes the distinction, and if you start wrapping things in objects you end up with a sizable performance hit.)
--Rex
On Tue, Dec 20, 2011 at 9:58 AM, Sophie <itsme213@hotmail.com> wrote:
def foo(a: Int)(b: String, c: Int) = ...
Additionally, later blocks can refer to earlier ones for default values, e.g.:
def foo(a: Int)(b: String, c: Int = a/2) = ...
And if you really want to you can use this to take apart tuples:
def foo(a: Int)(x: (String,Int))(b: String = x._1, c: Int = x._2)
foo(1)(("hi",2))() // Note--need extra empty parens to get defaults
but there's not much reason to do that since you can just
def foo(a: Int)(x: (String, Int)) = {
val (b,c) = x
...
}
to take apart your tuple if that's what you've got.
(Scala hasn't unified tuples and function arguments; it's a difficult thing to do well, because the JVM makes the distinction, and if you start wrapping things in objects you end up with a sizable performance hit.)
--Rex
On Tue, Dec 20, 2011 at 9:58 AM, Sophie <itsme213@hotmail.com> wrote:
On 2011-12-20 00:41:02 -0600, Erik Osheim said:
On Mon, Dec 19, 2011 at 11:32:13PM -0600, Sophie wrote:
How do I write a method which takes a Tuple (Int, String), and
deconstructs it in the parameter list itself?
I guess you're looking for something like this?
def foo(a:Int, (b, c):(String, Int)) = ...
Yes, that is what I meant. I use tuples anytime I just want a logical group with no meaningful name (Compound + #OfMolecules, product + quantity, etc.) and it feels artificial to use "tpl" in that context. Love how Clojure does this.
Tue, 2011-12-20, 21:31
#5
Re: Pattern match / deconstruct in method parameter list
Am 20.12.2011 06:32, schrieb Sophie:
> How do I write a method which takes a Tuple (Int, String), and
> deconstructs it in the parameter list itself?
>
> Thanks.
>
i've gotten used to this in clojure, but scala has no such feature :(
but i stumbled upon the opposite feature (write parameter list and get a
tuple):
def takesTuple(tpl:Product) = {
tpl match {
case (a:String,b:Int,c:Double) => println(a+b+c)
}
}
you can call it like this:
takesTuple("hi",5,5.5)
you'll switch off type safety by using this little trick :(, but you'll
get a pretty clojurery feel. throw in some implicit conversions,
additional extractors and you'll probably get it run with lists, array
and whatever else you want.
if that doesn't scare you off, be happy with it.
automatic conversion from tuple to parameters lists has not been
implemented afaik.
a bit safer:
def takesSafeTuple(t:(Int, String)) {
}
takesSafeTuple(5,"hello tuple world")
actually i discovered this just a few minutes ago while poking around,
but i really like it. the method signature actually becomes *shorter*
because i can just skip all the parameter names which are useless in
obvious cases
i love scala. awesome language.
Wed, 2011-12-21, 13:11
#6
Re: Pattern match / deconstruct in method parameter list
On Tue, Dec 20, 2011 at 18:22, HamsterofDeath wrote:
> Am 20.12.2011 06:32, schrieb Sophie:
>> How do I write a method which takes a Tuple (Int, String), and
>> deconstructs it in the parameter list itself?
>>
>> Thanks.
>>
>
> i've gotten used to this in clojure, but scala has no such feature :(
>
> but i stumbled upon the opposite feature (write parameter list and get a
> tuple):
>
> def takesTuple(tpl:Product) = {
> tpl match {
> case (a:String,b:Int,c:Double) => println(a+b+c)
> }
> }
>
> you can call it like this:
>
> takesTuple("hi",5,5.5)
>
> you'll switch off type safety by using this little trick :(, but you'll
> get a pretty clojurery feel. throw in some implicit conversions,
> additional extractors and you'll probably get it run with lists, array
> and whatever else you want.
> if that doesn't scare you off, be happy with it.
Ok, that trick I had never thought of. Nice!
Wed, 2011-12-21, 20:31
#7
Re: Pattern match / deconstruct in method parameter list
one of the elite ones patted my back. *happy* :)
Am 21.12.2011 13:05, schrieb Daniel Sobral:
> On Tue, Dec 20, 2011 at 18:22, HamsterofDeath wrote:
>> Am 20.12.2011 06:32, schrieb Sophie:
>>> How do I write a method which takes a Tuple (Int, String), and
>>> deconstructs it in the parameter list itself?
>>>
>>> Thanks.
>>>
>> i've gotten used to this in clojure, but scala has no such feature :(
>>
>> but i stumbled upon the opposite feature (write parameter list and get a
>> tuple):
>>
>> def takesTuple(tpl:Product) = {
>> tpl match {
>> case (a:String,b:Int,c:Double) => println(a+b+c)
>> }
>> }
>>
>> you can call it like this:
>>
>> takesTuple("hi",5,5.5)
>>
>> you'll switch off type safety by using this little trick :(, but you'll
>> get a pretty clojurery feel. throw in some implicit conversions,
>> additional extractors and you'll probably get it run with lists, array
>> and whatever else you want.
>> if that doesn't scare you off, be happy with it.
> Ok, that trick I had never thought of. Nice!
>
Fri, 2011-12-23, 08:21
#8
Re: Pattern match / deconstruct in method parameter list
Does this help?
def m: (Int, String) => XX = {
case (n, s) if n < 0 => sys.error("todo")
case (0, s) => sys.error("todo")
case (n, SomeExtractor(s)) => sys.error("todo")
}
On Tuesday, December 20, 2011, Sophie <itsme213@hotmail.com> wrote:
> How do I write a method which takes a Tuple (Int, String), and deconstructs it in the parameter list itself?
>
> Thanks.
>
>
>
def m: (Int, String) => XX = {
case (n, s) if n < 0 => sys.error("todo")
case (0, s) => sys.error("todo")
case (n, SomeExtractor(s)) => sys.error("todo")
}
On Tuesday, December 20, 2011, Sophie <itsme213@hotmail.com> wrote:
> How do I write a method which takes a Tuple (Int, String), and deconstructs it in the parameter list itself?
>
> Thanks.
>
>
>
Fri, 2011-12-23, 08:31
#9
Re: Pattern match / deconstruct in method parameter list
Am 23.12.2011 08:05, schrieb Naftoli Gugenheim:
*adding to cheat sheet*
pRO6x5j935yYhXBqbKysfQVS+2VA [at] mail [dot] gmail [dot] com" type="cite"> Does this help?omg!!!!
def m: (Int, String) => XX = {
case (n, s) if n < 0 => sys.error("todo")
case (0, s) => sys.error("todo")
case (n, SomeExtractor(s)) => sys.error("todo")
}
On Tuesday, December 20, 2011, Sophie <itsme213 [at] hotmail [dot] com" target="_blank" rel="nofollow">itsme213@hotmail.com> wrote:
> How do I write a method which takes a Tuple (Int, String), and deconstructs it in the parameter list itself?
>
> Thanks.
>
>
>
*adding to cheat sheet*
scala> def add(a:Int,b:Int) = a + b
add: (a: Int, b: Int)Int
scala> val f:(Int,Int)=>Int = add
f: (Int, Int) => Int = <function2>
scala> val g = Function.tupled(f)
g: (Int, Int) => Int = <function1>
scala> val tuple = (1, 3)
tuple: (Int, Int) = (1,3)
scala> val tuple = (1, 3)
tuple: (Int, Int) = (1,3)
scala> val h = Function.untupled(g)
h: (Int, Int) => Int = <function2>
scala> h(5, 5)
res11: Int = 10
On Tue, Dec 20, 2011 at 6:32 AM, Sophie <itsme213@hotmail.com> wrote: