Create a parser that runs the resulting parser at the output of another parser and monadically joins the results

The following type and function is set up to parse the CSV field field in a string:

type Parser resultType = ParsecT String () Identity resultType
cell :: Parser String 

I performed the following function:

customCell :: String -> Parser res  -> Parser res
customCell typeName subparser = 
  cell
    >>= either (const $ unexpected typeName) 
               return . parse (subparser <* eof) ""

Although I cannot stop thinking that I am not using the Monad concept as much as desired, and that ultimately there is a better way to combine the result of the internal with the external parser, especially with regard to its failure.

Does anyone know how I can do this, or is this the code that needs to be executed?

PS - , , , , , ... , , .

PS2 - , , ?

+3
2

@ ... , Parsec, , (, ), (, many1, , , , , " ", "", " " ).

, , , .

, , , , , String , String ( ) . " ", Parsec .

, , (, , ), , Parsec .

- , cell, String, , . Parsec CSV :

import Text.Parsec
import Text.Parsec.String

-- | `csv cell` parses a CSV file each of whose elements is parsed by `cell`
csv :: Parser a -> Parser [[a]]
csv cell = many (row cell)

-- | `row cell` parses a newline-terminated row of comma separated
--   `cell`-expressions
row :: Parser a -> Parser [a]
row cell = sepBy cell (char ',') <* char '\n'

, :

customCell :: Parser Int
customCell = read <$> many1 digit

CSV :

> parse (csv customCell) "" "1,2,3\n4,5,6\n"
Right [[1,2,3],[4,5,6]]
>

, cell, - , , "" , , , - .

+4

, , Haskell, , . - , , , . Dang!

+2

Source: https://habr.com/ru/post/1685563/


All Articles