Why this foldLeft scala code does not work?











up vote
0
down vote

favorite












The following codesnippet is upposed to get me a hash map of Map[String, (String, Int)].



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 -> (w, xv))
}
else {
if(src.get._2 < xv) {
cmap + (ps -> (w, xv))
}
else cmap
}

})
}
)


But I am getting the following error:



error: too many arguments for method ->: (y: B)(String, B)
cmap + (ps -> (w, xv))
^


Update: With the suggested changes that are mentioned in the answers, the above error is removed.



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
}

})
}
)


But now getting a new error on the above code as follows:



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









share|improve this question




















  • 1




    Compiler sometimes gets confused with the implicit class that defines the -> method. You're usually better off changing a -> b to (a, b). You have more errors in this code, but this should take care of the weird "too many arguments" message that you're seeing.
    – slouc
    Nov 18 at 19:07






  • 2




    Please do not edit your question in such a way that existing answers are invalidated. If you have a question, post a question.
    – Jörg W Mittag
    Nov 18 at 20:00










  • you are giving Map[String,[(String, Int)] as the initial value but you are iterating over list and creating map inside it so it's returning you List[Map[String,(String,Int)]].
    – Raman Mishra
    Nov 18 at 20:07










  • Every iteration of the fold, /:, needs to return a Map[_,_] but genList() is returning a Seq[_], which you are mapping over to create a Seq[Map[_,_]], which is not what what the fold requires.
    – jwvh
    Nov 19 at 2:32















up vote
0
down vote

favorite












The following codesnippet is upposed to get me a hash map of Map[String, (String, Int)].



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 -> (w, xv))
}
else {
if(src.get._2 < xv) {
cmap + (ps -> (w, xv))
}
else cmap
}

})
}
)


But I am getting the following error:



error: too many arguments for method ->: (y: B)(String, B)
cmap + (ps -> (w, xv))
^


Update: With the suggested changes that are mentioned in the answers, the above error is removed.



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
}

})
}
)


But now getting a new error on the above code as follows:



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









share|improve this question




















  • 1




    Compiler sometimes gets confused with the implicit class that defines the -> method. You're usually better off changing a -> b to (a, b). You have more errors in this code, but this should take care of the weird "too many arguments" message that you're seeing.
    – slouc
    Nov 18 at 19:07






  • 2




    Please do not edit your question in such a way that existing answers are invalidated. If you have a question, post a question.
    – Jörg W Mittag
    Nov 18 at 20:00










  • you are giving Map[String,[(String, Int)] as the initial value but you are iterating over list and creating map inside it so it's returning you List[Map[String,(String,Int)]].
    – Raman Mishra
    Nov 18 at 20:07










  • Every iteration of the fold, /:, needs to return a Map[_,_] but genList() is returning a Seq[_], which you are mapping over to create a Seq[Map[_,_]], which is not what what the fold requires.
    – jwvh
    Nov 19 at 2:32













up vote
0
down vote

favorite









up vote
0
down vote

favorite











The following codesnippet is upposed to get me a hash map of Map[String, (String, Int)].



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 -> (w, xv))
}
else {
if(src.get._2 < xv) {
cmap + (ps -> (w, xv))
}
else cmap
}

})
}
)


But I am getting the following error:



error: too many arguments for method ->: (y: B)(String, B)
cmap + (ps -> (w, xv))
^


Update: With the suggested changes that are mentioned in the answers, the above error is removed.



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
}

})
}
)


But now getting a new error on the above code as follows:



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









share|improve this question















The following codesnippet is upposed to get me a hash map of Map[String, (String, Int)].



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 -> (w, xv))
}
else {
if(src.get._2 < xv) {
cmap + (ps -> (w, xv))
}
else cmap
}

})
}
)


But I am getting the following error:



error: too many arguments for method ->: (y: B)(String, B)
cmap + (ps -> (w, xv))
^


Update: With the suggested changes that are mentioned in the answers, the above error is removed.



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
}

})
}
)


