How can I make this code more elegant?

osocron

I have the following function:

private def solveError(hostIp: String, command: String) = {
  commandSlave(hostIp, "STOP SLAVE") match {
    case Success(x) => commandSlave(hostIp, command) match {
      case Success(y) => commandSlave(hostIp, "START SLAVE") match {
        case Success(z) => Success(z)
        case Failure(ex) => Failure(ex)
      }
      case Failure(ex) => Failure(ex)
    }
    case Failure(ex) => Failure(ex)
  }
}

I think there must be a more elegant way to write this function but I'm not sure how. Using nested match cases doesn't feel right to me but I'm new to Scala and I don't know if there is a better way. Any help is much appreciated.

Aivean

Assuming that commandSlave returns Try, flatMap is what you want:

commandSlave(hostIp, "STOP SLAVE")
  .flatMap(_ => commandSlave(hostIp, command))
  .flatMap(_ => commandSlave(hostIp, "START SLAVE"))

From scaladocs of flatMap of Try:

Returns the given function applied to the value from this Success or returns this if this is a Failure.

Also you can use for comprehension here, which internally desugars to the same sequence of flatMaps:

private def solveError(hostIp: String, serverHost: Host, backHost: Host, command: String) = 
  for {
    _ <- commandSlave(hostIp, "STOP SLAVE")
    _ <- commandSlave(hostIp, command)
    z <- commandSlave(hostIp, "START SLAVE")
  } yield z 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How can I make this PyTorch tensor (B, C, H, W) tiling & blending code simpler and more efficient?

From Dev

How to make this code more elegant?

From Dev

How to make this UINavigationController seeking code more elegant?

From Dev

How can I make my Windows API GUI code more object-oriented?

From Dev

How can I make this Observable more reusable?

From Dev

How can I make my sort more elegant?

From Dev

How can I make this code to find a pair with a sum more efficient?

From Dev

How can I make unwrapping this optional in Swift, a little more elegant?

From Dev

How do I make this code more responsive?

From Dev

How can I make this loop more efficient?

From Dev

How can I make this method more concise?

From Dev

ValueError: need more than 1 value to unpack, how can I make my code more robust?

From Dev

How can I use interfaces to allow for more than 1 struct type to make code more useable?

From Dev

Trying to make this code in r more elegant and shorter if possible - R

From Dev

How can I make my code more readable and DRYer when working with XML namespaces in Python?

From Dev

How can I make this Makefile more generic?

From Dev

How can I refactor my kotlin code and make it more clear

From Dev

How can I make the button more visible?

From Dev

How can I refactor / make this code more pythonic?

From Dev

Removal of whitespace and text in brackets - make my working code more elegant

From Dev

I have a script code which works, but how do I make this script code more "elegant"?

From Dev

How can I perform the following computation in a more elegant (and Pythonic) way?

From Dev

How to make this simple C# code more efficent/elegant

From Dev

How can I make this loop more efficient?

From Dev

Make a line of code more elegant

From Dev

How to make this R code more elegant?

From Dev

How can i make these 3 lines of code more DRY

From Dev

How can I make a Read More Button?

From Dev

How can I make my JavaScript code more DRY?

Related Related

  1. 1

    How can I make this PyTorch tensor (B, C, H, W) tiling & blending code simpler and more efficient?

  2. 2

    How to make this code more elegant?

  3. 3

    How to make this UINavigationController seeking code more elegant?

  4. 4

    How can I make my Windows API GUI code more object-oriented?

  5. 5

    How can I make this Observable more reusable?

  6. 6

    How can I make my sort more elegant?

  7. 7

    How can I make this code to find a pair with a sum more efficient?

  8. 8

    How can I make unwrapping this optional in Swift, a little more elegant?

  9. 9

    How do I make this code more responsive?

  10. 10

    How can I make this loop more efficient?

  11. 11

    How can I make this method more concise?

  12. 12

    ValueError: need more than 1 value to unpack, how can I make my code more robust?

  13. 13

    How can I use interfaces to allow for more than 1 struct type to make code more useable?

  14. 14

    Trying to make this code in r more elegant and shorter if possible - R

  15. 15

    How can I make my code more readable and DRYer when working with XML namespaces in Python?

  16. 16

    How can I make this Makefile more generic?

  17. 17

    How can I refactor my kotlin code and make it more clear

  18. 18

    How can I make the button more visible?

  19. 19

    How can I refactor / make this code more pythonic?

  20. 20

    Removal of whitespace and text in brackets - make my working code more elegant

  21. 21

    I have a script code which works, but how do I make this script code more "elegant"?

  22. 22

    How can I perform the following computation in a more elegant (and Pythonic) way?

  23. 23

    How to make this simple C# code more efficent/elegant

  24. 24

    How can I make this loop more efficient?

  25. 25

    Make a line of code more elegant

  26. 26

    How to make this R code more elegant?

  27. 27

    How can i make these 3 lines of code more DRY

  28. 28

    How can I make a Read More Button?

  29. 29

    How can I make my JavaScript code more DRY?

HotTag

Archive