[Haskell-beginners] Mutable collection of gui objects (wxHaskell)

Henk-Jan van Tuyl hjgtuyl at chello.nl
Tue Mar 31 07:48:54 UTC 2015


On Tue, 31 Mar 2015 04:14:35 +0200, Jeffrey Brown  
<jeffbrown.the at gmail.com> wrote:

:
> I am trying to modify Layout.hs [1] to permit a variable number, rather
> than a fixed number, of text entry boxes. I shrank the program, mostly by
> removing features, until it was this:
>
>   main = start $ do
>     f      <- frame  [text := "Layout test"]
>     p      <- panel  f []
>     xinput <- textEntry p [text := "100"]
>     yinput <- textEntry p [text := "100"]
>     myVar <- varCreate [xinput,yinput]
>     set f [ layout := container p $ margin 10 $
>       column 5 [boxed "coordinates" (grid 5 5
>           [[hfill $ widget xinput], [hfill $ widget yinput]] -- replacing
>         ) ] ]
>     return ()
>
> I want to replace the line marked "replacing". Rather than hard-coding  
> the
> number of text entry boxes (2), I want it to deal with a mutable  
> collection
> of them, in myVar.
>
> I tried this:
>   [ fmap (\e -> hfill $ widget e) $ varGet myVar ]
:

The result of varGet is of type "IO something", you must convert that to  
"something", by using "<-". You can do this by adding the line:
   inputs <- map (\e -> [hfill $ widget e]) <$> varGet myVar
before the set command (note the square brackets). The <$> operator is  
 from module Data.Functor and is defined as
   (<$>) = fmap

The main function becomes this:
   main = start $ do
       f      <- frame  [text := "Layout test"]
       p      <- panel  f []
       xinput <- textEntry p [text := "100"]
       yinput <- textEntry p [text := "100"]
       myVar  <- varCreate [xinput, yinput]
       inputs <- map (\e -> [hfill $ widget e]) <$> varGet myVar
       set f [ layout := container p $ margin 10 $
               column 5 [boxed "coordinates" (grid 5 5 inputs)]
             ]
       return ()


Regards,
Henk-Jan van Tuyl


-- 
Folding at home
What if you could share your unused computer power to help find a cure? In  
just 5 minutes you can join the world's biggest networked computer and get  
us closer sooner. Watch the video.
http://folding.stanford.edu/


http://Van.Tuyl.eu/
http://members.chello.nl/hjgtuyl/tourdemonad.html
Haskell programming
--


More information about the Beginners mailing list