>>> stripPrefix "foo" "foobar" Just "bar"
>>> stripPrefix "foo" "foo" Just ""
>>> stripPrefix "foo" "barfoo" Nothing
>>> stripPrefix "foo" "barfoobaz" Nothing
>>> stripPrefix "foo" "foobar" Just "bar"
>>> stripPrefix "" "baz" Just "baz"
>>> stripPrefix "foo" "quux" NothingThis is particularly useful with the ViewPatterns extension to GHC, as follows:
{-# LANGUAGE ViewPatterns #-} import Data.Text as T fnordLength :: Text -> Int fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf fnordLength _ = -1
stripPrefix "foo" "foobar" == Just "bar" stripPrefix "" "baz" == Just "baz" stripPrefix "foo" "quux" == NothingThis is particularly useful with the ViewPatterns extension to GHC, as follows:
{-# LANGUAGE ViewPatterns #-} import Data.Text.Lazy as T fnordLength :: Text -> Int fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf fnordLength _ = -1
stripPrefix "/foo/" "/foo/bar/baz.txt" == Just "bar/baz.txt" stripPrefix "/foo/" "/bar/baz.txt" == NothingThis function operates on logical prefixes, rather than by counting characters. The prefix "/foo/bar/baz" is interpreted the path ("/foo/bar/", "baz"), and will be stripped accordingly:
stripPrefix "/foo/bar/baz" "/foo/bar/baz/qux" == Nothing stripPrefix "/foo/bar/baz" "/foo/bar/baz.txt" == Just ".txt"Since: 0.4.1
>>> stripPrefix "foo" "foobar" Just "bar"
>>> stripPrefix "foo" "foo" Just ""
>>> stripPrefix "foo" "barfoo" Nothing
>>> stripPrefix "foo" "barfoobaz" Nothing
> stripPrefix "foo" "foobar" Just "bar" > stripPrefix "abc" "foobar" Nothing
stripPrefix "foo" "foobar" == Just "bar" stripPrefix "" "baz" == Just "baz" stripPrefix "foo" "quux" == NothingThis is particularly useful with the ViewPatterns extension to GHC, as follows:
{-# LANGUAGE ViewPatterns #-} import Data.Text as T fnordLength :: JSString -> Int fnordLength (stripPrefix "fnord" -> Just suf) = T.length suf fnordLength _ = -1
isJust (stripPrefix m1 m2) == m1 `isPrefixOf` m2The value for any key k in the result is identical to the result of stripping the value for k in map m1 from the value for k in map m2:
all (\r -> Just (get k r) == stripPrefix (get k m1) (get k m2)) (stripPrefix m1 m2)If we append prefix m1 to the left-hand side of the result, we can always recover the original map m2:
all (\r -> m1 <> r == m2) (stripPrefix m1 m2)This function provides the definition of stripPrefix for the MonoidMap instance of LeftReductive.
>>> m1 = fromList [(1, "" ), (2, "i" ), (3, "pq" ), (4, "xyz")] >>> m2 = fromList [(1, "abc"), (2, "ijk"), (3, "pqr"), (4, "xyz")] >>> m3 = fromList [(1, "abc"), (2, "jk"), (3, "r"), (4, "")]
>>> stripPrefix m1 m2 == Just m3 True
>>> stripPrefix m2 m1 == Nothing TrueWith Sum Natural values:
>>> m1 = fromList [("a", 0), ("b", 1), ("c", 2), ("d", 3)] >>> m2 = fromList [("a", 3), ("b", 3), ("c", 3), ("d", 3)] >>> m3 = fromList [("a", 3), ("b", 2), ("c", 1), ("d", 0)]
>>> stripPrefix m1 m2 == Just m3 True
>>> stripPrefix m2 m1 == Nothing True