两个函数readMay
和readMaybe
具有相同的签名Read a => String -> Maybe a
。
它们之间有什么区别吗?如果是这样,他们是什么?哪两种功能应该首选?
答案 0 :(得分:6)
没有区别。以下是gulp.task('cssInject', ['styles'], function() {
browserSync.stream();
});
定义的方式:
readMay
这是-- | This function provides a more precise error message than 'readEither' from 'base'.
readEitherSafe :: Read a => String -> Either String a
readEitherSafe s = case [x | (x,t) <- reads s, ("","") <- lex t] of
[x] -> Right x
[] -> Left $ "no parse on " ++ prefix
_ -> Left $ "ambiguous parse on " ++ prefix
where
maxLength = 15
prefix = '\"' : a ++ if length s <= maxLength then b ++ "\"" else "...\""
where (a,b) = splitAt (maxLength - 3) s
readMay :: Read a => String -> Maybe a
readMay = eitherToMaybe . readEitherSafe
:
readMaybe
它们在中间错误消息中有所不同(-- | Parse a string using the 'Read' instance.
-- Succeeds if there is exactly one valid result.
-- A 'Left' value indicates a parse error.
--
-- @since 4.6.0.0
readEither :: Read a => String -> Either String a
readEither s =
case [ x | (x,"") <- readPrec_to_S read' minPrec s ] of
[x] -> Right x
[] -> Left "Prelude.read: no parse"
_ -> Left "Prelude.read: ambiguous parse"
where
read' =
do x <- readPrec
lift P.skipSpaces
return x
-- | Parse a string using the 'Read' instance.
-- Succeeds if there is exactly one valid result.
--
-- @since 4.6.0.0
readMaybe :: Read a => String -> Maybe a
readMaybe s = case readEither s of
Left _ -> Nothing
Right a -> Just a
显示输入),但结果将相同。
readEitherSafe
的{p> readMay
早于Safe
readMaybe
。除非您的Text.Read
版本低于4.6.0.0,否则请使用base
中的readMaybe
,因为它不需要其他软件包。