- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Swap and tuples
Fri, 2010-09-03, 00:49
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)
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)
Fri, 2010-09-03, 01:17
#2
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:
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)
Fri, 2010-09-03, 14:47
#3
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.
Fri, 2010-09-03, 14:57
#4
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:
--
Kevin Wright
mail / gtalk / msn : kev [dot] lee [dot] wright [at] gmail [dot] com
pulse / skype: kev.lee.wright
twitter: @thecoda
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
Fri, 2010-09-03, 15:07
#5
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
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)