Scala function that accepts array argument and returns a mutated array2019 Community Moderator ElectionIs there a NumPy function to return the first index of something in an array?Converting an array to a function arguments listDifference between Array and List in scalaWhat is the apply function in Scala?Covariance of a passed function argumentHow to get distinct values from an array of objects in JavaScript?Why not inherit from List<T>?How do you return an array in a method?Scala ambiguity with paren-less function callsScala: type arguments do not conform to trait Subtractable's type parameter bounds

Is it illegal in Germany to take sick leave if you caused your own illness with food?

Am I not good enough for you?

validation vs test vs training accuracy, which one to compare for claiming overfit?

What is the definition of "Natural Selection"?

Touchscreen-controlled dentist office snowman collector game

How does Dispel Magic work against Stoneskin?

Can infringement of a trademark be pursued for using a company's name in a sentence?

What Happens when Passenger Refuses to Fly Boeing 737 Max?

"One can do his homework in the library"

Rejected in 4th interview round citing insufficient years of experience

Co-worker team leader wants to inject the crap software product of his friends into our development. What should I say to our common boss?

Coworker uses her breast-pump everywhere in the office

Can the druid cantrip Thorn Whip really defeat a water weird this easily?

Potentiometer like component

If the Captain's screens are out, does he switch seats with the co-pilot?

Welcoming 2019 Pi day: How to draw the letter π?

Should QA ask requirements to developers?

It's a yearly task, alright

Is it true that real estate prices mainly go up?

Can you reject a postdoc offer after the PI has paid a large sum for flights/accommodation for your visit?

My adviser wants to be the first author

"However" used in a conditional clause?

Make a transparent 448*448 image

How could a female member of a species produce eggs unto death?



Scala function that accepts array argument and returns a mutated array



2019 Community Moderator ElectionIs there a NumPy function to return the first index of something in an array?Converting an array to a function arguments listDifference between Array and List in scalaWhat is the apply function in Scala?Covariance of a passed function argumentHow to get distinct values from an array of objects in JavaScript?Why not inherit from List<T>?How do you return an array in a method?Scala ambiguity with paren-less function callsScala: type arguments do not conform to trait Subtractable's type parameter bounds










0















I would like to figure out the most pragmatic way to accept an array (or list) and append to the data structure. Then finally return the new data structure.



Something like this:



 def template(array: Array[String]): Array[Nothing] = 
val staging_path = "s3//clone-staging/"
var path_list = Array()
//iterate through each of the items in the array and append to the new string.
for(outputString <- array)
var new_path = staging_path.toString + outputString
println(new_path)
//path_list I thought would add these new staging_path to the array
path_list +: new_path


path_list(4)



However, calling a single index of the data structure as a shanty way of checking existence, path_list(4) returns an Out of Bounds.



Thanks.










share|improve this question



















  • 2





    I'm not sure what the path_list(4) is doing there, but a much more idiomatic solution would be something like def template(values: List[String]) = values.map("s3//clone-staging" + _).

    – Travis Brown
    Mar 6 at 17:46











  • Just trying to return a single index from the new data structure as a way to check if it exists or not.

    – tjmorri7
    Mar 6 at 17:49















0















I would like to figure out the most pragmatic way to accept an array (or list) and append to the data structure. Then finally return the new data structure.



Something like this:



 def template(array: Array[String]): Array[Nothing] = 
val staging_path = "s3//clone-staging/"
var path_list = Array()
//iterate through each of the items in the array and append to the new string.
for(outputString <- array)
var new_path = staging_path.toString + outputString
println(new_path)
//path_list I thought would add these new staging_path to the array
path_list +: new_path


path_list(4)



However, calling a single index of the data structure as a shanty way of checking existence, path_list(4) returns an Out of Bounds.



Thanks.










share|improve this question



















  • 2





    I'm not sure what the path_list(4) is doing there, but a much more idiomatic solution would be something like def template(values: List[String]) = values.map("s3//clone-staging" + _).

    – Travis Brown
    Mar 6 at 17:46











  • Just trying to return a single index from the new data structure as a way to check if it exists or not.

    – tjmorri7
    Mar 6 at 17:49













0












0








0








I would like to figure out the most pragmatic way to accept an array (or list) and append to the data structure. Then finally return the new data structure.



Something like this:



 def template(array: Array[String]): Array[Nothing] = 