But now getting a new error on the above code as follows:



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






scala






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 19 at 22:42









halfer

14.2k758106




14.2k758106










asked Nov 18 at 18:40









user3243499

72211125




72211125








  • 1




    Compiler sometimes gets confused with the implicit class that defines the -> method. You're usually better off changing a -> b to (a, b). You have more errors in this code, but this should take care of the weird "too many arguments" message that you're seeing.
    – slouc
    Nov 18 at 19:07






  • 2




    Please do not edit your question in such a way that existing answers are invalidated. If you have a question, post a question.
    – Jörg W Mittag
    Nov 18 at 20:00










  • you are giving Map[String,[(String, Int)] as the initial value but you are iterating over list and creating map inside it so it's returning you List[Map[String,(String,Int)]].
    – Raman Mishra
    Nov 18 at 20:07










  • Every iteration of the fold, /:, needs to return a Map[_,_] but genList() is returning a Seq[_], which you are mapping over to create a Seq[Map[_,_]], which is not what what the fold requires.
    – jwvh
    Nov 19 at 2:32














  • 1




    Compiler sometimes gets confused with the implicit class that defines the -> method. You're usually better off changing a -> b to (a, b). You have more errors in this code, but this should take care of the weird "too many arguments" message that you're seeing.
    – slouc
    Nov 18 at 19:07






  • 2




    Please do not edit your question in such a way that existing answers are invalidated. If you have a question, post a question.
    – Jörg W Mittag
    Nov 18 at 20:00










  • you are giving Map[String,[(String, Int)] as the initial value but you are iterating over list and creating map inside it so it's returning you List[Map[String,(String,Int)]].
    – Raman Mishra
    Nov 18 at 20:07










  • Every iteration of the fold, /:, needs to return a Map[_,_] but genList() is returning a Seq[_], which you are mapping over to create a Seq[Map[_,_]], which is not what what the fold requires.
    – jwvh
    Nov 19 at 2:32








1




1




Compiler sometimes gets confused with the implicit class that defines the -> method. You're usually better off changing a -> b to (a, b). You have more errors in this code, but this should take care of the weird "too many arguments" message that you're seeing.
– slouc
Nov 18 at 19:07




Compiler sometimes gets confused with the implicit class that defines the -> method. You're usually better off changing a -> b to (a, b). You have more errors in this code, but this should take care of the weird "too many arguments" message that you're seeing.
– slouc
Nov 18 at 19:07




2




2




Please do not edit your question in such a way that existing answers are invalidated. If you have a question, post a question.
– Jörg W Mittag
Nov 18 at 20:00




Please do not edit your question in such a way that existing answers are invalidated. If you have a question, post a question.
– Jörg W Mittag
Nov 18 at 20:00












you are giving Map[String,[(String, Int)] as the initial value but you are iterating over list and creating map inside it so it's returning you List[Map[String,(String,Int)]].
– Raman Mishra
Nov 18 at 20:07




you are giving Map[String,[(String, Int)] as the initial value but you are iterating over list and creating map inside it so it's returning you List[Map[String,(String,Int)]].
– Raman Mishra
Nov 18 at 20:07












Every iteration of the fold, /:, needs to return a Map[_,_] but genList() is returning a Seq[_], which you are mapping over to create a Seq[Map[_,_]], which is not what what the fold requires.
– jwvh
Nov 19 at 2:32




Every iteration of the fold, /:, needs to return a Map[_,_] but genList() is returning a Seq[_], which you are mapping over to create a Seq[Map[_,_]], which is not what what the fold requires.
– jwvh
Nov 19 at 2:32












2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










ps -> (w, xv)


is interpreted as



ps.->(w, xv)


i.e. as passing two arguments instead of what you intend, which is passing a 2-tuple as single argument:



ps.->((w, xv))


or in operator syntax:



ps -> ((w, xv))





share|improve this answer





















  • Now I am getting another error after your changes. Please help. I have updated the description with the new error.
    – user3243499
    Nov 18 at 19:18


















up vote
1
down vote













You need to add an extra parenthesis as the single parenthesis is interpreted as method application:



cmap + (ps -> ((w, xv)))


which means:



cmap + (ps.->((w, xv)))


Or you can use the -> twice:



cmap + (ps -> (w -> xv))





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%2f53364264%2fwhy-this-foldleft-scala-code-does-not-work%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








    up vote
    2
    down vote



    accepted










    ps -> (w, xv)


    is interpreted as



    ps.->(w, xv)


    i.e. as passing two arguments instead of what you intend, which is passing a 2-tuple as single argument:



    ps.->((w, xv))


    or in operator syntax:



    ps -> ((w, xv))





    share|improve this answer





















    • Now I am getting another error after your changes. Please help. I have updated the description with the new error.
      – user3243499
      Nov 18 at 19:18















    up vote
    2
    down vote



    accepted










    ps -> (w, xv)


    is interpreted as



    ps.->(w, xv)


    i.e. as passing two arguments instead of what you intend, which is passing a 2-tuple as single argument:



    ps.->((w, xv))


    or in operator syntax:



    ps -> ((w, xv))





    share|improve this answer





















    • Now I am getting another error after your changes. Please help. I have updated the description with the new error.
      – user3243499
      Nov 18 at 19:18













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    ps -> (w, xv)


    is interpreted as



    ps.->(w, xv)


    i.e. as passing two arguments instead of what you intend, which is passing a 2-tuple as single argument:



    ps.->((w, xv))


    or in operator syntax:



    ps -> ((w, xv))





    share|improve this answer












    ps -> (w, xv)


    is interpreted as



    ps.->(w, xv)


    i.e. as passing two arguments instead of what you intend, which is passing a 2-tuple as single argument:



    ps.->((w, xv))


    or in operator syntax:



    ps -> ((w, xv))






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 18 at 18:53









    Jörg W Mittag

    287k62352545




    287k62352545












    • Now I am getting another error after your changes. Please help. I have updated the description with the new error.
      – user3243499
      Nov 18 at 19:18


















    • Now I am getting another error after your changes. Please help. I have updated the description with the new error.
      – user3243499
      Nov 18 at 19:18
















    Now I am getting another error after your changes. Please help. I have updated the description with the new error.
    – user3243499
    Nov 18 at 19:18




    Now I am getting another error after your changes. Please help. I have updated the description with the new error.
    – user3243499
    Nov 18 at 19:18












    up vote
    1
    down vote













    You need to add an extra parenthesis as the single parenthesis is interpreted as method application:



    cmap + (ps -> ((w, xv)))


    which means:



    cmap + (ps.->((w, xv)))


    Or you can use the -> twice:



    cmap + (ps -> (w -> xv))





    share|improve this answer

























      up vote
      1
      down vote













      You need to add an extra parenthesis as the single parenthesis is interpreted as method application:



      cmap + (ps -> ((w, xv)))


      which means:



      cmap + (ps.->((w, xv)))


      Or you can use the -> twice:



      cmap + (ps -> (w -> xv))





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        You need to add an extra parenthesis as the single parenthesis is interpreted as method application:



        cmap + (ps -> ((w, xv)))


        which means:



        cmap + (ps.->((w, xv)))


        Or you can use the -> twice:



        cmap + (ps -> (w -> xv))





        share|improve this answer












        You need to add an extra parenthesis as the single parenthesis is interpreted as method application:



        cmap + (ps -> ((w, xv)))


        which means:



        cmap + (ps.->((w, xv)))


        Or you can use the -> twice:



        cmap + (ps -> (w -> xv))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 18 at 18:52









        Gábor Bakos

        6,52592541




        6,52592541






























            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.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • 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%2f53364264%2fwhy-this-foldleft-scala-code-does-not-work%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

            Tonle Sap (See)

            I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

            Guatemaltekische Davis-Cup-Mannschaft