How to render Dynamic of list (`Dynamics t [a]`)?

122 views Asked by At

I can draw element for Dynamic t Item but don't know how to draw element of Dynamic t [Item]

import Reflex.Dom

data ItemType
  = Apple
  | Banana
 deriving (Eq, Show)

-- In the real-world, this function build complex DOM. Oversimplified here for MCVE
-- 
itemElDyn :: MonadWidget t m => Dynamic t ItemType -> m ()
itemElDyn = display

type Items = [ItemType]

-- listItemElDyn :: MonadWidget t m => Dynamic t Items -> m ()
-- listItemElDyn = mapM_ display . sequenceA

-- listItemElDyn lsDyn = do
--   let dynList = sequenceA lsDyn
--   mapM_ display dynList

main :: IO ()
main = mainWidget $ itemElDyn (constDyn Apple)

Checking the type in ghci seem like a valid combination


```λ> :t mapM_ display . sequence $ constDyn [Apple,Banana]                       
mapM_ display . sequence $ constDyn [Apple,Banana]
  :: (PostBuild t m, DomBuilder t m, Traversable (Dynamic t)) => m ()
*Main
λ> :t mapM_ display . sequenceA $ constDyn [Apple,Banana]
mapM_ display . sequenceA $ constDyn [Apple,Banana]
  :: (PostBuild t m, DomBuilder t m, Traversable (Dynamic t)) => m ()

but got error when try to run

λ> mapM_ display . sequence $ constDyn [Apple,Banana]

<interactive>:72:1: error:
    • No instance for (Traversable (Dynamic t0))
        arising from a use of ‘it’
    • In a stmt of an interactive GHCi command: print it
1

There are 1 answers

0
mb21 On

What about mconcat? Something like (untested):

main = mainWidget $ display itemsDyn
  where
    itemsDyn = mconcat $ fmap (itemElDyn . constDyn) [Apple,Banana]

(Have you seen https://github.com/reflex-frp/reflex/blob/develop/Quickref.md?)