How to create an immutable Map in Scala using foldings?












-4















I am trying to create an immutable hash Map[String, (String, Int)] using the following code:



def genList(xx: String) = {
Seq("one", "two", "three", "four")
}

val oriwords = Set("hello", "how", "are", "you")

val newMap = (Map[String, (String, Int)]() /: oriwords) (
(cmap, currentWord) => {
val xv = 2

genList(currentWord).map(ps => {
val src = cmap get ps

if(src == None) {
cmap + (ps -> ((currentWord, xv)))
}
else {
if(src.get._2 < xv) {
cmap + (ps -> ((currentWord, xv)))
}
else cmap
}

})
}
)


With this code I am getting the following exception:



error: type mismatch;
found : Seq[scala.collection.immutable.Map[String,(String, Int)]]
required: scala.collection.immutable.Map[String,(String, Int)]
genList(currentWord).map(ps => {
^


I know that it is returning a list of Map[String, (String, Int)] as opposed to an update for Map[String, (String, Int)] for the fold operation. Unfortunately,
I am getting any pointers how to fix it. I am very new to Scala.



Expected output is:



scala.collection.immutable.Map[String,(String, Int)] = Map(one -> (are,2), two -> (are,2), three -> (are,2), four -> (are,2))









share|improve this question

























  • Can you provide an example of what your output should look like?

    – Terry Dactyl
    Nov 19 '18 at 9:29











  • Output should be a map[String, (String, Int)]. For example, in this case it would be, scala.collection.immutable.Map[String,(String, Int)] = Map(one -> (are,2), two -> (are,2), three -> (are,2), four -> (are,2))

    – user3243499
    Nov 19 '18 at 9:34











  • updated description as well as comment.

    – user3243499
    Nov 19 '18 at 9:35











  • One way to fix the error is to change the map() call to a fold, but you still might not get the output you're looking for. oriwords is a Set and sets have no defined order, so the order of elements that appear in the fold operation is indeterminate and probably not the order you expect.

    – jwvh
    Nov 19 '18 at 9:48













  • Order is not of concerned. I only intend to get the hash map of key value pairs.

    – user3243499
    Nov 19 '18 at 10:21
















-4















I am trying to create an immutable hash Map[String, (String, Int)] using the following code:



def genList(xx: String) = {
Seq("one", "two", "three", "four")
}

val oriwords = Set("hello", "how", "are", "you")

val newMap = (Map[String, (String, Int)]() /: oriwords) (
(cmap, currentWord) => {
val xv = 2

genList(currentWord).map(ps => {
val src = cmap get ps

if(src == None) {
cmap + (ps -> ((currentWord, xv)))
}
else {
if(src.get._2 < xv) {
cmap + (ps -> ((currentWord, xv)))
}
else cmap
}

})
}
)


With this code I am getting the following exception:



error: type mismatch;
found : Seq[scala.collection.immutable.Map[String,(String, Int)]]
required: scala.collection.immutable.Map[String,(String, Int)]
genList(currentWord).map(ps => {
^


I know that it is returning a list of Map[String, (String, Int)] as opposed to an update for Map[String, (String, Int)] for the fold operation. Unfortunately,
I am getting any pointers how to fix it. I am very new to Scala.



Expected output is:



scala.collection.immutable.Map[String,(String, Int)] = Map(one -> (are,2), two -> (are,2), three -> (are,2), four -> (are,2))









share|improve this question

























  • Can you provide an example of what your output should look like?

    – Terry Dactyl
    Nov 19 '18 at 9:29











  • Output should be a map[String, (String, Int)]. For example, in this case it would be, scala.collection.immutable.Map[String,(String, Int)] = Map(one -> (are,2), two -> (are,2), three -> (are,2), four -> (are,2))

    – user3243499
    Nov 19 '18 at 9:34











  • updated description as well as comment.

    – user3243499
    Nov 19 '18 at 9:35











  • One way to fix the error is to change the map() call to a fold, but you still might not get the output you're looking for. oriwords is a Set and sets have no defined order, so the order of elements that appear in the fold operation is indeterminate and probably not the order you expect.

    – jwvh
    Nov 19 '18 at 9:48













  • Order is not of concerned. I only intend to get the hash map of key value pairs.

    – user3243499
    Nov 19 '18 at 10:21














-4












-4








-4








I am trying to create an immutable hash Map[String, (String, Int)] using the following code:



def genList(xx: String) = {
Seq("one", "two", "three", "four")
}

val oriwords = Set("hello", "how", "are", "you")

val newMap = (Map[String, (String, Int)]() /: oriwords) (
(cmap, currentWord) => {
val xv = 2

genList(currentWord).map(ps => {
val src = cmap get ps

if(src == None) {
cmap + (ps -> ((currentWord, xv)))
}
else {
if(src.get._2 < xv) {
cmap + (ps -> ((currentWord, xv)))
}
else cmap
}

})
}
)


With this code I am getting the following exception:



error: type mismatch;
found : Seq[scala.collection.immutable.Map[String,(String, Int)]]
required: scala.collection.immutable.Map[String,(String, Int)]
genList(currentWord).map(ps => {
^


I know that it is returning a list of Map[String, (String, Int)] as opposed to an update for Map[String, (String, Int)] for the fold operation. Unfortunately,
I am getting any pointers how to fix it. I am very new to Scala.



Expected output is:



scala.collection.immutable.Map[String,(String, Int)] = Map(one -> (are,2), two -> (are,2), three -> (are,2), four -> (are,2))









share|improve this question
















I am trying to create an immutable hash Map[String, (String, Int)] using the following code:



def genList(xx: String) = {
Seq("one", "two", "three", "four")
}

val oriwords = Set("hello", "how", "are", "you")

val newMap = (Map[String, (String, Int)]() /: oriwords) (
(cmap, currentWord) => {
val xv = 2

genList(currentWord).map(ps => {
val src = cmap get ps

if(src == None) {
cmap + (ps -> ((currentWord, xv)))
}
else {
if(src.get._2 < xv) {
cmap + (ps -> ((currentWord, xv)))
}
else cmap
}

})
}
)


With this code I am getting the following exception:



error: type mismatch;
found : Seq[scala.collection.immutable.Map[String,(String, Int)]]
required: scala.collection.immutable.Map[String,(String, Int)]
genList(currentWord).map(ps => {
^


I know that it is returning a list of Map[String, (String, Int)] as opposed to an update for Map[String, (String, Int)] for the fold operation. Unfortunately,
I am getting any pointers how to fix it. I am very new to Scala.



Expected output is:



scala.collection.immutable.Map[String,(String, Int)] = Map(one -> (are,2), two -> (are,2), three -> (are,2), four -> (are,2))






scala






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 '18 at 22:42









halfer

14.4k758109




14.4k758109










asked Nov 19 '18 at 9:03









user3243499user3243499

75611126




75611126













  • Can you provide an example of what your output should look like?

    – Terry Dactyl
    Nov 19 '18 at 9:29











  • Output should be a map[String, (String, Int)]. For example, in this case it would be, scala.collection.immutable.Map[String,(String, Int)] = Map(one -> (are,2), two -> (are,2), three -> (are,2), four -> (are,2))

    – user3243499
    Nov 19 '18 at 9:34











  • updated description as well as comment.

    – user3243499
    Nov 19 '18 at 9:35











  • One way to fix the error is to change the map() call to a fold, but you still might not get the output you're looking for. oriwords is a Set and sets have no defined order, so the order of elements that appear in the fold operation is indeterminate and probably not the order you expect.

    – jwvh
    Nov 19 '18 at 9:48













  • Order is not of concerned. I only intend to get the hash map of key value pairs.

    – user3243499
    Nov 19 '18 at 10:21



















  • Can you provide an example of what your output should look like?

    – Terry Dactyl
    Nov 19 '18 at 9:29











  • Output should be a map[String, (String, Int)]. For example, in this case it would be, scala.collection.immutable.Map[String,(String, Int)] = Map(one -> (are,2), two -> (are,2), three -> (are,2), four -> (are,2))

    – user3243499
    Nov 19 '18 at 9:34











  • updated description as well as comment.

    – user3243499
    Nov 19 '18 at 9:35











  • One way to fix the error is to change the map() call to a fold, but you still might not get the output you're looking for. oriwords is a Set and sets have no defined order, so the order of elements that appear in the fold operation is indeterminate and probably not the order you expect.

    – jwvh
    Nov 19 '18 at 9:48













  • Order is not of concerned. I only intend to get the hash map of key value pairs.

    – user3243499
    Nov 19 '18 at 10:21

















Can you provide an example of what your output should look like?

– Terry Dactyl
Nov 19 '18 at 9:29





Can you provide an example of what your output should look like?

– Terry Dactyl
Nov 19 '18 at 9:29













Output should be a map[String, (String, Int)]. For example, in this case it would be, scala.collection.immutable.Map[String,(String, Int)] = Map(one -> (are,2), two -> (are,2), three -> (are,2), four -> (are,2))

– user3243499
Nov 19 '18 at 9:34





Output should be a map[String, (String, Int)]. For example, in this case it would be, scala.collection.immutable.Map[String,(String, Int)] = Map(one -> (are,2), two -> (are,2), three -> (are,2), four -> (are,2))

– user3243499
Nov 19 '18 at 9:34













updated description as well as comment.

– user3243499
Nov 19 '18 at 9:35





updated description as well as comment.

– user3243499
Nov 19 '18 at 9:35













One way to fix the error is to change the map() call to a fold, but you still might not get the output you're looking for. oriwords is a Set and sets have no defined order, so the order of elements that appear in the fold operation is indeterminate and probably not the order you expect.

– jwvh
Nov 19 '18 at 9:48







One way to fix the error is to change the map() call to a fold, but you still might not get the output you're looking for. oriwords is a Set and sets have no defined order, so the order of elements that appear in the fold operation is indeterminate and probably not the order you expect.

– jwvh
Nov 19 '18 at 9:48















Order is not of concerned. I only intend to get the hash map of key value pairs.

– user3243499
Nov 19 '18 at 10:21





Order is not of concerned. I only intend to get the hash map of key value pairs.

– user3243499
Nov 19 '18 at 10:21












2 Answers
2






active

oldest

votes


















1














Using fold:



def genList(xx: String) = {
Seq("one", "two", "three", "four")
}

val oriwords = Set("hello", "how", "are", "you")

val newMap = (Map[String, (String, Int)]() /: oriwords) (
(cmap, currentWord) => {
val xv = 2

genList(currentWord).map(ps => {
val src = cmap get ps

if(src == None) {
cmap + (ps -> ((currentWord, xv)))
}
else {
if(src.get._2 < xv) {
cmap + (ps -> ((currentWord, xv)))
}
else cmap
}

}).fold(Map[String, (String, Int)]())((a, b) => a ++ b)
}
)
println(newMap) //Map(one -> (hello,2), two -> (hello,2), three -> (hello,2), four -> (hello,2))





share|improve this answer

































    1














    The accumulator your foldLeft returns is of type Seq[Map[String, (String, Int)]] when it should be Map[String, (String, Int)]



    As mentioned above the reason is because you are calling map on a Seq which returns a Seq. You can solve the problem by using foldLeft



      def genList(xx: String) = {
    Seq("one", "two", "three", "four")
    }

    val oriwords = Set("hello", "how", "are", "you")

    val newMap = (Map[String, (String, Int)]() /: oriwords) (
    (cmap, currentWord) => {
    val xv = 2

    genList(currentWord).foldLeft(cmap) {
    (acc, ps) => {
    val src = acc get ps

    if (src == None) {
    acc + (ps -> ((currentWord, xv)))
    }
    else {
    if (src.get._2 < xv) {
    acc + (ps -> ((currentWord, xv)))
    }
    else acc
    }

    }
    }
    }
    )

    println(newMap)

    Map(one -> (hello,2), two -> (hello,2), three -> (hello,2), four -> (hello,2))


    However the output does not match the expected values so you will need to check your logic...






    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',
      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%2f53371286%2fhow-to-create-an-immutable-map-in-scala-using-foldings%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      Using fold:



      def genList(xx: String) = {
      Seq("one", "two", "three", "four")
      }

      val oriwords = Set("hello", "how", "are", "you")

      val newMap = (Map[String, (String, Int)]() /: oriwords) (
      (cmap, currentWord) => {
      val xv = 2

      genList(currentWord).map(ps => {
      val src = cmap get ps

      if(src == None) {
      cmap + (ps -> ((currentWord, xv)))
      }
      else {
      if(src.get._2 < xv) {
      cmap + (ps -> ((currentWord, xv)))
      }
      else cmap
      }

      }).fold(Map[String, (String, Int)]())((a, b) => a ++ b)
      }
      )
      println(newMap) //Map(one -> (hello,2), two -> (hello,2), three -> (hello,2), four -> (hello,2))





      share|improve this answer






























        1














        Using fold:



        def genList(xx: String) = {
        Seq("one", "two", "three", "four")
        }

        val oriwords = Set("hello", "how", "are", "you")

        val newMap = (Map[String, (String, Int)]() /: oriwords) (
        (cmap, currentWord) => {
        val xv = 2

        genList(currentWord).map(ps => {
        val src = cmap get ps

        if(src == None) {
        cmap + (ps -> ((currentWord, xv)))
        }
        else {
        if(src.get._2 < xv) {
        cmap + (ps -> ((currentWord, xv)))
        }
        else cmap
        }

        }).fold(Map[String, (String, Int)]())((a, b) => a ++ b)
        }
        )
        println(newMap) //Map(one -> (hello,2), two -> (hello,2), three -> (hello,2), four -> (hello,2))





        share|improve this answer




























          1












          1








          1







          Using fold:



          def genList(xx: String) = {
          Seq("one", "two", "three", "four")
          }

          val oriwords = Set("hello", "how", "are", "you")

          val newMap = (Map[String, (String, Int)]() /: oriwords) (
          (cmap, currentWord) => {
          val xv = 2

          genList(currentWord).map(ps => {
          val src = cmap get ps

          if(src == None) {
          cmap + (ps -> ((currentWord, xv)))
          }
          else {
          if(src.get._2 < xv) {
          cmap + (ps -> ((currentWord, xv)))
          }
          else cmap
          }

          }).fold(Map[String, (String, Int)]())((a, b) => a ++ b)
          }
          )
          println(newMap) //Map(one -> (hello,2), two -> (hello,2), three -> (hello,2), four -> (hello,2))





          share|improve this answer















          Using fold:



          def genList(xx: String) = {
          Seq("one", "two", "three", "four")
          }

          val oriwords = Set("hello", "how", "are", "you")

          val newMap = (Map[String, (String, Int)]() /: oriwords) (
          (cmap, currentWord) => {
          val xv = 2

          genList(currentWord).map(ps => {
          val src = cmap get ps

          if(src == None) {
          cmap + (ps -> ((currentWord, xv)))
          }
          else {
          if(src.get._2 < xv) {
          cmap + (ps -> ((currentWord, xv)))
          }
          else cmap
          }

          }).fold(Map[String, (String, Int)]())((a, b) => a ++ b)
          }
          )
          println(newMap) //Map(one -> (hello,2), two -> (hello,2), three -> (hello,2), four -> (hello,2))






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 10:06

























          answered Nov 19 '18 at 9:51









          Raman MishraRaman Mishra

          1,2741417




          1,2741417

























              1














              The accumulator your foldLeft returns is of type Seq[Map[String, (String, Int)]] when it should be Map[String, (String, Int)]



              As mentioned above the reason is because you are calling map on a Seq which returns a Seq. You can solve the problem by using foldLeft



                def genList(xx: String) = {
              Seq("one", "two", "three", "four")
              }

              val oriwords = Set("hello", "how", "are", "you")

              val newMap = (Map[String, (String, Int)]() /: oriwords) (
              (cmap, currentWord) => {
              val xv = 2

              genList(currentWord).foldLeft(cmap) {
              (acc, ps) => {
              val src = acc get ps

              if (src == None) {
              acc + (ps -> ((currentWord, xv)))
              }
              else {
              if (src.get._2 < xv) {
              acc + (ps -> ((currentWord, xv)))
              }
              else acc
              }

              }
              }
              }
              )

              println(newMap)

              Map(one -> (hello,2), two -> (hello,2), three -> (hello,2), four -> (hello,2))


              However the output does not match the expected values so you will need to check your logic...






              share|improve this answer






























                1














                The accumulator your foldLeft returns is of type Seq[Map[String, (String, Int)]] when it should be Map[String, (String, Int)]



                As mentioned above the reason is because you are calling map on a Seq which returns a Seq. You can solve the problem by using foldLeft



                  def genList(xx: String) = {
                Seq("one", "two", "three", "four")
                }

                val oriwords = Set("hello", "how", "are", "you")

                val newMap = (Map[String, (String, Int)]() /: oriwords) (
                (cmap, currentWord) => {
                val xv = 2

                genList(currentWord).foldLeft(cmap) {
                (acc, ps) => {
                val src = acc get ps

                if (src == None) {
                acc + (ps -> ((currentWord, xv)))
                }
                else {
                if (src.get._2 < xv) {
                acc + (ps -> ((currentWord, xv)))
                }
                else acc
                }

                }
                }
                }
                )

                println(newMap)

                Map(one -> (hello,2), two -> (hello,2), three -> (hello,2), four -> (hello,2))


                However the output does not match the expected values so you will need to check your logic...






                share|improve this answer




























                  1












                  1








                  1







                  The accumulator your foldLeft returns is of type Seq[Map[String, (String, Int)]] when it should be Map[String, (String, Int)]



                  As mentioned above the reason is because you are calling map on a Seq which returns a Seq. You can solve the problem by using foldLeft



                    def genList(xx: String) = {
                  Seq("one", "two", "three", "four")
                  }

                  val oriwords = Set("hello", "how", "are", "you")

                  val newMap = (Map[String, (String, Int)]() /: oriwords) (
                  (cmap, currentWord) => {
                  val xv = 2

                  genList(currentWord).foldLeft(cmap) {
                  (acc, ps) => {
                  val src = acc get ps

                  if (src == None) {
                  acc + (ps -> ((currentWord, xv)))
                  }
                  else {
                  if (src.get._2 < xv) {
                  acc + (ps -> ((currentWord, xv)))
                  }
                  else acc
                  }

                  }
                  }
                  }
                  )

                  println(newMap)

                  Map(one -> (hello,2), two -> (hello,2), three -> (hello,2), four -> (hello,2))


                  However the output does not match the expected values so you will need to check your logic...






                  share|improve this answer















                  The accumulator your foldLeft returns is of type Seq[Map[String, (String, Int)]] when it should be Map[String, (String, Int)]



                  As mentioned above the reason is because you are calling map on a Seq which returns a Seq. You can solve the problem by using foldLeft



                    def genList(xx: String) = {
                  Seq("one", "two", "three", "four")
                  }

                  val oriwords = Set("hello", "how", "are", "you")

                  val newMap = (Map[String, (String, Int)]() /: oriwords) (
                  (cmap, currentWord) => {
                  val xv = 2

                  genList(currentWord).foldLeft(cmap) {
                  (acc, ps) => {
                  val src = acc get ps

                  if (src == None) {
                  acc + (ps -> ((currentWord, xv)))
                  }
                  else {
                  if (src.get._2 < xv) {
                  acc + (ps -> ((currentWord, xv)))
                  }
                  else acc
                  }

                  }
                  }
                  }
                  )

                  println(newMap)

                  Map(one -> (hello,2), two -> (hello,2), three -> (hello,2), four -> (hello,2))


                  However the output does not match the expected values so you will need to check your logic...







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 19 '18 at 10:25

























                  answered Nov 19 '18 at 9:59









                  Terry DactylTerry Dactyl

                  1,104412




                  1,104412






























                      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%2f53371286%2fhow-to-create-an-immutable-map-in-scala-using-foldings%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

                      How to send String Array data to Server using php in android

                      Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

                      Is anime1.com a legal site for watching anime?