<commit_before>{-# LANGUAGE OverloadedStrings, LambdaCase #-}
module React.Interpret where

import Control.Monad

import GHCJS.Prim
import GHCJS.Types

import React.Events
import React.Imports
import React.Types


element :: (RawAttrs -> ReactArray -> IO ForeignNode)
        -> Attrs
        -> [(RawEvent -> Maybe (IO ()), EvtType)]
        -> [ForeignNode]
        -> IO ForeignNode
element constructor attrs handlers content = do
    attr <- js_empty_object
    mapM_ (setField attr) attrs
    mapM_ (makeHandler attr) handlers

    children <- js_ReactArray_empty
    mapM_ (js_ReactArray_push children) content
    constructor attr children


setField :: RawAttrs -> (JSString, JSON) -> IO ()
setField attr (fld, Num v) = js_set_field_Double attr fld v
setField attr (fld, Str v) = js_set_field_String attr fld v
setField attr (fld, Bool True) = js_set_field_True attr fld
setField attr (fld, Bool False) = js_set_field_False attr fld
setField attr (fld, Arr arr) = do
    jsArr <- js_empty_arr
    let arr' = zip arr [0..]
    forM_ arr' $ \(val, ix) -> setIx jsArr ix val
    js_set_field_Arr attr fld jsArr
setField attr (fld, Dict vs) = do
    subObj <- js_empty_object
    mapM_ (setField subObj) vs
    js_set_field_Obj attr fld subObj

-- TODO this seems wrong
setField attr (fld, Null) = return ()

setIx :: RawAttrs -> Int -> JSON -> IO ()
setIx arr i (Num v) = js_set_ix_Double arr i v
setIx arr i (Str v) = js_set_ix_String arr i v
setIx arr i (Bool v) = js_set_ix_Bool arr i v
setIx arr i (Arr setArr) = do
    jsArr <- js_empty_arr
    let setArr' = zip [0..] setArr
    mapM_ (uncurry (setIx jsArr)) setArr'
    js_set_ix_Arr arr i jsArr
setIx arr i (Dict d) = do
    subObj <- js_empty_object
    mapM_ (setField subObj) d
    js_set_ix_Obj arr i subObj

-- TODO
setIx arr i Null = return ()


interpret :: Monad m
          => ReactT state sig anim m ()<commit_msg>Change setField implementation to match setIx.<commit_after>{-# LANGUAGE OverloadedStrings, LambdaCase #-}
module React.Interpret where

import Control.Monad

import GHCJS.Prim
import GHCJS.Types

import React.Events
import React.Imports
import React.Types


element :: (RawAttrs -> ReactArray -> IO ForeignNode)
        -> Attrs
        -> [(RawEvent -> Maybe (IO ()), EvtType)]
        -> [ForeignNode]
        -> IO ForeignNode
element constructor attrs handlers content = do
    attr <- js_empty_object
    mapM_ (setField attr) attrs
    mapM_ (makeHandler attr) handlers

    children <- js_ReactArray_empty
    mapM_ (js_ReactArray_push children) content
    constructor attr children


setField :: RawAttrs -> (JSString, JSON) -> IO ()
setField attr (fld, Num v) = js_set_field_Double attr fld v
setField attr (fld, Str v) = js_set_field_String attr fld v
setField attr (fld, Bool True) = js_set_field_True attr fld
setField attr (fld, Bool False) = js_set_field_False attr fld
setField attr (fld, Arr arr) = do
    jsArr <- js_empty_arr
    let arr' = zip [0..] arr
    mapM_ (uncurry (setIx jsArr)) arr'
    js_set_field_Arr attr fld jsArr
setField attr (fld, Dict vs) = do
    subObj <- js_empty_object
    mapM_ (setField subObj) vs
    js_set_field_Obj attr fld subObj

-- TODO this seems wrong
setField attr (fld, Null) = return ()

setIx :: RawAttrs -> Int -> JSON -> IO ()
setIx arr i (Num v) = js_set_ix_Double arr i v
setIx arr i (Str v) = js_set_ix_String arr i v
setIx arr i (Bool v) = js_set_ix_Bool arr i v
setIx arr i (Arr setArr) = do
    jsArr <- js_empty_arr
    let setArr' = zip [0..] setArr
    mapM_ (uncurry (setIx jsArr)) setArr'
    js_set_ix_Arr arr i jsArr
setIx arr i (Dict d) = do
    subObj <- js_empty_object
    mapM_ (setField subObj) d
    js_set_ix_Obj arr i subObj

-- TODO
setIx arr i Null = return ()


interpret :: Monad m
          => ReactT state sig anim m ()
{'fraction_non_alphanumeric': 0.09062170706006323, 'fraction_numerical': 0.001053740779768177, 'mean_word_length': 3.2095343680709534, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 30, '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': '774894', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1343, 'n_words': 462}