Retrieve value from item wrapped in a Future[Option[Item]] [closed]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
0
down vote
favorite
I have a function like this
def findById(id: UUID): Future[Option[Item]]
case class Item(subject: String)
Now I want to retrieve the subject
if the item exists else I want the subject
to be empty string.
val subject: Future[String] = repository
.findById(someId)
.map(_.map(i => i.subject))
.map(_.getOrElse(""))
subject.foreach(doSomething)
Am I doing it right or is there a better way in Scala without frameworks like Scalaz.
scala
closed as off-topic by Billal BEGUERADJ, t3chb0t, Stephen Rauch, Sam Onela, Dannnno May 23 at 13:46
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." â Billal BEGUERADJ, t3chb0t, Stephen Rauch, Sam Onela, Dannnno
add a comment |Â
up vote
0
down vote
favorite
I have a function like this
def findById(id: UUID): Future[Option[Item]]
case class Item(subject: String)
Now I want to retrieve the subject
if the item exists else I want the subject
to be empty string.
val subject: Future[String] = repository
.findById(someId)
.map(_.map(i => i.subject))
.map(_.getOrElse(""))
subject.foreach(doSomething)
Am I doing it right or is there a better way in Scala without frameworks like Scalaz.
scala
closed as off-topic by Billal BEGUERADJ, t3chb0t, Stephen Rauch, Sam Onela, Dannnno May 23 at 13:46
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." â Billal BEGUERADJ, t3chb0t, Stephen Rauch, Sam Onela, Dannnno
Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does
â Dannnno
May 23 at 13:45
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a function like this
def findById(id: UUID): Future[Option[Item]]
case class Item(subject: String)
Now I want to retrieve the subject
if the item exists else I want the subject
to be empty string.
val subject: Future[String] = repository
.findById(someId)
.map(_.map(i => i.subject))
.map(_.getOrElse(""))
subject.foreach(doSomething)
Am I doing it right or is there a better way in Scala without frameworks like Scalaz.
scala
I have a function like this
def findById(id: UUID): Future[Option[Item]]
case class Item(subject: String)
Now I want to retrieve the subject
if the item exists else I want the subject
to be empty string.
val subject: Future[String] = repository
.findById(someId)
.map(_.map(i => i.subject))
.map(_.getOrElse(""))
subject.foreach(doSomething)
Am I doing it right or is there a better way in Scala without frameworks like Scalaz.
scala
edited May 23 at 9:22
asked May 23 at 8:24
uraza
865
865
closed as off-topic by Billal BEGUERADJ, t3chb0t, Stephen Rauch, Sam Onela, Dannnno May 23 at 13:46
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." â Billal BEGUERADJ, t3chb0t, Stephen Rauch, Sam Onela, Dannnno
closed as off-topic by Billal BEGUERADJ, t3chb0t, Stephen Rauch, Sam Onela, Dannnno May 23 at 13:46
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Lacks concrete context: Code Review requires concrete code from a project, with sufficient context for reviewers to understand how that code is used. Pseudocode, stub code, hypothetical code, obfuscated code, and generic best practices are outside the scope of this site." â Billal BEGUERADJ, t3chb0t, Stephen Rauch, Sam Onela, Dannnno
Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does
â Dannnno
May 23 at 13:45
add a comment |Â
Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does
â Dannnno
May 23 at 13:45
Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does
â Dannnno
May 23 at 13:45
Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does
â Dannnno
May 23 at 13:45
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
It's better that you are staying in the Futures domain, no longer trying to unwrap to String
.
It's not so easy to follow all of the underscore-based function expressions. I'd try something like...
val subject: Future[String] =
repository.findById(someId) map mbItem =>
mbItem match
case Some( item ) => item.subject
case None => ""
add a comment |Â
up vote
0
down vote
It's best to stay in the Futures domain if you can. But if you absolutely need out, you need to await to be sure a result has been computed.
val result = Await.result(repository.findById(someId), Duration.Inf)
val subject = result match
case Some( item ) => item.subject
case None => ""
(If you prefer not to wait, but you are cool with the computation maybe not having completed, see the value method of Future
.)
I thought the map applies a function on the future when it completes. Isn't the Await.result also blocking?
â uraza
May 23 at 9:19
My apologies, sir. I forgot to add a foreach at the end. See edited post.
â uraza
May 23 at 9:22
yes, await is blocking. but in the original version of your post subject was aString
rather thanFuture[String]
, so you would have had to block to safely unwrap. now that what you want isFuture[String]
, you can stay in the Futures domain withmap(...)
etc.
â Steve Waldman
May 23 at 10:20
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
It's better that you are staying in the Futures domain, no longer trying to unwrap to String
.
It's not so easy to follow all of the underscore-based function expressions. I'd try something like...
val subject: Future[String] =
repository.findById(someId) map mbItem =>
mbItem match
case Some( item ) => item.subject
case None => ""
add a comment |Â
up vote
1
down vote
It's better that you are staying in the Futures domain, no longer trying to unwrap to String
.
It's not so easy to follow all of the underscore-based function expressions. I'd try something like...
val subject: Future[String] =
repository.findById(someId) map mbItem =>
mbItem match
case Some( item ) => item.subject
case None => ""
add a comment |Â
up vote
1
down vote
up vote
1
down vote
It's better that you are staying in the Futures domain, no longer trying to unwrap to String
.
It's not so easy to follow all of the underscore-based function expressions. I'd try something like...
val subject: Future[String] =
repository.findById(someId) map mbItem =>
mbItem match
case Some( item ) => item.subject
case None => ""
It's better that you are staying in the Futures domain, no longer trying to unwrap to String
.
It's not so easy to follow all of the underscore-based function expressions. I'd try something like...
val subject: Future[String] =
repository.findById(someId) map mbItem =>
mbItem match
case Some( item ) => item.subject
case None => ""
answered May 23 at 10:28
Steve Waldman
1193
1193
add a comment |Â
add a comment |Â
up vote
0
down vote
It's best to stay in the Futures domain if you can. But if you absolutely need out, you need to await to be sure a result has been computed.
val result = Await.result(repository.findById(someId), Duration.Inf)
val subject = result match
case Some( item ) => item.subject
case None => ""
(If you prefer not to wait, but you are cool with the computation maybe not having completed, see the value method of Future
.)
I thought the map applies a function on the future when it completes. Isn't the Await.result also blocking?
â uraza
May 23 at 9:19
My apologies, sir. I forgot to add a foreach at the end. See edited post.
â uraza
May 23 at 9:22
yes, await is blocking. but in the original version of your post subject was aString
rather thanFuture[String]
, so you would have had to block to safely unwrap. now that what you want isFuture[String]
, you can stay in the Futures domain withmap(...)
etc.
â Steve Waldman
May 23 at 10:20
add a comment |Â
up vote
0
down vote
It's best to stay in the Futures domain if you can. But if you absolutely need out, you need to await to be sure a result has been computed.
val result = Await.result(repository.findById(someId), Duration.Inf)
val subject = result match
case Some( item ) => item.subject
case None => ""
(If you prefer not to wait, but you are cool with the computation maybe not having completed, see the value method of Future
.)
I thought the map applies a function on the future when it completes. Isn't the Await.result also blocking?
â uraza
May 23 at 9:19
My apologies, sir. I forgot to add a foreach at the end. See edited post.
â uraza
May 23 at 9:22
yes, await is blocking. but in the original version of your post subject was aString
rather thanFuture[String]
, so you would have had to block to safely unwrap. now that what you want isFuture[String]
, you can stay in the Futures domain withmap(...)
etc.
â Steve Waldman
May 23 at 10:20
add a comment |Â
up vote
0
down vote
up vote
0
down vote
It's best to stay in the Futures domain if you can. But if you absolutely need out, you need to await to be sure a result has been computed.
val result = Await.result(repository.findById(someId), Duration.Inf)
val subject = result match
case Some( item ) => item.subject
case None => ""
(If you prefer not to wait, but you are cool with the computation maybe not having completed, see the value method of Future
.)
It's best to stay in the Futures domain if you can. But if you absolutely need out, you need to await to be sure a result has been computed.
val result = Await.result(repository.findById(someId), Duration.Inf)
val subject = result match
case Some( item ) => item.subject
case None => ""
(If you prefer not to wait, but you are cool with the computation maybe not having completed, see the value method of Future
.)
answered May 23 at 8:51
Steve Waldman
1193
1193
I thought the map applies a function on the future when it completes. Isn't the Await.result also blocking?
â uraza
May 23 at 9:19
My apologies, sir. I forgot to add a foreach at the end. See edited post.
â uraza
May 23 at 9:22
yes, await is blocking. but in the original version of your post subject was aString
rather thanFuture[String]
, so you would have had to block to safely unwrap. now that what you want isFuture[String]
, you can stay in the Futures domain withmap(...)
etc.
â Steve Waldman
May 23 at 10:20
add a comment |Â
I thought the map applies a function on the future when it completes. Isn't the Await.result also blocking?
â uraza
May 23 at 9:19
My apologies, sir. I forgot to add a foreach at the end. See edited post.
â uraza
May 23 at 9:22
yes, await is blocking. but in the original version of your post subject was aString
rather thanFuture[String]
, so you would have had to block to safely unwrap. now that what you want isFuture[String]
, you can stay in the Futures domain withmap(...)
etc.
â Steve Waldman
May 23 at 10:20
I thought the map applies a function on the future when it completes. Isn't the Await.result also blocking?
â uraza
May 23 at 9:19
I thought the map applies a function on the future when it completes. Isn't the Await.result also blocking?
â uraza
May 23 at 9:19
My apologies, sir. I forgot to add a foreach at the end. See edited post.
â uraza
May 23 at 9:22
My apologies, sir. I forgot to add a foreach at the end. See edited post.
â uraza
May 23 at 9:22
yes, await is blocking. but in the original version of your post subject was a
String
rather than Future[String]
, so you would have had to block to safely unwrap. now that what you want is Future[String]
, you can stay in the Futures domain with map(...)
etc.â Steve Waldman
May 23 at 10:20
yes, await is blocking. but in the original version of your post subject was a
String
rather than Future[String]
, so you would have had to block to safely unwrap. now that what you want is Future[String]
, you can stay in the Futures domain with map(...)
etc.â Steve Waldman
May 23 at 10:20
add a comment |Â
Welcome to Code Review! This question is incomplete. To help reviewers give you better answers, please add sufficient context to your question. The more you tell us about what your code does and what the purpose of doing that is, the easier it will be for reviewers to help you. Questions should include a description of what the code does
â Dannnno
May 23 at 13:45