From e14fff7d4b04c37155acf046b7b3f350cf6ad3b6 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Thu, 6 Aug 2020 00:15:31 +0100 Subject: Support Transforms.optimize Partially optimize inputs and document rules for further optimizations we can make. --- scratch/brilliant/Spec.hs | 7 +++++++ scratch/brilliant/Transforms.hs | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/scratch/brilliant/Spec.hs b/scratch/brilliant/Spec.hs index d5ad6234ddc0..f2450bdd9a5c 100644 --- a/scratch/brilliant/Spec.hs +++ b/scratch/brilliant/Spec.hs @@ -71,3 +71,10 @@ main = hspec $ do , Utils.rotate (-3) ['A','S','D','F','G','H','J','K','L',';'] , Utils.rotate (-3) ['Z','X','C','V','B','N','M',',','.','/'] ] + + describe "Transforms.optimize" $ do + it "removes superfluous horizontal transformations" $ do + Transforms.optimize [HorizontalFlip, HorizontalFlip] == [] + + it "removes superfluous vertical transformations" $ do + Transforms.optimize [VerticalFlip, VerticalFlip] == [] diff --git a/scratch/brilliant/Transforms.hs b/scratch/brilliant/Transforms.hs index 810c5f960ae1..d8df8f8372e0 100644 --- a/scratch/brilliant/Transforms.hs +++ b/scratch/brilliant/Transforms.hs @@ -33,6 +33,18 @@ command = vertical Nothing -> pure $ Shift n Just _ -> pure $ Shift (-1 * n) +-- | Attempt to remove redundant transformations. +-- | Here are some rules that I'd like to support but may not have time for: +-- | - All even-numbered flips (w/o intermittent shifts) can become zero +-- | - All odd-numbered flips (w/o intermittent shifts) can become 1 +-- | - All shifts can be be reduce to the absolute value of shifts +optimize :: [Transform] -> [Transform] +optimize [] = [] +optimize [x] = [x] +optimize (VerticalFlip:VerticalFlip:xs) = optimize xs +optimize (HorizontalFlip:HorizontalFlip:xs) = optimize xs +optimize xs = xs + fromString :: String -> Maybe [Transform] fromString x = case readP_to_S (manyTill command eof) x of -- cgit 1.4.1