Skip to content

Instantly share code, notes, and snippets.

@Wizek
Forked from ion1/0readme.md
Created October 8, 2018 12:19
Show Gist options
  • Save Wizek/1a57f86cda78cf905d6f498996bf6af1 to your computer and use it in GitHub Desktop.
Save Wizek/1a57f86cda78cf905d6f498996bf6af1 to your computer and use it in GitHub Desktop.
Emulating the elegance of JavaScript's `new Array` in Haskell
{-# 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