How can I avoid these case statements?

Sean Clark Hess

How can I express the following without the nested case statements? Specifically, I would like to do the steps in order, and call fail if any of the steps hit the "bad" branch.

loadData :: IO [Market]
loadData = do
  filedata <- B.readFile "markets.json"
  case parseData filedata of
    Left err -> fail err
    Right v -> do
      case fromJSON v of
        Error err -> fail err
        Success a -> return a
Luis Casillas

You probably want the exception monad transformer here. Something like this (utterly untested):

import Control.Monad.Trans
import Control.Monad.Except

type MyMonadStack a = ExceptT String IO a

loadData :: MyMonadStack [Market]
loadData = do filedata <- lift $ B.readFile "markets.json"
              v <- ExceptT $ parseData filedata
              a <- ExceptT $ toEither $ fromJSON v
              return a

toEither :: WhateverTypeFromJSONReturns a b -> Either a b
toEither (Error a) = Left a
toEither (Success b) = Right b

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I avoid using instanceof in this case?

From Dev

How can i avoid ArrayIndexOutOfBoundsException in this case?

From Dev

How can I make switch-case statements case insensitive?

From Dev

Java - How can I avoid using try catch statements

From Dev

How can I avoid if else statements within a for loop?

From Dev

How to avoid switch-case statements in Java

From Dev

How to avoid switch-case statements in Java

From Dev

PostgreSQL: How to avoid division by zero when using case statements

From Dev

How to avoid repetitive CASE statements in SQL WHILE loop doing INSERT

From Dev

PostgreSQL: How to avoid division by zero when using case statements

From Dev

VBA - How to avoid copy paste code in select-case statements?

From Dev

How can I avoid so many if statements in copying one object to another?

From Dev

How can I draw Hangman in Bash without embedding echo statements for each case?

From Dev

How to avoid numerous If statements?

From Dev

if list is empty in that case how can i avoid null pointer exception in java?

From Dev

SQL - Why does adding CASE to an ORDER BY clause drastically cut performance? And how can I avoid this?

From Dev

How can I avoid this NullPointerException?

From Dev

How do I avoid using if statements with a large amount of variables in java

From Dev

Can I avoid case sensitivity problems when comparing element tagName?

From Dev

Can I avoid case sensitivity problems when comparing element tagName?

From Dev

How can use the result of case statements in to another manipulation?

From Dev

How can I reduce this long list of if statements?

From Dev

How can I combine these jQuery statements?

From Dev

How can I call a constructor after statements?

From Dev

How can I refactor a set of ugly if statements?

From Dev

How can I combine these jQuery statements?

From Dev

How can I DRY this series of conditional statements?

From Dev

How can I combine these two statements?

From Dev

How can I write regex for the following statements?

Related Related

  1. 1

    How can I avoid using instanceof in this case?

  2. 2

    How can i avoid ArrayIndexOutOfBoundsException in this case?

  3. 3

    How can I make switch-case statements case insensitive?

  4. 4

    Java - How can I avoid using try catch statements

  5. 5

    How can I avoid if else statements within a for loop?

  6. 6

    How to avoid switch-case statements in Java

  7. 7

    How to avoid switch-case statements in Java

  8. 8

    PostgreSQL: How to avoid division by zero when using case statements

  9. 9

    How to avoid repetitive CASE statements in SQL WHILE loop doing INSERT

  10. 10

    PostgreSQL: How to avoid division by zero when using case statements

  11. 11

    VBA - How to avoid copy paste code in select-case statements?

  12. 12

    How can I avoid so many if statements in copying one object to another?

  13. 13

    How can I draw Hangman in Bash without embedding echo statements for each case?

  14. 14

    How to avoid numerous If statements?

  15. 15

    if list is empty in that case how can i avoid null pointer exception in java?

  16. 16

    SQL - Why does adding CASE to an ORDER BY clause drastically cut performance? And how can I avoid this?

  17. 17

    How can I avoid this NullPointerException?

  18. 18

    How do I avoid using if statements with a large amount of variables in java

  19. 19

    Can I avoid case sensitivity problems when comparing element tagName?

  20. 20

    Can I avoid case sensitivity problems when comparing element tagName?

  21. 21

    How can use the result of case statements in to another manipulation?

  22. 22

    How can I reduce this long list of if statements?

  23. 23

    How can I combine these jQuery statements?

  24. 24

    How can I call a constructor after statements?

  25. 25

    How can I refactor a set of ugly if statements?

  26. 26

    How can I combine these jQuery statements?

  27. 27

    How can I DRY this series of conditional statements?

  28. 28

    How can I combine these two statements?

  29. 29

    How can I write regex for the following statements?

HotTag

Archive