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 high order function for checking equality after transformation #238

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 10 additions & 0 deletions Data/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ module Data.Text
, isPrefixOf
, isSuffixOf
, isInfixOf
, transformEq

-- ** View patterns
, stripPrefix
Expand Down Expand Up @@ -1753,6 +1754,15 @@ isInfixOf needle haystack
isInfixOf (singleton n) h = S.elem n (S.stream h)
#-}

-- | The 'transformEq' function takes a `Text -> Text` function
-- and two 'Text's if the 'Text's are equal after transformation. This
-- function relies on fusion to avoid materialising the transformed 'Text'
transformEq :: (Text -> Text) -> Text -> Text -> Bool
transformEq f x y
| length x == length y = S.stream (f x) == S.stream (f y)
| otherwise = False
{-# INLINE [1] transformEq #-}

-------------------------------------------------------------------------------
-- * View patterns

Expand Down
2 changes: 2 additions & 0 deletions benchmarks/haskell/Benchmarks.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import qualified Benchmarks.Concat as Concat
import qualified Benchmarks.DecodeUtf8 as DecodeUtf8
import qualified Benchmarks.EncodeUtf8 as EncodeUtf8
import qualified Benchmarks.Equality as Equality
import qualified Benchmarks.TransformEq as TransformEq
import qualified Benchmarks.FileRead as FileRead
import qualified Benchmarks.FoldLines as FoldLines
import qualified Benchmarks.Mul as Mul
Expand Down Expand Up @@ -63,6 +64,7 @@ benchmarks = do
, Search.benchmark (tf "russian.txt") "принимая"
, Stream.benchmark (tf "russian.txt")
, WordFrequencies.benchmark (tf "russian.txt")
, TransformEq.benchmark
]

-- Program-like benchmarks
Expand Down
30 changes: 30 additions & 0 deletions benchmarks/haskell/Benchmarks/TransformEq.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- | Compare a string after a transformation
--
-- Tested in this benchmark:
--
-- * Comparison of transformed strings via transformEq
--
module Benchmarks.TransformEq
( benchmark
) where

import Criterion (Benchmark, bgroup, bench, whnf)
import Data.Function (on)
import Data.Text (transformEq, toCaseFold, pack)

benchmark :: IO Benchmark
benchmark = do
let
equiv = (pack "Fooooooo", pack "fOOOOOOO")
notEq = (pack "fooooooo", pack "barrrrrr")
lengthNotEq = (pack "foo", pack "foooooooo")
eq = uncurry ((==) `on` toCaseFold)
transEq = uncurry $ transformEq toCaseFold
return $ bgroup "transformEq"
[ bench "Text ==: Eq" $ whnf eq equiv
, bench "Text transformEq: Eq" $ whnf transEq equiv
, bench "Text ==: Not Eq" $ whnf eq notEq
, bench "Text transformEq: Not Eq" $ whnf transEq notEq
, bench "Text ==: Not Length" $ whnf eq lengthNotEq
, bench "Text transformEq: Not Length" $ whnf transEq lengthNotEq
]
1 change: 1 addition & 0 deletions benchmarks/text-benchmarks.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ executable text-benchmarks
Benchmarks.Replace
Benchmarks.Search
Benchmarks.Stream
Benchmarks.TransformEq
Benchmarks.WordFrequencies

-- Source code for IUT (implementation under test)
Expand Down