If its immutable, you wouldn't "change" it -- you would replace it's value in the containing collection.
Scala's immutable collections are written to be efficient under replaced contents. Take, for example, a List (which is a classic cons list): Replacing the value at the head of the list actually only replaces the node object at the head of the list. The entire tail is preserved. Replacing the tail-most item, however, would cause the entire List to have to be recreated, because either directly or indirectly every other node holds a reference to the tail-most item.
Maps and other structures similarly attempt to preserve as must of the data structure as possible during value replacement. The concept is called "persistent data structures" (not 'persistent' in the persistent-DB sense on the word, but rather in the sense that as much of the data structure is re-used as possible). Check out the wikipedia article, which is a good summary of the concept: http://en.wikipedia.org/wiki/Persistent_data_structure.
best,Miguel Negrão
class a(val b:B)
class b(val a:A)
;)
-------- Original-Nachricht --------
> Datum: Tue, 10 May 2011 14:12:14 +0100
> Von: Kevin Wright <kev.lee...@gmail.com>
> An: scala...@googlegroups.com
> Betreff: Re: [scala-user] Re: Object hierarchies consisting of immutable objects?
Dennis> that's a problem you have with immutable data structures. some
Dennis> things are simply not possible: class a(val b:B) class b(val
Dennis> a:A)
Not impossible!
Miles Sabin on how to make an immutable one-object cycle:
The same technique with two classes:
scala> :paste
class A private (_b: B) {
val b = if(_b == null) new B(this) else _b
def this() = this(null)
}
class B(val a: A)
^D
defined class A
defined class B
scala> new A
res0: A = A@39c931fb
scala> res0.b
res1: B = B@6b79d47f
scala> res0.b.a
res2: A = A@39c931fb
How useful that is in practice, I don't know. But certainly the
following immutable cycle is useful:
scala> val ones: Stream[Int] = 1 #:: ones
ones: Stream[Int] = Stream(1, ?)
scala> ones.take(10).force
res5: scala.collection.immutable.Stream[Int] = Stream(1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
Here the knot is tied using a call-by-name parameter rather than Miles's trick.
--
Seth Tisue | Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/
-------- Original-Nachricht --------
> Datum: Tue, 10 May 2011 09:59:14 -0400
> Von: Seth Tisue <se...@tisue.net>
> An: scala...@googlegroups.com
> Betreff: Re: [scala-user] Re: Object hierarchies consisting of immutable objects?
> >>>>> "Dennis" == Dennis Haupt <h-s...@gmx.de> writes:
>> var x = List(List(List(1,2,3)))
> 7 :: x.tail
This will not work, x is List[List[List[Int]]] and not List[Int]
This should do the trick, but it’s not pretty:
x map { _ map { 7 :: _.tail } }
Tommaso
-------- Original-Nachricht --------
> Datum: Tue, 10 May 2011 15:37:38 +0100
> Von: "Tommaso Galleri" <Tommaso...@bjss.com>
> An: "Kevin Wright" <kev.lee...@gmail.com>, scala...@googlegroups.com
> Betreff: RE: [scala-user] Re: Object hierarchies consisting of immutable objects?
> <mailto:kev.lee...@gmail.com> mail: kevin....@scalatechnology.com
>
> vibe / skype: kev.lee.wright
>
> quora: http://www.quora.com/Kevin-Wright
> twitter: @thecoda
>
>
>
> "My point today is that, if we wish to count lines of code, we should not
> regard them as "lines produced" but as "lines spent": the current
> conventional wisdom is so foolish as to book that count on the wrong side of the
> ledger" ~ Dijkstra
>
>
>
>
>
-------- Original-Nachricht --------
> Datum: Tue, 10 May 2011 17:10:39 +0200
> Von: "Dennis Haupt" <h-s...@gmx.de>
> An: "Tommaso Galleri" <Tommaso...@bjss.com>, scala...@googlegroups.com, kev.lee...@gmail.com
> Betreff: Re: RE: [scala-user] Re: Object hierarchies consisting of immutable objects?
>> var x = List(List(List(1,2,3)))
> 7 :: x.tail
This will not work, x is List[List[List[Int]]] and not List[Int]
This should do the trick, but it’s not pretty:
x map { _ map { 7 :: _.tail } }
such nested collections are never pretty. composite structures are better for this
trees for example. or tale a look at scala.vector
On 11 May 2011 00:00, "Miguel Negrão" <miguel.ne...@friendlyvirus.org> wrote:
>
>
>
> On Tuesday, May 10, 2011 4:37:38 PM UTC+2, Tommaso Galleri wrote:
>>
>> >> var x = List(List(List(1,2,3)))
>>
>>
>>
>> > 7 :: x.tail
>>
>>
>>
>> This will not work, x is List[List[List[Int]]] and not List[Int]
>>
>>
>>
>> This should do the trick, but it’s not pretty:
>>
>>
>>
>> x map { _ map { 7 :: _.tail } }
>
>
> What about :
>
> x = List(List( (1 to 1000) ))
>
> change element number 756 of the inner list to 0 ?
>
I wouldn't do this. List is optimised for working with successive elements starting at the first. If you need random access, use Vectors.
Having said that...
def zeroElem[T](xs: List[T]) = (xs take 755) ::: ( 0 :: (xs drop 756) )
x map { _ map { zeroElem _ } }
Though `updated` would be clearer here.
basically, you have a nested structure of nodes. but since every element is a node and knows its children, you can solve your problems recursively.
-------- Original-Nachricht --------
> Datum: Tue, 10 May 2011 16:07:02 -0700 (PDT)
> Von: "Miguel Negrão" <miguel.ne...@friendlyvirus.org>
> An: scala...@googlegroups.com
> CC: Dennis Haupt <h-s...@gmx.de>, kev.lee...@gmail.com, Tommaso...@bjss.com
> Betreff: Re: RE: [scala-user] Re: Object hierarchies consisting of immutable objects?
>
>
> What about :
> x = List(List( (1 to 1000) ))
> change element number 756 of the inner list to 0 ?
You would not want to use a List (singly linked list), it’s not efficient to change an element in the middle of the list (a singly linked list is just efficient at one end).
Tommaso
Cheers,
K.