Skip to content

Instantly share code, notes, and snippets.

@kephas
Created October 16, 2023 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kephas/9e486ce0b6a4c2a9c06eadb1615680d2 to your computer and use it in GitHub Desktop.
Save kephas/9e486ce0b6a4c2a9c06eadb1615680d2 to your computer and use it in GitHub Desktop.
Type-safe refactoring with ad-hoc polymorphism
module Adhoc where
data ClientIdentifier
= Admin
| User String
deriving (Eq, Show)
data Settings = Settings
{settingAllowList :: [ClientIdentifier]}
deriving (Show)
clientIsAllowed :: Settings -> ClientIdentifier -> Bool
clientIsAllowed settings client =
clientIsInAllowList (settingAllowList settings) client
clientIsInAllowList :: Foldable t => t ClientIdentifier -> ClientIdentifier -> Bool
clientIsInAllowList allowList client =
foldr checkOne False allowList
where
checkOne :: ClientIdentifier -> Bool -> Bool
checkOne _ True = True
checkOne otherClient False = client == otherClient
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment