On 18 February 2011 14:12, Chandrashekar Babu <[email protected]>wrote:
> Hi, > > On 18/02/11 10:49 AM, openbala wrote: > > The gist of immutability is that any operations on a data structure > > should not change its data structure. (for e.g. java's String class is > > immutable) > > But the List implementations (LinkedList and Vector for example) in > Java are mutable. So are List implementations in C++ STL. > > > The general consensus is that mutability is bad for programming. YMMV. > > Any data-structure that goes beyond the simplicity of a String must > be mutable irrespective of the programming language (of course with > some possible exceptions). > There is no such hard and fast rule. > If lists were not mutable, how would you insert, remove or modify > an element in a list ? The very purpose of using a list is to allow > someone to add, remove or update elements on it right ? > Complex data structures can be made immutable without needing to copy the entire structure every time a modification is made. Only those parts that actually change need to be copied. The unchanged part is shared between the new and old structures. This ensures that the complexity of algorithms remain the same irrespective of the mutability issue. > > Coming back to the original context where list.reverse() modified > the original list, there is also reversed(list) function that > returns a list iterator that allow you to traverse a list in reverse > order without modifying the original list - right design for the right > purpose :-) > > Cheers, > Chandrashekar. > > -- > http://www.chandrashekar.info/ > http://www.slashprog.com/ > _______________________________________________ > ILUGC Mailing List: > http://www.ae.iitm.ac.in/mailman/listinfo/ilugc > _______________________________________________ ILUGC Mailing List: http://www.ae.iitm.ac.in/mailman/listinfo/ilugc
