Last active
October 5, 2020 01:22
-
-
Save jlavelle/a1ea6a7eeac979f0a769cb3d7f992a44 to your computer and use it in GitHub Desktop.
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
{-# OPTIONS_GHC -fno-warn-missing-methods #-} | |
{-# LANGUAGE OverloadedLists #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE RankNTypes #-} | |
module Main where | |
import Data.Monoid (Ap(..), Sum(..)) | |
import Data.Foldable (traverse_) | |
import Control.Applicative (ZipList(..)) | |
import GHC.Exts (IsList(..)) | |
il :: IsList l => (forall a. [a] -> [a]) -> l -> l | |
il f = fromList . f . toList | |
instance Enum a => Enum (Sum a) where | |
toEnum = Sum . toEnum | |
fromEnum = fromEnum . getSum | |
-- this one is coming in base 4.15.0.0 | |
instance IsList (ZipList a) where | |
type Item (ZipList a) = a | |
fromList = ZipList | |
toList = getZipList | |
instance IsList (f a) => IsList (Ap f a) where | |
type Item (Ap f a) = Item (f a) | |
fromList = Ap . fromList | |
toList = toList . getAp | |
data Fizzer = N Int | Fizz | Buzz | FizzBuzz deriving Eq | |
instance Show Fizzer where | |
show (N x) = show x | |
show Fizz = "Fizz" | |
show Buzz = "Buzz" | |
show FizzBuzz = "Fizz Buzz" | |
instance Num Fizzer where | |
fromInteger = N . fromInteger | |
Fizz + Buzz = FizzBuzz | |
x + 0 = x | |
_ + x = x | |
instance Enum Fizzer where | |
toEnum = N | |
fromEnum (N x) = x | |
fizzBuzz :: Ap ZipList (Sum Fizzer) | |
fizzBuzz = [1..] <> il cycle [0,0,Sum Fizz] <> il cycle [0,0,0,0,Sum Buzz] | |
main :: IO () | |
main = traverse_ (print . getSum) $ il (take 100) fizzBuzz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment