<commit_before>module Conway where
import Data.List (nub, intersect)
import Control.Monad (forM_)

type Cell = (Int, Int)

--basic glider
startingCells :: [Cell]
startingCells = [(1,0), (2,1), (0,2), (1,2), (2,2), --glider
	(5,5),(5,4),(5,6)]

nextGeneration :: [Cell] -> [Cell]
nextGeneration cs = nub $ concat $ map (newAlive cs) cs

newAlive :: [Cell] -> Cell -> [Cell]
newAlive cs c = filter (isAlive cs) $ c:(neighBours c)

neighBours :: Cell -> [Cell]
neighBours (x,y) 
    = [(ax, ay) | 
        ax <- [x-1, x, x+1], 
        ay <- [y-1, y, y+1],
        ax /= x || ay /= y]

-- takes a gameboard and cell, and returns if it's alive next gen 
-- or not
isAlive :: [Cell] -> Cell -> Bool
isAlive cs c 
    | aliveCount < 2 = False
    | aliveCount == 2 = (c `elem` cs)
    | aliveCount == 3 = True
    | aliveCount > 3 = False
    | otherwise = False
    where
        aliveCount = length $ liveNeighbours cs c

-- takes a list of live cells, and a coord, and 
-- returns a list of the neighbours that are currently alive
liveNeighbours :: [Cell] -> Cell -> [Cell]
liveNeighbours cs c
    = intersect cs $ neighBours c

maxY :: [Cell] -> Int
maxX :: [Cell] -> Int
minY :: [Cell] -> Int
minX :: [Cell] -> Int
maxY = maximum . (map (snd))
maxX = maximum . (map (fst))
minY = minimum . (map (snd))
minX = minimum . (map (fst))

printCells :: [Cell] -> IO ()
printCells [] = return ()
printCells cells =  
	forM_ [(minY cells) .. (maxY cells)] (\y -> 
		forM_ [(minX cells) .. (maxX cells)] (\x -> 
			if ((x,y) `elem` cells) then 
				putStr "X" 
			else 
				putStr ".")
		>> putStrLn "")

runLife :: (Show n, Enum n) => n -> [Cell] -> IO ()
runLife n cells = do
	putStrLn ("generation " ++ show n)
	getLine
	printCells cells
	runLife (succ n) $ nextGeneration cells
<commit_msg>Fix up suggestions from hlint<commit_after>module Conway where
import Data.List (nub, intersect)
import Control.Monad (forM_)

type Cell = (Int, Int)

--basic glider
startingCells :: [Cell]
startingCells = [(1,0), (2,1), (0,2), (1,2), (2,2), --glider
	(5,5),(5,4),(5,6)]

nextGeneration :: [Cell] -> [Cell]
nextGeneration cs = nub $ concatMap (newAlive cs) cs

newAlive :: [Cell] -> Cell -> [Cell]
newAlive cs c = filter (isAlive cs) $ c:neighBours c

neighBours :: Cell -> [Cell]
neighBours (x,y) 
    = [(ax, ay) | 
        ax <- [x-1, x, x+1], 
        ay <- [y-1, y, y+1],
        ax /= x || ay /= y]

-- takes a gameboard and cell, and returns if it's alive next gen 
-- or not
isAlive :: [Cell] -> Cell -> Bool
isAlive cs c 
    | aliveCount < 2 = False
    | aliveCount == 2 = c `elem` cs
    | aliveCount == 3 = True
    | aliveCount > 3 = False
    | otherwise = False
    where
        aliveCount = length $ liveNeighbours cs c

-- takes a list of live cells, and a coord, and 
-- returns a list of the neighbours that are currently alive
liveNeighbours :: [Cell] -> Cell -> [Cell]
liveNeighbours cs c
    = intersect cs $ neighBours c

maxY :: [Cell] -> Int
maxX :: [Cell] -> Int
minY :: [Cell] -> Int
minX :: [Cell] -> Int
maxY = maximum . map snd
maxX = maximum . map fst
minY = minimum . map snd
minX = minimum . map fst

printCells :: [Cell] -> IO ()
printCells [] = return ()
printCells cells =  
	forM_ [(minY cells) .. (maxY cells)] (\y -> 
            forM_ [(minX cells) .. (maxX cells)] (\x -> 
                putStr (if (x,y) `elem` cells then 
                            "X" else "."))
            >> putStrLn "")

runLife :: (Show n, Enum n) => n -> [Cell] -> IO ()
runLife n cells = do
	putStrLn ("generation " ++ show n)
	getLine
	printCells cells
	runLife (succ n) $ nextGeneration cells

{'fraction_non_alphanumeric': 0.16583333333333333, 'fraction_numerical': 0.013333333333333334, 'mean_word_length': 2.630040322580645, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5460501', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1300, 'n_words': 448}