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?










share|improve this question



























    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?










    share|improve this question

























      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?










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 20:34









      Tzach Zohar

      28.2k33754




      28.2k33754










      asked Nov 9 at 20:32









      Balaji Krishnamoorthy

      12




      12






















          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)





          share|improve this answer



























            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)





            share|improve this answer



























              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





              share|improve this answer



























                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).






                share|improve this answer




















                  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',
                  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%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

























                  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)





                  share|improve this answer
























                    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)





                    share|improve this answer






















                      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)





                      share|improve this answer












                      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)






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 9 at 20:39









                      Tzach Zohar

                      28.2k33754




                      28.2k33754






















                          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)





                          share|improve this answer
























                            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)





                            share|improve this answer






















                              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)





                              share|improve this answer












                              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)






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 9 at 20:49









                              Berthur

                              4908




                              4908




















                                  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





                                  share|improve this answer
























                                    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





                                    share|improve this answer






















                                      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





                                      share|improve this answer












                                      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






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 9 at 20:42









                                      Dima

                                      23.3k32234




                                      23.3k32234




















                                          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).






                                          share|improve this answer
























                                            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).






                                            share|improve this answer






















                                              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).






                                              share|improve this answer












                                              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).







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 10 at 1:33









                                              swapnil shashank

                                              625




                                              625



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  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





















































                                                  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

                                                  Use pre created SQLite database for Android project in kotlin

                                                  Darth Vader #20

                                                  Ondo