How to create an immutable Map in Scala using foldings?
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
|
show 2 more comments
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
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 themap()call to a fold, but you still might not get the output you're looking for.oriwordsis aSetand 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
|
show 2 more comments
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
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
scala
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 themap()call to a fold, but you still might not get the output you're looking for.oriwordsis aSetand 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
|
show 2 more comments
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 themap()call to a fold, but you still might not get the output you're looking for.oriwordsis aSetand 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
|
show 2 more comments
2 Answers
2
active
oldest
votes
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))
add a comment |
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...
add a comment |
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
});
}
});
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%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
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))
add a comment |
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))
add a comment |
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))
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))
edited Nov 19 '18 at 10:06
answered Nov 19 '18 at 9:51
Raman MishraRaman Mishra
1,2741417
1,2741417
add a comment |
add a comment |
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...
add a comment |
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...
add a comment |
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...
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...
edited Nov 19 '18 at 10:25
answered Nov 19 '18 at 9:59
Terry DactylTerry Dactyl
1,104412
1,104412
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.
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%2f53371286%2fhow-to-create-an-immutable-map-in-scala-using-foldings%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
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.oriwordsis aSetand 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