Scala - create a new list and update particular element from existing list
up vote
0
down vote
favorite
I am new to Scala and new OOP too. How can I update a particular element in a list while creating a new list.
val numbers= List(1,2,3,4,5)
val result = numbers.map(_*2)
I need to update third element only -> multiply by 2. How can I do that by using map
?
scala
add a comment |
up vote
0
down vote
favorite
I am new to Scala and new OOP too. How can I update a particular element in a list while creating a new list.
val numbers= List(1,2,3,4,5)
val result = numbers.map(_*2)
I need to update third element only -> multiply by 2. How can I do that by using map
?
scala
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am new to Scala and new OOP too. How can I update a particular element in a list while creating a new list.
val numbers= List(1,2,3,4,5)
val result = numbers.map(_*2)
I need to update third element only -> multiply by 2. How can I do that by using map
?
scala
I am new to Scala and new OOP too. How can I update a particular element in a list while creating a new list.
val numbers= List(1,2,3,4,5)
val result = numbers.map(_*2)
I need to update third element only -> multiply by 2. How can I do that by using map
?
scala
scala
edited Nov 9 at 20:34
Tzach Zohar
28.2k33754
28.2k33754
asked Nov 9 at 20:32
Balaji Krishnamoorthy
12
12
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
up vote
1
down vote
You can use zipWithIndex
to map the list into a list of tuples, where each element is accompanied by its index. Then, using map
with pattern matching - you single out the third element (index = 2):
val numbers = List(1,2,3,4,5)
val result = numbers.zipWithIndex.map
case (v, i) if i == 2 => v * 2
case (v, _) => v
// result: List[Int] = List(1, 2, 6, 4, 5)
Alternatively - you can use patch
, which replaces a sub-sequence with a provided one:
numbers.patch(from = 2, patch = Seq(numbers(2) * 2), replaced = 1)
add a comment |
up vote
1
down vote
I think the clearest way of achieving this is by using updated(index: Int, elem: Int)
. For your example, it could be applied as follows:
val result = numbers.updated(2, numbers(2) * 2)
add a comment |
up vote
0
down vote
list.zipWithIndex
creates a list of pairs with original element on the left, and index in the list on the right (indices are 0-based, so "third element" is at index 2).
val result = number.zipWithIndex.map
case (n, 2) => n*2
case n => n
This creates an intermediate list holding the pairs, and then maps through it to do your transformation. A bit more efficient approach is to use iterator
. Iterators a 'lazy', so, rather than creating an intermediate container, it will generate the pairs one-by-one, and send them straight to the .map
:
val result = number.iterator.zipWithIndex.map
case (n, 2) => n*2
case n => n
.toList
add a comment |
up vote
0
down vote
1st and the foremost scala is FOP and not OOP. You can update any element of a list through the keyword "updated", see the following example for details:
Signature :- updated(index,value)
val numbers= List(1,2,3,4,5)
print(numbers.updated(2,10))
Now here the 1st argument is the index and the 2nd argument is the value. The result of this code will modify the list to:
List(1, 2, 10, 4, 5).
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can use zipWithIndex
to map the list into a list of tuples, where each element is accompanied by its index. Then, using map
with pattern matching - you single out the third element (index = 2):
val numbers = List(1,2,3,4,5)
val result = numbers.zipWithIndex.map
case (v, i) if i == 2 => v * 2
case (v, _) => v
// result: List[Int] = List(1, 2, 6, 4, 5)
Alternatively - you can use patch
, which replaces a sub-sequence with a provided one:
numbers.patch(from = 2, patch = Seq(numbers(2) * 2), replaced = 1)
add a comment |
up vote
1
down vote
You can use zipWithIndex
to map the list into a list of tuples, where each element is accompanied by its index. Then, using map
with pattern matching - you single out the third element (index = 2):
val numbers = List(1,2,3,4,5)
val result = numbers.zipWithIndex.map
case (v, i) if i == 2 => v * 2
case (v, _) => v
// result: List[Int] = List(1, 2, 6, 4, 5)
Alternatively - you can use patch
, which replaces a sub-sequence with a provided one:
numbers.patch(from = 2, patch = Seq(numbers(2) * 2), replaced = 1)
add a comment |
up vote
1
down vote
up vote
1
down vote
You can use zipWithIndex
to map the list into a list of tuples, where each element is accompanied by its index. Then, using map
with pattern matching - you single out the third element (index = 2):
val numbers = List(1,2,3,4,5)
val result = numbers.zipWithIndex.map
case (v, i) if i == 2 => v * 2
case (v, _) => v
// result: List[Int] = List(1, 2, 6, 4, 5)
Alternatively - you can use patch
, which replaces a sub-sequence with a provided one:
numbers.patch(from = 2, patch = Seq(numbers(2) * 2), replaced = 1)
You can use zipWithIndex
to map the list into a list of tuples, where each element is accompanied by its index. Then, using map
with pattern matching - you single out the third element (index = 2):
val numbers = List(1,2,3,4,5)
val result = numbers.zipWithIndex.map
case (v, i) if i == 2 => v * 2
case (v, _) => v
// result: List[Int] = List(1, 2, 6, 4, 5)
Alternatively - you can use patch
, which replaces a sub-sequence with a provided one:
numbers.patch(from = 2, patch = Seq(numbers(2) * 2), replaced = 1)
answered Nov 9 at 20:39
Tzach Zohar
28.2k33754
28.2k33754
add a comment |
add a comment |
up vote
1
down vote
I think the clearest way of achieving this is by using updated(index: Int, elem: Int)
. For your example, it could be applied as follows:
val result = numbers.updated(2, numbers(2) * 2)
add a comment |
up vote
1
down vote
I think the clearest way of achieving this is by using updated(index: Int, elem: Int)
. For your example, it could be applied as follows:
val result = numbers.updated(2, numbers(2) * 2)
add a comment |
up vote
1
down vote
up vote
1
down vote
I think the clearest way of achieving this is by using updated(index: Int, elem: Int)
. For your example, it could be applied as follows:
val result = numbers.updated(2, numbers(2) * 2)
I think the clearest way of achieving this is by using updated(index: Int, elem: Int)
. For your example, it could be applied as follows:
val result = numbers.updated(2, numbers(2) * 2)
answered Nov 9 at 20:49
Berthur
4908
4908
add a comment |
add a comment |
up vote
0
down vote
list.zipWithIndex
creates a list of pairs with original element on the left, and index in the list on the right (indices are 0-based, so "third element" is at index 2).
val result = number.zipWithIndex.map
case (n, 2) => n*2
case n => n
This creates an intermediate list holding the pairs, and then maps through it to do your transformation. A bit more efficient approach is to use iterator
. Iterators a 'lazy', so, rather than creating an intermediate container, it will generate the pairs one-by-one, and send them straight to the .map
:
val result = number.iterator.zipWithIndex.map
case (n, 2) => n*2
case n => n
.toList
add a comment |
up vote
0
down vote
list.zipWithIndex
creates a list of pairs with original element on the left, and index in the list on the right (indices are 0-based, so "third element" is at index 2).
val result = number.zipWithIndex.map
case (n, 2) => n*2
case n => n
This creates an intermediate list holding the pairs, and then maps through it to do your transformation. A bit more efficient approach is to use iterator
. Iterators a 'lazy', so, rather than creating an intermediate container, it will generate the pairs one-by-one, and send them straight to the .map
:
val result = number.iterator.zipWithIndex.map
case (n, 2) => n*2
case n => n
.toList
add a comment |
up vote
0
down vote
up vote
0
down vote
list.zipWithIndex
creates a list of pairs with original element on the left, and index in the list on the right (indices are 0-based, so "third element" is at index 2).
val result = number.zipWithIndex.map
case (n, 2) => n*2
case n => n
This creates an intermediate list holding the pairs, and then maps through it to do your transformation. A bit more efficient approach is to use iterator
. Iterators a 'lazy', so, rather than creating an intermediate container, it will generate the pairs one-by-one, and send them straight to the .map
:
val result = number.iterator.zipWithIndex.map
case (n, 2) => n*2
case n => n
.toList
list.zipWithIndex
creates a list of pairs with original element on the left, and index in the list on the right (indices are 0-based, so "third element" is at index 2).
val result = number.zipWithIndex.map
case (n, 2) => n*2
case n => n
This creates an intermediate list holding the pairs, and then maps through it to do your transformation. A bit more efficient approach is to use iterator
. Iterators a 'lazy', so, rather than creating an intermediate container, it will generate the pairs one-by-one, and send them straight to the .map
:
val result = number.iterator.zipWithIndex.map
case (n, 2) => n*2
case n => n
.toList
answered Nov 9 at 20:42
Dima
23.3k32234
23.3k32234
add a comment |
add a comment |
up vote
0
down vote
1st and the foremost scala is FOP and not OOP. You can update any element of a list through the keyword "updated", see the following example for details:
Signature :- updated(index,value)
val numbers= List(1,2,3,4,5)
print(numbers.updated(2,10))
Now here the 1st argument is the index and the 2nd argument is the value. The result of this code will modify the list to:
List(1, 2, 10, 4, 5).
add a comment |
up vote
0
down vote
1st and the foremost scala is FOP and not OOP. You can update any element of a list through the keyword "updated", see the following example for details:
Signature :- updated(index,value)
val numbers= List(1,2,3,4,5)
print(numbers.updated(2,10))
Now here the 1st argument is the index and the 2nd argument is the value. The result of this code will modify the list to:
List(1, 2, 10, 4, 5).
add a comment |
up vote
0
down vote
up vote
0
down vote
1st and the foremost scala is FOP and not OOP. You can update any element of a list through the keyword "updated", see the following example for details:
Signature :- updated(index,value)
val numbers= List(1,2,3,4,5)
print(numbers.updated(2,10))
Now here the 1st argument is the index and the 2nd argument is the value. The result of this code will modify the list to:
List(1, 2, 10, 4, 5).
1st and the foremost scala is FOP and not OOP. You can update any element of a list through the keyword "updated", see the following example for details:
Signature :- updated(index,value)
val numbers= List(1,2,3,4,5)
print(numbers.updated(2,10))
Now here the 1st argument is the index and the 2nd argument is the value. The result of this code will modify the list to:
List(1, 2, 10, 4, 5).
answered Nov 10 at 1:33
swapnil shashank
625
625
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232888%2fscala-create-a-new-list-and-update-particular-element-from-existing-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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