val staging_path = "s3//clone-staging/"
var path_list = Array()
//iterate through each of the items in the array and append to the new string.
for(outputString <- array)
var new_path = staging_path.toString + outputString
println(new_path)
//path_list I thought would add these new staging_path to the array
path_list +: new_path


path_list(4)



However, calling a single index of the data structure as a shanty way of checking existence, path_list(4) returns an Out of Bounds.



Thanks.










share|improve this question
















I would like to figure out the most pragmatic way to accept an array (or list) and append to the data structure. Then finally return the new data structure.



Something like this:



 def template(array: Array[String]): Array[Nothing] = 
val staging_path = "s3//clone-staging/"
var path_list = Array()
//iterate through each of the items in the array and append to the new string.
for(outputString <- array)
var new_path = staging_path.toString + outputString
println(new_path)
//path_list I thought would add these new staging_path to the array
path_list +: new_path


path_list(4)



However, calling a single index of the data structure as a shanty way of checking existence, path_list(4) returns an Out of Bounds.



Thanks.







arrays scala list string-interpolation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 6 at 20:23







tjmorri7

















asked Mar 6 at 17:34









tjmorri7tjmorri7

34




34







  • 2





    I'm not sure what the path_list(4) is doing there, but a much more idiomatic solution would be something like def template(values: List[String]) = values.map("s3//clone-staging" + _).

    – Travis Brown
    Mar 6 at 17:46











  • Just trying to return a single index from the new data structure as a way to check if it exists or not.

    – tjmorri7
    Mar 6 at 17:49












  • 2





    I'm not sure what the path_list(4) is doing there, but a much more idiomatic solution would be something like def template(values: List[String]) = values.map("s3//clone-staging" + _).

    – Travis Brown
    Mar 6 at 17:46











  • Just trying to return a single index from the new data structure as a way to check if it exists or not.

    – tjmorri7
    Mar 6 at 17:49







2




2





I'm not sure what the path_list(4) is doing there, but a much more idiomatic solution would be something like def template(values: List[String]) = values.map("s3//clone-staging" + _).

– Travis Brown
Mar 6 at 17:46





I'm not sure what the path_list(4) is doing there, but a much more idiomatic solution would be something like def template(values: List[String]) = values.map("s3//clone-staging" + _).

– Travis Brown
Mar 6 at 17:46













Just trying to return a single index from the new data structure as a way to check if it exists or not.

– tjmorri7
Mar 6 at 17:49





Just trying to return a single index from the new data structure as a way to check if it exists or not.

– tjmorri7
Mar 6 at 17:49












1 Answer
1






active

oldest

votes


















1














I think you just want to use map here:



val staging_path = "s3//clone-staging/"
val dirs = Array("one", "two", "three", "four", "five")
val paths = dirs.map(dir => staging_path + dir)
println(paths)
// result: paths: Array[String] = Array(s3//clone-staging/one, s3//clone-staging/two, s3//clone-staging/three, s3//clone-staging/four, s3//clone-staging/five)
println(paths.length)
// result: 5


In functional programming land you are generally trying to avoid mutations. Instead, think of it as transforming your input array into a new array.






share|improve this answer























  • Avoiding the original mutation of the data structure is what I was attempting by creating the local var path_list = Array()

    – tjmorri7
    Mar 6 at 18:24











  • @tjmorri7 if this solved your problem, please consider accepting this answer. If it does not, for whatever reason, please leave a comment explaining why it does not, so the answer could be improved. - Take a look to What should I do when someone answers my question?.

    – Luis Miguel Mejía Suárez
    Mar 6 at 18:43










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55029046%2fscala-function-that-accepts-array-argument-and-returns-a-mutated-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














I think you just want to use map here:



val staging_path = "s3//clone-staging/"
val dirs = Array("one", "two", "three", "four", "five")
val paths = dirs.map(dir => staging_path + dir)
println(paths)
// result: paths: Array[String] = Array(s3//clone-staging/one, s3//clone-staging/two, s3//clone-staging/three, s3//clone-staging/four, s3//clone-staging/five)
println(paths.length)
// result: 5


In functional programming land you are generally trying to avoid mutations. Instead, think of it as transforming your input array into a new array.






share|improve this answer























  • Avoiding the original mutation of the data structure is what I was attempting by creating the local var path_list = Array()

    – tjmorri7
    Mar 6 at 18:24











  • @tjmorri7 if this solved your problem, please consider accepting this answer. If it does not, for whatever reason, please leave a comment explaining why it does not, so the answer could be improved. - Take a look to What should I do when someone answers my question?.

    – Luis Miguel Mejía Suárez
    Mar 6 at 18:43















1














I think you just want to use map here:



val staging_path = "s3//clone-staging/"
val dirs = Array("one", "two", "three", "four", "five")
val paths = dirs.map(dir => staging_path + dir)
println(paths)
// result: paths: Array[String] = Array(s3//clone-staging/one, s3//clone-staging/two, s3//clone-staging/three, s3//clone-staging/four, s3//clone-staging/five)
println(paths.length)
// result: 5


In functional programming land you are generally trying to avoid mutations. Instead, think of it as transforming your input array into a new array.






share|improve this answer























  • Avoiding the original mutation of the data structure is what I was attempting by creating the local var path_list = Array()

    – tjmorri7
    Mar 6 at 18:24











  • @tjmorri7 if this solved your problem, please consider accepting this answer. If it does not, for whatever reason, please leave a comment explaining why it does not, so the answer could be improved. - Take a look to What should I do when someone answers my question?.

    – Luis Miguel Mejía Suárez
    Mar 6 at 18:43













1












1








1







I think you just want to use map here:



val staging_path = "s3//clone-staging/"
val dirs = Array("one", "two", "three", "four", "five")
val paths = dirs.map(dir => staging_path + dir)
println(paths)
// result: paths: Array[String] = Array(s3//clone-staging/one, s3//clone-staging/two, s3//clone-staging/three, s3//clone-staging/four, s3//clone-staging/five)
println(paths.length)
// result: 5


In functional programming land you are generally trying to avoid mutations. Instead, think of it as transforming your input array into a new array.






share|improve this answer













I think you just want to use map here:



val staging_path = "s3//clone-staging/"
val dirs = Array("one", "two", "three", "four", "five")
val paths = dirs.map(dir => staging_path + dir)
println(paths)
// result: paths: Array[String] = Array(s3//clone-staging/one, s3//clone-staging/two, s3//clone-staging/three, s3//clone-staging/four, s3//clone-staging/five)
println(paths.length)
// result: 5


In functional programming land you are generally trying to avoid mutations. Instead, think of it as transforming your input array into a new array.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 6 at 18:14









Kit MenkeKit Menke

6,56112752




6,56112752












  • Avoiding the original mutation of the data structure is what I was attempting by creating the local var path_list = Array()

    – tjmorri7
    Mar 6 at 18:24











  • @tjmorri7 if this solved your problem, please consider accepting this answer. If it does not, for whatever reason, please leave a comment explaining why it does not, so the answer could be improved. - Take a look to What should I do when someone answers my question?.

    – Luis Miguel Mejía Suárez
    Mar 6 at 18:43

















  • Avoiding the original mutation of the data structure is what I was attempting by creating the local var path_list = Array()

    – tjmorri7
    Mar 6 at 18:24











  • @tjmorri7 if this solved your problem, please consider accepting this answer. If it does not, for whatever reason, please leave a comment explaining why it does not, so the answer could be improved. - Take a look to What should I do when someone answers my question?.

    – Luis Miguel Mejía Suárez
    Mar 6 at 18:43
















Avoiding the original mutation of the data structure is what I was attempting by creating the local var path_list = Array()

– tjmorri7
Mar 6 at 18:24





Avoiding the original mutation of the data structure is what I was attempting by creating the local var path_list = Array()

– tjmorri7
Mar 6 at 18:24













@tjmorri7 if this solved your problem, please consider accepting this answer. If it does not, for whatever reason, please leave a comment explaining why it does not, so the answer could be improved. - Take a look to What should I do when someone answers my question?.

– Luis Miguel Mejía Suárez
Mar 6 at 18:43





@tjmorri7 if this solved your problem, please consider accepting this answer. If it does not, for whatever reason, please leave a comment explaining why it does not, so the answer could be improved. - Take a look to What should I do when someone answers my question?.

– Luis Miguel Mejía Suárez
Mar 6 at 18:43



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55029046%2fscala-function-that-accepts-array-argument-and-returns-a-mutated-array%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved