这只是丹尼尔的一个额外想法。看起来你Grids
只是Colors
一个小“网格”可能不会做太多,但创建一个Unbox
实例相对容易Color
。然后一个网格将包含一个未装箱的数组。在Grid.hs
您将导入Data.Vector.Unboxed
而不是Data.Vector
. 由于许多原因,这通常要好得多,但需要您Unbox a =>
对许多定义进行限制。如果您想制作或“映射”到充满另一种类型的东西的网格中,这可能会产生后果Color
,除非它有一个Unbox
实例。
下面我只是添加了 TH 咒语vector-th-unbox
(我最近刚刚了解了那个包,并借此机会再次测试它)和两个必要的定义。在Data.Vector.Unboxed.Base
.
{-#LANGUAGE TemplateHaskell, TypeFamilies, MultiParamTypeClasses#-}
module Color where
import Display
import Data.Vector.Unboxed.Deriving
import qualified Data.Vector.Unboxed as V
import qualified Data.Vector.Generic as G
import qualified Data.Vector.Generic.Mutable as M
import Data.Word (Word8)
data Color = Yellow | Red | Green | Blue | Empty
deriving (Show, Eq, Ord, Enum, Bounded)
fromColor :: Color -> Word8
{-# INLINE fromColor #-}
fromColor = fromIntegral . fromEnum
toColor :: Word8 -> Color
{-# INLINE toColor #-}
toColor x | x < 5 = toEnum (fromIntegral x)
toColor _ = Empty
derivingUnbox "Color"
[t| Color -> Word8 |]
[| fromColor |]
[| toColor |]
-- test
colorCycle :: Int -> V.Vector Color
colorCycle n = V.unfoldr colorop 0 where
colorop m | m < n = Just (toColor (fromIntegral (m `mod` 5)),m+1)
colorop _ = Nothing
-- *Colour> colorCycle 12
-- fromList [Yellow,Red,Green,Blue,Empty,Yellow,
-- Red,Green,Blue,Empty,Yellow,Red]
colorBlack = "\ESC[0;30m"
colorRed = "\ESC[0;31m"
colorGreen = "\ESC[0;32m"
colorYellow = "\ESC[0;33m"
colorBlue = "\ESC[0;34m"
instance Display Color where
display Red = colorRed ++ "R" ++ colorBlack
display Green = colorGreen ++ "G" ++ colorBlack
display Yellow = colorYellow ++ "Y" ++ colorBlack
display Blue = colorBlue ++ "B" ++ colorBlack
display Empty = "."
编辑:在vector-th-unbox
前面的版本中0.1
使用了以下模板:
derivingUnbox "Color"
[d| instance Unbox' (Color) Word8 |]
[| fromColor |]
[| toColor |]