-
-
Save Wizek/1a57f86cda78cf905d6f498996bf6af1 to your computer and use it in GitHub Desktop.
Emulating the elegance of JavaScript's `new Array` in Haskell
This file contains hidden or 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
{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, TypeApplications, TypeFamilies #-} | |
module EvilList where | |
import Data.Typeable | |
type family EvilListContent a where | |
EvilListContent Int = () | |
EvilListContent a = a | |
-- | Emulating the elegance of JavaScript's @new Array@. | |
-- | |
-- >>> evilList (5 :: Int) | |
-- [(),(),(),(),()] | |
-- | |
-- >>> evilList "hello" | |
-- ["hello"] | |
evilList :: forall a. (Typeable a, Typeable (EvilListContent a)) => a -> [EvilListContent a] | |
evilList a | |
| Just Refl <- eqT @a @Int = replicate a () | |
| Just Refl <- eqT @a @(EvilListContent a) = [a] | |
| otherwise = undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment