-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
invert: rename from representing-enums, and remove Rep
- Loading branch information
1 parent
0f9376c
commit d5ea969
Showing
5 changed files
with
71 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
|
||
import GHC.Generics (Generic) | ||
import Generics.Deriving.Enum (GEnum (genum)) | ||
import qualified Data.Map.Strict as Map | ||
|
||
data Product = Basic | Standard | Pro | ||
deriving stock (Generic, Show) | ||
deriving anyclass GEnum | ||
|
||
data Frequency = Monthly | Annual | ||
deriving stock (Generic, Show) | ||
deriving anyclass GEnum | ||
|
||
data Bill = Bill Product Frequency | ||
deriving stock (Generic, Show) | ||
deriving anyclass GEnum | ||
|
||
encodeProduct :: Product -> String | ||
encodeProduct = \case | ||
Basic -> "p1" | ||
Standard -> "p2" | ||
Pro -> "p3" | ||
|
||
encodeBill :: Bill -> Integer | ||
encodeBill = \case | ||
Bill Basic Monthly -> 10 | ||
Bill Basic Annual -> 11 | ||
Bill Standard Monthly -> 20 | ||
Bill Standard Annual -> 21 | ||
Bill Pro Monthly -> 30 | ||
Bill Pro Annual -> 31 | ||
|
||
invert :: (GEnum a, Ord b) => (a -> b) -> b -> Maybe a | ||
invert f = | ||
let | ||
reverseMap = foldMap (\a -> Map.singleton (f a) a) genum | ||
in | ||
\b -> Map.lookup b reverseMap | ||
|
||
decodeProduct :: String -> Maybe Product | ||
decodeProduct = invert encodeProduct | ||
|
||
decodeBill :: Integer -> Maybe Bill | ||
decodeBill = invert encodeBill | ||
|
||
main = | ||
do | ||
putStrLn (encodeProduct Basic) | ||
putStrLn (encodeProduct Standard) | ||
|
||
putStrLn (show (decodeProduct "p1")) | ||
putStrLn (show (decodeProduct "xyz")) | ||
|
||
putStrLn (show (encodeBill (Bill Basic Annual))) | ||
putStrLn (show (encodeBill (Bill Pro Monthly))) | ||
|
||
putStrLn (show (decodeBill 31)) | ||
putStrLn (show (decodeBill 50)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
p1 | ||
p2 | ||
Just Basic | ||
Nothing | ||
11 | ||
30 | ||
Just (Bill Pro Annual) | ||
Nothing |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters