<commit_before>{-# LANGUAGE DeriveFunctor #-}
import Data.Word
import Data.Bits

import Control.Applicative
import Control.Monad
import Control.Monad.State

import Text.Parsec hiding ((<|>))
import Text.Parsec.String (Parser)
import Text.Printf

data Tree a
    = Leaf a
    | Branch (Tree a) a (Tree a)
      deriving (Functor, Show)
      
type Rule a = a -> a -> a -> a -> a

type Dir = Char

makeRule :: Word16 -> Rule Bool
makeRule ruleEncoding v1 v2 v3 v4 = testBit ruleEncoding pos
    where pos = sum (ZipWith (*) (map fromEnum [v4, v3, v2, v1]) (iterate (*2 ) 1))
    
getValue :: Tree a -> a
getValue (Left v) = v
getValue (Branch _ v _) = v<commit_msg>add parse and rule in the-tree-of-life<commit_after>{-# LANGUAGE DeriveFunctor #-}
import Data.Word
import Data.Bits

import Control.Applicative
import Control.Monad
import Control.Monad.State

import Text.Parsec hiding ((<|>))
import Text.Parsec.String (Parser)
import Text.Printf

data Tree a
    = Leaf a
    | Branch (Tree a) a (Tree a)
      deriving (Functor, Show)
      
type Rule a = a -> a -> a -> a -> a

type Dir = Char

makeRule :: Word16 -> Rule Bool
makeRule ruleEncoding v1 v2 v3 v4 = testBit ruleEncoding pos
    where pos = sum (ZipWith (*) (map fromEnum [v4, v3, v2, v1]) (iterate (*2 ) 1))
    
getValue :: Tree a -> a
getValue (Left v) = v
getValue (Branch _ v _) = v

nextTree :: Rule Bool -> Tree Bool -> Tree Bool
nextTree rule = withParent False
    where withParent p t = case t of
        Leaf a -> Leaf (rule p False v False)
        Branch l a r -> Branch (withParent a l)
                               (rule p (getValue l) a (getValue r))
                               (withParent a r)
                               
parseTree :: Parser (Tree Bool)
parseTree = parseBranch <|> parseLeaf
    where parseCell = (char 'X' >> return True) <|> (char '.' >> return False)
          parseLeaf = Leaf <$> parseCell
          parseBranch = between (char '(') (char ')')
                                (Branch <$> parseTree <*> (space *> parseCell *< space) <*> parseTree)
{'fraction_non_alphanumeric': 0.11850412821758136, 'fraction_numerical': 0.011656143759106362, 'mean_word_length': 2.4390651085141903, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 32, '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': '3131814', 'n_tokens_mistral': 677, 'n_tokens_neox': 660, 'n_words': 245}