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

Swap and tuples

5 replies
tesshu
Joined: 2010-08-18,
User offline. Last seen 2 years 8 weeks ago.
Since Scala supports things like
var (a,b) = (1,2)
why it doesn't support 
(a,b) = (b,a)???
Is there anyway of making use of implicit declarations of a Tuple2 to something that overloads the = method and do the swapping?
I know the Tuple2 swap method works for this example (a,b).swap will swap a and b values, but i want something that work for more complex stuff, like an Array
scala> var a = Array(1, 2) a: Array[Int] = Array(1, 2)
scala> (a(0), a(1)).swapres75: (Int, Int) = (2,1)
scala> ares76: Array[Int] = Array(1, 2)
richard emberson
Joined: 2010-03-22,
User offline. Last seen 42 years 45 weeks ago.
Re: Swap and tuples

scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> a.reverse
res0: Array[Int] = Array(3, 2, 1)

On 09/02/2010 04:50 PM, Bruno Mendes wrote:
> Since Scala supports things like
>
> var (a,b) = (1,2)
>
> why it doesn't support
>
> (a,b) = (b,a)???
>
> Is there anyway of making use of implicit declarations of a Tuple2 to
> something that overloads the = method and do the swapping?
>
> I know the Tuple2 swap method works for this example (a,b).swap will
> swap a and b values, but i want something that work for more complex
> stuff, like an Array
>
> scala> var a = Array(1, 2)
> a: Array[Int] = Array(1, 2)
>
> scala> (a(0), a(1)).swap
> res75: (Int, Int) = (2,1)
>
> scala> a
> res76: Array[Int] = Array(1, 2)

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 5 days ago.
Re: Swap and tuples
If you absolutely insist on using a mutable type...
def swap[T](arr : Array[T], idx1 : Int, idx2 : Int) = {  val x = arr(idx1)  arr(idx1) = arr(idx2)  arr(idx2) = x }
I make no guarantees for thread safety :)

On 3 September 2010 00:58, richard emberson <richard [dot] emberson [at] gmail [dot] com> wrote:
scala> val a = Array(1,2,3)
a: Array[Int] = Array(1, 2, 3)

scala> a.reverse
res0: Array[Int] = Array(3, 2, 1)



On 09/02/2010 04:50 PM, Bruno Mendes wrote:
Since Scala supports things like

var (a,b) = (1,2)

why it doesn't support

(a,b) = (b,a)???

Is there anyway of making use of implicit declarations of a Tuple2 to
something that overloads the = method and do the swapping?

I know the Tuple2 swap method works for this example (a,b).swap will
swap a and b values, but i want something that work for more complex
stuff, like an Array

scala> var a = Array(1, 2)
a: Array[Int] = Array(1, 2)

scala> (a(0), a(1)).swap
res75: (Int, Int) = (2,1)

scala> a
res76: Array[Int] = Array(1, 2)

Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.
Re: Swap and tuples

>>>>> "Bruno" == Bruno Mendes writes:

Bruno> Since Scala supports things like var (a,b) = (1,2)
Bruno> why it doesn't support
Bruno> (a,b) = (b,a)???

I'd swear there's a ticket on this, but I just can't seem to find it no
matter what I search for in Trac. Anyway, iirc, the answer is, it's
trickier than you might imagine and a SID is needed.

Consider that var (a,b) = (1,2) isn't special syntax, it's just pattern
matching. So the SID would need to cover patterns in general, not just
tuples.

Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 5 days ago.
Re: Swap and tuples
let me get this right, you're looking to extract the results of a pattern match into pre-existing vars?
I'm not sure if it's inspired, or a recipe for disaster :P


On 3 September 2010 14:43, Seth Tisue <seth [at] tisue [dot] net> wrote:
>>>>> "Bruno" == Bruno Mendes <b [dot] mendes00 [at] gmail [dot] com> writes:

 Bruno> Since Scala supports things like var (a,b) = (1,2)
 Bruno> why it doesn't support
 Bruno> (a,b) = (b,a)???

I'd swear there's a ticket on this, but I just can't seem to find it no
matter what I search for in Trac.  Anyway, iirc, the answer is, it's
trickier than you might imagine and a SID is needed.

Consider that var (a,b) = (1,2) isn't special syntax, it's just pattern
matching.  So the SID would need to cover patterns in general, not just
tuples.

--
Seth Tisue @ Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/



--
Kevin Wright

mail / gtalk / msn : kev [dot] lee [dot] wright [at] gmail [dot] com
pulse / skype: kev.lee.wright
twitter: @thecoda

ijuma
Joined: 2008-08-20,
User offline. Last seen 22 weeks 3 days ago.
Re: Swap and tuples

On Fri, Sep 3, 2010 at 2:43 PM, Seth Tisue wrote:
> I'd swear there's a ticket on this, but I just can't seem to find it no
> matter what I search for in Trac.

It takes special skills to find things on Trac:

http://lampsvn.epfl.ch/trac/scala/ticket/1324

;)

Ismael

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