次のようなAttoparsecパーサーがあります。
myParser :: Parser Text
myParser = char '"' *> takeWhile (not . isspace) <* char '"'
このパーサーをオプションにしたいのでJust txt
、パーサーが一致するかどうかを返す関数Nothing
、つまり署名の関数を取得します。
myMaybeParser :: Parser (Maybe Text)
これどうやってするの?
次のようなAttoparsecパーサーがあります。
myParser :: Parser Text
myParser = char '"' *> takeWhile (not . isspace) <* char '"'
このパーサーをオプションにしたいのでJust txt
、パーサーが一致するかどうかを返す関数Nothing
、つまり署名の関数を取得します。
myMaybeParser :: Parser (Maybe Text)
これどうやってするの?
これoption
には Applicative インスタンスを使用できます。Parser
-- Make a parser optional, return Nothing if there is no match
maybeOption :: Parser a -> Parser (Maybe a)
maybeOption p = option Nothing (Just <$> p)
その後、次のように使用できます。
myMaybeParser = maybeOption myParser