The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Mutating map for Arrays?

3 replies on 1 page. Most recent reply: Jan 29, 2008 3:29 AM by Javier Diaz Soto

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Toby Donaldson

Posts: 8
Nickname: tjd
Registered: Jun, 2003

Mutating map for Arrays? Posted: Dec 27, 2007 7:17 PM
Reply to this message Reply
Advertisement
Is there a more elegant way of adding one to every element of an array (without making a copy) than this:

val arr = new Array[Int](10)
for(i <- 0 until arr.length) arr(i) += 1

If I could make a copy, then arr.map(_ + 1) would work nicely. I can't find a "mutating" map function for Array, e.g. something that would work like this:

arr.mutating_map(_ + 1)


Javier Diaz Soto

Posts: 29
Nickname: javierbds
Registered: Sep, 2005

Re: Mutating map for Arrays? Posted: Jan 29, 2008 3:21 AM
Reply to this message Reply
Well, I haven't found I way of not having a copy, but you do not need to keep it:

a.map(_ + 1).copyToArray(a)

Javier Diaz Soto

Posts: 29
Nickname: javierbds
Registered: Sep, 2005

Re: Mutating map for Arrays? Posted: Jan 29, 2008 3:22 AM
Reply to this message Reply
oops,

a.map(_ + 1).copyToArray(a,0)

Javier Diaz Soto

Posts: 29
Nickname: javierbds
Registered: Sep, 2005

Re: Mutating map for Arrays? Posted: Jan 29, 2008 3:29 AM
Reply to this message Reply
well, even this works (I am a newbie)

a = a.map(_ + 1)

Flat View: This topic has 3 replies on 1 page
Topic: Possible mistakes in first 4 chapters Previous Topic   Next Topic Topic: chapter 4, companion objects

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use