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 => {
^
scala
add a comment |
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 => {
^
scala
1
Compiler sometimes gets confused with the implicit class that defines the->method. You're usually better off changinga -> bto(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 aMap[_,_]butgenList()is returning aSeq[_], which you are mapping over to create aSeq[Map[_,_]], which is not what what the fold requires.
– jwvh
Nov 19 at 2:32
add a comment |
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 => {
^
scala
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
scala
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 changinga -> bto(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 aMap[_,_]butgenList()is returning aSeq[_], which you are mapping over to create aSeq[Map[_,_]], which is not what what the fold requires.
– jwvh
Nov 19 at 2:32
add a comment |
1
Compiler sometimes gets confused with the implicit class that defines the->method. You're usually better off changinga -> bto(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 aMap[_,_]butgenList()is returning aSeq[_], which you are mapping over to create aSeq[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
add a comment |
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))
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
add a comment |
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))
add a comment |
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))
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
add a comment |
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))
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
add a comment |
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))
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))
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
add a comment |
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
add a comment |
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))
add a comment |
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))
add a comment |
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))
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))
answered Nov 18 at 18:52
Gábor Bakos
6,52592541
6,52592541
add a comment |
add a comment |
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.
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%2f53364264%2fwhy-this-foldleft-scala-code-does-not-work%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
1
Compiler sometimes gets confused with the implicit class that defines the
->method. You're usually better off changinga -> bto(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 aMap[_,_]butgenList()is returning aSeq[_], which you are mapping over to create aSeq[Map[_,_]], which is not what what the fold requires.– jwvh
Nov 19 at 2:32