Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prisms for working with prefixed and suffixed text #982

Merged
merged 7 commits into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Allow building with GHC 9.2.
* Define `_CharTyLit` in `Language.Haskell.TH.Lens` when building with
`template-haskell-2.18` or later.
* Add prisms for working with text with prefixes or suffices in
`Data.Text.Strict.Lens` and `Data.Text.Lazy.Lens`
RyanGlScott marked this conversation as resolved.
Show resolved Hide resolved

5.0.1 [2021.02.24]
------------------
Expand Down
30 changes: 30 additions & 0 deletions src/Data/Text/Lazy/Lens.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE RankNTypes #-}
#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
Expand All @@ -17,6 +18,7 @@
----------------------------------------------------------------------------
module Data.Text.Lazy.Lens
( packed, unpacked
, prefixed, suffixed
, _Text
, text
, builder
Expand Down Expand Up @@ -82,6 +84,34 @@ unpacked :: Iso' Text String
unpacked = iso Text.unpack Text.pack
{-# INLINE unpacked #-}

-- | A 'Prism' stripping a prefix from a text when used as a 'Traversal', or
-- prepending that prefix when run backwards:
--
-- >>> "preview" ^? prefixed "pre"
-- Just "view"
--
-- >>> "review" ^? prefixed "pre"
-- Nothing
--
-- >>> prefixed "pre" # "amble"
-- "preamble"
prefixed :: Text -> Prism' Text Text
prefixed p = prism' (p <>) (Text.stripPrefix p)

-- | A 'Prism' stripping a suffix from a text when used as a 'Traversal', or
-- appending that suffix when run backwards:
--
-- >>> "review" ^? suffixed "view"
-- Just "re"
--
-- >>> "review" ^? suffixed "tire"
-- Nothing
--
-- >>> suffixed ".o" # "hello"
-- "hello.o"
suffixed :: Text -> Prism' Text Text
suffixed q = prism' (<> q) (Text.stripSuffix q)

-- | This is an alias for 'unpacked' that makes it clearer how to use it with @('#')@.
--
-- @
Expand Down
30 changes: 30 additions & 0 deletions src/Data/Text/Strict/Lens.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
#if __GLASGOW_HASKELL__ >= 710
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ViewPatterns #-}
Expand All @@ -16,6 +17,7 @@
----------------------------------------------------------------------------
module Data.Text.Strict.Lens
( packed, unpacked
, prefixed, suffixed
, builder
, text
, utf8
Expand Down Expand Up @@ -84,6 +86,34 @@ unpacked :: Iso' Text String
unpacked = iso Strict.unpack Strict.pack
{-# INLINE unpacked #-}

-- | A 'Prism' stripping a prefix from a text when used as a 'Traversal', or
-- prepending that prefix when run backwards:
--
-- >>> "preview" ^? prefixed "pre"
-- Just "view"
--
-- >>> "review" ^? prefixed "pre"
-- Nothing
--
-- >>> prefixed "pre" # "amble"
-- "preamble"
prefixed :: Text -> Prism' Text Text
prefixed p = prism' (p <>) (Strict.stripPrefix p)

-- | A 'Prism' stripping a suffix from a text when used as a 'Traversal', or
-- appending that suffix when run backwards:
--
-- >>> "review" ^? suffixed "view"
-- Just "re"
--
-- >>> "review" ^? suffixed "tire"
-- Nothing
--
-- >>> suffixed ".o" # "hello"
-- "hello.o"
suffixed :: Text -> Prism' Text Text
suffixed q = prism' (<> q) (Strict.stripSuffix q)

-- | This is an alias for 'unpacked' that makes it more obvious how to use it with '#'
--
-- >> _Text # "hello" -- :: Text
Expand Down
4 changes: 2 additions & 2 deletions tests/properties.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import Test.QuickCheck
import Test.Framework
import Test.Framework.Providers.QuickCheck2
import Data.Char (isAlphaNum, isAscii, toUpper)
import Data.Text.Strict.Lens
import qualified Data.Text.Strict.Lens as Text
RyanGlScott marked this conversation as resolved.
Show resolved Hide resolved
import Data.List.Lens
import GHC.Exts (Constraint)
import Numeric (showHex, showOct, showSigned)
Expand Down Expand Up @@ -86,7 +86,7 @@ prop__Just = isPrism (_Just :: Prism' (Maybe Int) Int)
prop_prefixed s = isPrism (prefixed s :: Prism' String String)

-- Data.Text.Lens
prop_text s = s^.packed.from packed == s
prop_text s = s^.Text.packed.from Text.packed == s
--prop_text = isIso packed

-- Numeric.Lens
Expand Down