Retrieve value from item wrapped in a Future[Option[Item]] [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash 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.







share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.












  • 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
















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.







share|improve this question













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
If this question can be reworded to fit the rules in the help center, please edit the question.












  • 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












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.







share|improve this question













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.









share|improve this question












share|improve this question




share|improve this question








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
If this question can be reworded to fit the rules in the help center, please edit the question.




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
If this question can be reworded to fit the rules in the help center, please edit the question.











  • 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




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










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 => ""








share|improve this answer




























    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.)






    share|improve this answer





















    • 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 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

















    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 => ""








    share|improve this answer

























      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 => ""








      share|improve this answer























        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 => ""








        share|improve this answer













        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 => ""









        share|improve this answer













        share|improve this answer



        share|improve this answer











        answered May 23 at 10:28









        Steve Waldman

        1193




        1193






















            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.)






            share|improve this answer





















            • 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 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














            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.)






            share|improve this answer





















            • 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 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












            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.)






            share|improve this answer













            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.)







            share|improve this answer













            share|improve this answer



            share|improve this answer











            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 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
















            • 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 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















            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


            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation