
It works! And I don't understand it! Particularly this line:
inputs <- map (\e -> [hfill $ widget e]) <$> varGet myVar
I'm considering the type signatures:
map :: (a -> b) -> [a] -> [b]
(<$>) :: Functor f => (a -> b) -> f a -> f b
varGet :: Var a -> IO a
map and <$> (fmap) are nearly synonymous; map seems like a special case of
fmap. I gather the fmap must be to map inside the IO type that varGet
returns, and the map must be mapping across some list. But what about their
function arguments? Is map using the lambda expression, or is fmap? What
function argument is the other one using? Are they both somehow sharing the
lambda expression?
On Tue, Mar 31, 2015 at 12:48 AM, Henk-Jan van Tuyl
On Tue, 31 Mar 2015 04:14:35 +0200, Jeffrey Brown
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@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 --