set increment using flatMap
I have a mutuable set and assigning the values to it from input
var set = scala.collection.mutable.Set[Int]()
set ++= (in.readLine().split(" ").map(_.toInt))
Input:
1 5
Actual Output:
1,5
Required Output:
1,2,3,4,5
If have used flatMap with condition, but got error. How to achieve this with flatMap
scala
|
show 1 more comment
I have a mutuable set and assigning the values to it from input
var set = scala.collection.mutable.Set[Int]()
set ++= (in.readLine().split(" ").map(_.toInt))
Input:
1 5
Actual Output:
1,5
Required Output:
1,2,3,4,5
If have used flatMap with condition, but got error. How to achieve this with flatMap
scala
2
If have used flatMap with condition, but got error.
Can you post what you tried and the error exactly ?
– Chirlo
Nov 22 '18 at 9:18
2
If the input is only 2 values why should the output have 5 values? ASet
only contains what you put in it (minus any duplicates).
– jwvh
Nov 22 '18 at 9:22
1
why you are so obsessed with flatMap why do you want to use only flat map? as i can see you are giving 2 int in the input which are space separated and if you will split it it's going to give you List("1", "5") now what makes you think you can use flatMap on that?
– Raman Mishra
Nov 22 '18 at 9:26
3
It seems, that you simply misunderstoodflatMap
and now you have wrong expectations.
– ygor
Nov 22 '18 at 9:28
1
I perhaps you meanRange
instead ofSet
.
– ygor
Nov 22 '18 at 9:30
|
show 1 more comment
I have a mutuable set and assigning the values to it from input
var set = scala.collection.mutable.Set[Int]()
set ++= (in.readLine().split(" ").map(_.toInt))
Input:
1 5
Actual Output:
1,5
Required Output:
1,2,3,4,5
If have used flatMap with condition, but got error. How to achieve this with flatMap
scala
I have a mutuable set and assigning the values to it from input
var set = scala.collection.mutable.Set[Int]()
set ++= (in.readLine().split(" ").map(_.toInt))
Input:
1 5
Actual Output:
1,5
Required Output:
1,2,3,4,5
If have used flatMap with condition, but got error. How to achieve this with flatMap
scala
scala
asked Nov 22 '18 at 9:17
Sivakumar MSivakumar M
175
175
2
If have used flatMap with condition, but got error.
Can you post what you tried and the error exactly ?
– Chirlo
Nov 22 '18 at 9:18
2
If the input is only 2 values why should the output have 5 values? ASet
only contains what you put in it (minus any duplicates).
– jwvh
Nov 22 '18 at 9:22
1
why you are so obsessed with flatMap why do you want to use only flat map? as i can see you are giving 2 int in the input which are space separated and if you will split it it's going to give you List("1", "5") now what makes you think you can use flatMap on that?
– Raman Mishra
Nov 22 '18 at 9:26
3
It seems, that you simply misunderstoodflatMap
and now you have wrong expectations.
– ygor
Nov 22 '18 at 9:28
1
I perhaps you meanRange
instead ofSet
.
– ygor
Nov 22 '18 at 9:30
|
show 1 more comment
2
If have used flatMap with condition, but got error.
Can you post what you tried and the error exactly ?
– Chirlo
Nov 22 '18 at 9:18
2
If the input is only 2 values why should the output have 5 values? ASet
only contains what you put in it (minus any duplicates).
– jwvh
Nov 22 '18 at 9:22
1
why you are so obsessed with flatMap why do you want to use only flat map? as i can see you are giving 2 int in the input which are space separated and if you will split it it's going to give you List("1", "5") now what makes you think you can use flatMap on that?
– Raman Mishra
Nov 22 '18 at 9:26
3
It seems, that you simply misunderstoodflatMap
and now you have wrong expectations.
– ygor
Nov 22 '18 at 9:28
1
I perhaps you meanRange
instead ofSet
.
– ygor
Nov 22 '18 at 9:30
2
2
If have used flatMap with condition, but got error.
Can you post what you tried and the error exactly ?– Chirlo
Nov 22 '18 at 9:18
If have used flatMap with condition, but got error.
Can you post what you tried and the error exactly ?– Chirlo
Nov 22 '18 at 9:18
2
2
If the input is only 2 values why should the output have 5 values? A
Set
only contains what you put in it (minus any duplicates).– jwvh
Nov 22 '18 at 9:22
If the input is only 2 values why should the output have 5 values? A
Set
only contains what you put in it (minus any duplicates).– jwvh
Nov 22 '18 at 9:22
1
1
why you are so obsessed with flatMap why do you want to use only flat map? as i can see you are giving 2 int in the input which are space separated and if you will split it it's going to give you List("1", "5") now what makes you think you can use flatMap on that?
– Raman Mishra
Nov 22 '18 at 9:26
why you are so obsessed with flatMap why do you want to use only flat map? as i can see you are giving 2 int in the input which are space separated and if you will split it it's going to give you List("1", "5") now what makes you think you can use flatMap on that?
– Raman Mishra
Nov 22 '18 at 9:26
3
3
It seems, that you simply misunderstood
flatMap
and now you have wrong expectations.– ygor
Nov 22 '18 at 9:28
It seems, that you simply misunderstood
flatMap
and now you have wrong expectations.– ygor
Nov 22 '18 at 9:28
1
1
I perhaps you mean
Range
instead of Set
.– ygor
Nov 22 '18 at 9:30
I perhaps you mean
Range
instead of Set
.– ygor
Nov 22 '18 at 9:30
|
show 1 more comment
2 Answers
2
active
oldest
votes
I assume you have always a String like '2 5' as Input:
The solution could look like:
def toSeq(value: String): Seq[Int] = {
value.split(" ")
.map(_.toInt).toList match {
case x1::x2::_ => x1 to x2
case other => Nil// handle Exception
}
}
println(toSeq("1 5").toList)
Be aware that the input is not validated!
add a comment |
My opinion is, there is no need for flatMap
operation here. You might have received compiler error saying invalid syntax. yet i'm going ahead and give you the solution if I really understand your question.
val in = "1 5"
var set = scala.collection.mutable.Set[Int]()
set.++=(in.split(" ").flatMap(value => Set(value.trim.toInt)))
val range = set.head to set.last
println( range.mkString(","))
Result
1,2,3,4,5
You might have noticed that only change I had done is , return the value in Set(_)
.
The nature of the flatMap
operation is, it flats multiple collections into single collection hence I'm returning it as Set(_)
An easiest alternative I would think of is
val in = "1 5"
val split = in.split(" ")
val range = split(0).trim.toInt to split(1).trim.toInt
println(range.mkString(","))
1
Usingset.head
andset.last
is basically never right (unless you have aSortedSet
, or aLinkedHashSet
, or some other subtype which guarantees iteration order).
– Alexey Romanov
Nov 22 '18 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 '18 at 10:14
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%2f53427451%2fset-increment-using-flatmap%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
I assume you have always a String like '2 5' as Input:
The solution could look like:
def toSeq(value: String): Seq[Int] = {
value.split(" ")
.map(_.toInt).toList match {
case x1::x2::_ => x1 to x2
case other => Nil// handle Exception
}
}
println(toSeq("1 5").toList)
Be aware that the input is not validated!
add a comment |
I assume you have always a String like '2 5' as Input:
The solution could look like:
def toSeq(value: String): Seq[Int] = {
value.split(" ")
.map(_.toInt).toList match {
case x1::x2::_ => x1 to x2
case other => Nil// handle Exception
}
}
println(toSeq("1 5").toList)
Be aware that the input is not validated!
add a comment |
I assume you have always a String like '2 5' as Input:
The solution could look like:
def toSeq(value: String): Seq[Int] = {
value.split(" ")
.map(_.toInt).toList match {
case x1::x2::_ => x1 to x2
case other => Nil// handle Exception
}
}
println(toSeq("1 5").toList)
Be aware that the input is not validated!
I assume you have always a String like '2 5' as Input:
The solution could look like:
def toSeq(value: String): Seq[Int] = {
value.split(" ")
.map(_.toInt).toList match {
case x1::x2::_ => x1 to x2
case other => Nil// handle Exception
}
}
println(toSeq("1 5").toList)
Be aware that the input is not validated!
edited Nov 22 '18 at 10:08
answered Nov 22 '18 at 9:45
pmepme
2,55711325
2,55711325
add a comment |
add a comment |
My opinion is, there is no need for flatMap
operation here. You might have received compiler error saying invalid syntax. yet i'm going ahead and give you the solution if I really understand your question.
val in = "1 5"
var set = scala.collection.mutable.Set[Int]()
set.++=(in.split(" ").flatMap(value => Set(value.trim.toInt)))
val range = set.head to set.last
println( range.mkString(","))
Result
1,2,3,4,5
You might have noticed that only change I had done is , return the value in Set(_)
.
The nature of the flatMap
operation is, it flats multiple collections into single collection hence I'm returning it as Set(_)
An easiest alternative I would think of is
val in = "1 5"
val split = in.split(" ")
val range = split(0).trim.toInt to split(1).trim.toInt
println(range.mkString(","))
1
Usingset.head
andset.last
is basically never right (unless you have aSortedSet
, or aLinkedHashSet
, or some other subtype which guarantees iteration order).
– Alexey Romanov
Nov 22 '18 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 '18 at 10:14
add a comment |
My opinion is, there is no need for flatMap
operation here. You might have received compiler error saying invalid syntax. yet i'm going ahead and give you the solution if I really understand your question.
val in = "1 5"
var set = scala.collection.mutable.Set[Int]()
set.++=(in.split(" ").flatMap(value => Set(value.trim.toInt)))
val range = set.head to set.last
println( range.mkString(","))
Result
1,2,3,4,5
You might have noticed that only change I had done is , return the value in Set(_)
.
The nature of the flatMap
operation is, it flats multiple collections into single collection hence I'm returning it as Set(_)
An easiest alternative I would think of is
val in = "1 5"
val split = in.split(" ")
val range = split(0).trim.toInt to split(1).trim.toInt
println(range.mkString(","))
1
Usingset.head
andset.last
is basically never right (unless you have aSortedSet
, or aLinkedHashSet
, or some other subtype which guarantees iteration order).
– Alexey Romanov
Nov 22 '18 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 '18 at 10:14
add a comment |
My opinion is, there is no need for flatMap
operation here. You might have received compiler error saying invalid syntax. yet i'm going ahead and give you the solution if I really understand your question.
val in = "1 5"
var set = scala.collection.mutable.Set[Int]()
set.++=(in.split(" ").flatMap(value => Set(value.trim.toInt)))
val range = set.head to set.last
println( range.mkString(","))
Result
1,2,3,4,5
You might have noticed that only change I had done is , return the value in Set(_)
.
The nature of the flatMap
operation is, it flats multiple collections into single collection hence I'm returning it as Set(_)
An easiest alternative I would think of is
val in = "1 5"
val split = in.split(" ")
val range = split(0).trim.toInt to split(1).trim.toInt
println(range.mkString(","))
My opinion is, there is no need for flatMap
operation here. You might have received compiler error saying invalid syntax. yet i'm going ahead and give you the solution if I really understand your question.
val in = "1 5"
var set = scala.collection.mutable.Set[Int]()
set.++=(in.split(" ").flatMap(value => Set(value.trim.toInt)))
val range = set.head to set.last
println( range.mkString(","))
Result
1,2,3,4,5
You might have noticed that only change I had done is , return the value in Set(_)
.
The nature of the flatMap
operation is, it flats multiple collections into single collection hence I'm returning it as Set(_)
An easiest alternative I would think of is
val in = "1 5"
val split = in.split(" ")
val range = split(0).trim.toInt to split(1).trim.toInt
println(range.mkString(","))
edited Nov 22 '18 at 9:44
answered Nov 22 '18 at 9:28
Balaji ReddyBalaji Reddy
2,98331634
2,98331634
1
Usingset.head
andset.last
is basically never right (unless you have aSortedSet
, or aLinkedHashSet
, or some other subtype which guarantees iteration order).
– Alexey Romanov
Nov 22 '18 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 '18 at 10:14
add a comment |
1
Usingset.head
andset.last
is basically never right (unless you have aSortedSet
, or aLinkedHashSet
, or some other subtype which guarantees iteration order).
– Alexey Romanov
Nov 22 '18 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 '18 at 10:14
1
1
Using
set.head
and set.last
is basically never right (unless you have a SortedSet
, or a LinkedHashSet
, or some other subtype which guarantees iteration order).– Alexey Romanov
Nov 22 '18 at 10:07
Using
set.head
and set.last
is basically never right (unless you have a SortedSet
, or a LinkedHashSet
, or some other subtype which guarantees iteration order).– Alexey Romanov
Nov 22 '18 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 '18 at 10:14
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 '18 at 10:14
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%2f53427451%2fset-increment-using-flatmap%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
2
If have used flatMap with condition, but got error.
Can you post what you tried and the error exactly ?– Chirlo
Nov 22 '18 at 9:18
2
If the input is only 2 values why should the output have 5 values? A
Set
only contains what you put in it (minus any duplicates).– jwvh
Nov 22 '18 at 9:22
1
why you are so obsessed with flatMap why do you want to use only flat map? as i can see you are giving 2 int in the input which are space separated and if you will split it it's going to give you List("1", "5") now what makes you think you can use flatMap on that?
– Raman Mishra
Nov 22 '18 at 9:26
3
It seems, that you simply misunderstood
flatMap
and now you have wrong expectations.– ygor
Nov 22 '18 at 9:28
1
I perhaps you mean
Range
instead ofSet
.– ygor
Nov 22 '18 at 9:30