<commit_before>module Test (main) where

import Language.Fay.Prelude hiding (return, ($), (.), (>>=), (++), map, foldr, find)
import Language.Fay.JQuery
import Language.Fay.FFI

(>=>)       :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c)
f >=> g     = \x -> f x >>= g

myMapM            :: Monad m => (a -> m b) -> [a] -> m [b]
myMapM f as       =  mySequence (map f as)

mySequence    :: Monad m => [m a] -> m [a]
mySequence ms = let k m m' = do { x <- m; xs <- m'; return (x:xs) } in
                foldr k (return []) ms

tableData = [ ["S", "Soprano"]
            , ["A", "Alto"]
            , ["T", "Tenor"]
            , ["B", "Bass"]
            ]

buildCell :: String -> Fay JQuery
buildCell cellData = do
    cell <- select "<td/>"
    setText cellData cell

buildRow :: [String] -> Fay JQuery
buildRow rowData = do
    row <- select "<tr/>"
    myMapM (buildCell >=> appendToJQuery row) rowData
    return row

buildTable :: [[String]] -> Fay JQuery
buildTable rowsData = do
    table <- select "<table/>"
    myMapM (buildRow >=> appendToJQuery table) rowsData
    return table

dir :: (Foreign a) => a -> Fay JQuery
dir = ffi "console.dir(%1)"

clog :: String -> Fay JQuery
clog = ffi "console.log(%1)"

main :: Fay ()
main = ready $ do
    div <- select "#replacementDiv"
    table <- buildTable tableData
    replaceWithJQuery table div
    addZebraStriping table
    return ()

isDivisibleBy :: Double -> Double -> Bool
--isDivisibleBy num divisor = let divided = num/divisor in divided - (fromIntegral $ floor divided) == 0
--isDivisibleBy num divisor = (div (floor num) (floor divisor)) * (floor divisor) == (floor num)
isDivisibleBy num divisor = if num == 0 then True
                            else if num < 0 then False
                            else isDivisibleBy (num - divisor) divisor

zebraStripeRows :: Double -> String -> Fay String
zebraStripeRows index _ = return $ if isDivisibleBy index 2 then "odd" else "even"

addZebraStriping :: JQuery -> Fay JQuery
addZebraStriping table = do
    --addClassWith zebraStripeRows rows
    evenRows <- findSelector "tr:even" table
    addClass "even" evenRows
    oddRows <- findSelector "tr:odd" table
    addClass "odd" oddRows

{--
manipulateP :: JQuery -> Fay JQuery
manipulateP = setText "It's been replaced!" >=>
              --setStyle "background-color" "red" >=>
              addClass "asdf" >=>
              addClassWith addTableRowClasses
--}
<commit_msg>Added NoImplicitPrelude directive, made type signatures more restrictive to match how the monad functions are implemented in Fay.<commit_after>{-# LANGUAGE NoImplicitPrelude #-}

module Test (main) where

import Language.Fay.Prelude
import Language.Fay.JQuery
import Language.Fay.FFI

(>=>)       :: (a -> Fay b) -> (b -> Fay c) -> (a -> Fay c)
f >=> g     = \x -> f x >>= g

myMapM            :: (a -> Fay b) -> [a] -> Fay [b]
myMapM f as       =  mySequence (map f as)

mySequence    :: [Fay a] -> Fay [a]
mySequence ms = let k m m' = do { x <- m; xs <- m'; return (x:xs) } in
                foldr k (return []) ms

tableData = [ ["S", "Soprano"]
            , ["A", "Alto"]
            , ["T", "Tenor"]
            , ["B", "Bass"]
            ]

buildCell :: String -> Fay JQuery
buildCell cellData = do
    cell <- select "<td/>"
    setText cellData cell

buildRow :: [String] -> Fay JQuery
buildRow rowData = do
    row <- select "<tr/>"
    myMapM (buildCell >=> appendToJQuery row) rowData
    return row

buildTable :: [[String]] -> Fay JQuery
buildTable rowsData = do
    table <- select "<table/>"
    myMapM (buildRow >=> appendToJQuery table) rowsData
    return table

dir :: (Foreign a) => a -> Fay JQuery
dir = ffi "console.dir(%1)"

clog :: String -> Fay JQuery
clog = ffi "console.log(%1)"

main :: Fay ()
main = ready $ do
    div <- select "#replacementDiv"
    table <- buildTable tableData
    replaceWithJQuery table div
    addZebraStriping table
    return ()

isDivisibleBy :: Double -> Double -> Bool
--isDivisibleBy num divisor = let divided = num/divisor in divided - (fromIntegral $ floor divided) == 0
--isDivisibleBy num divisor = (div (floor num) (floor divisor)) * (floor divisor) == (floor num)
isDivisibleBy num divisor = if num == 0 then True
                            else if num < 0 then False
                            else isDivisibleBy (num - divisor) divisor

zebraStripeRows :: Double -> String -> Fay String
zebraStripeRows index _ = return $ if isDivisibleBy index 2 then "odd" else "even"

addZebraStriping :: JQuery -> Fay JQuery
addZebraStriping table = do
    --addClassWith zebraStripeRows rows
    evenRows <- findSelector "tr:even" table
    addClass "even" evenRows
    oddRows <- findSelector "tr:odd" table
    addClass "odd" oddRows

{--
manipulateP :: JQuery -> Fay JQuery
manipulateP = setText "It's been replaced!" >=>
              --setStyle "background-color" "red" >=>
              addClass "asdf" >=>
              addClassWith addTableRowClasses
--}

{'fraction_non_alphanumeric': 0.13687600644122383, 'fraction_numerical': 0.0024154589371980675, 'mean_word_length': 2.511660777385159, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 90, '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': '3028729', 'n_tokens_mistral': 1687, 'n_tokens_neox': 1607, 'n_words': 539}