set increment using flatMap
up vote
-4
down vote
favorite
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
up vote
-4
down vote
favorite
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 at 9:18
2
If the input is only 2 values why should the output have 5 values? ASetonly contains what you put in it (minus any duplicates).
– jwvh
Nov 22 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 at 9:26
3
It seems, that you simply misunderstoodflatMapand now you have wrong expectations.
– ygor
Nov 22 at 9:28
1
I perhaps you meanRangeinstead ofSet.
– ygor
Nov 22 at 9:30
|
show 1 more comment
up vote
-4
down vote
favorite
up vote
-4
down vote
favorite
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 at 9:17
Sivakumar M
154
154
2
If have used flatMap with condition, but got error.Can you post what you tried and the error exactly ?
– Chirlo
Nov 22 at 9:18
2
If the input is only 2 values why should the output have 5 values? ASetonly contains what you put in it (minus any duplicates).
– jwvh
Nov 22 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 at 9:26
3
It seems, that you simply misunderstoodflatMapand now you have wrong expectations.
– ygor
Nov 22 at 9:28
1
I perhaps you meanRangeinstead ofSet.
– ygor
Nov 22 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 at 9:18
2
If the input is only 2 values why should the output have 5 values? ASetonly contains what you put in it (minus any duplicates).
– jwvh
Nov 22 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 at 9:26
3
It seems, that you simply misunderstoodflatMapand now you have wrong expectations.
– ygor
Nov 22 at 9:28
1
I perhaps you meanRangeinstead ofSet.
– ygor
Nov 22 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 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 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 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 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 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 at 9:26
3
3
It seems, that you simply misunderstood
flatMap and now you have wrong expectations.– ygor
Nov 22 at 9:28
It seems, that you simply misunderstood
flatMap and now you have wrong expectations.– ygor
Nov 22 at 9:28
1
1
I perhaps you mean
Range instead of Set.– ygor
Nov 22 at 9:30
I perhaps you mean
Range instead of Set.– ygor
Nov 22 at 9:30
|
show 1 more comment
2 Answers
2
active
oldest
votes
up vote
0
down vote
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.headandset.lastis basically never right (unless you have aSortedSet, or aLinkedHashSet, or some other subtype which guarantees iteration order).
– Alexey Romanov
Nov 22 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 at 10:14
add a comment |
up vote
0
down vote
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 |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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.headandset.lastis basically never right (unless you have aSortedSet, or aLinkedHashSet, or some other subtype which guarantees iteration order).
– Alexey Romanov
Nov 22 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 at 10:14
add a comment |
up vote
0
down vote
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.headandset.lastis basically never right (unless you have aSortedSet, or aLinkedHashSet, or some other subtype which guarantees iteration order).
– Alexey Romanov
Nov 22 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 at 10:14
add a comment |
up vote
0
down vote
up vote
0
down vote
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 at 9:44
answered Nov 22 at 9:28
Balaji Reddy
2,88831534
2,88831534
1
Usingset.headandset.lastis basically never right (unless you have aSortedSet, or aLinkedHashSet, or some other subtype which guarantees iteration order).
– Alexey Romanov
Nov 22 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 at 10:14
add a comment |
1
Usingset.headandset.lastis basically never right (unless you have aSortedSet, or aLinkedHashSet, or some other subtype which guarantees iteration order).
– Alexey Romanov
Nov 22 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 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 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 at 10:07
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 at 10:14
@AlexeyRomanov I agree
– Balaji Reddy
Nov 22 at 10:14
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 at 10:08
answered Nov 22 at 9:45
pme
2,1691123
2,1691123
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%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 at 9:18
2
If the input is only 2 values why should the output have 5 values? A
Setonly contains what you put in it (minus any duplicates).– jwvh
Nov 22 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 at 9:26
3
It seems, that you simply misunderstood
flatMapand now you have wrong expectations.– ygor
Nov 22 at 9:28
1
I perhaps you mean
Rangeinstead ofSet.– ygor
Nov 22 at 9:30