Mutable collection of gui objects (wxHaskell)

Hi, On Github, Jeremy O'Donoghue provides wx example code. From his collection 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 ] and got this error: Layout.hs:23:11: Couldn't match type `IO' with `[]' Expected type: [Layout] Actual type: IO Layout In the expression: fmap (\ e -> hfill $ widget e) $ varGet myVar In the third argument of `grid', namely `[fmap (\ e -> hfill $ widget e) $ varGet myVar]' In the second argument of `boxed', namely `(grid 5 5 [fmap (\ e -> hfill $ widget e) $ varGet myVar])' So then I tried something I thought would be equivalent: [[ (hfill $ widget e) | e <- (varGet myVar)]] and got a different error: Layout.hs:23:39: Couldn't match expected type `[w0]' with actual type `IO [TextCtrl ()]' In the return type of a call of `varGet' In the expression: (varGet myVar) In a stmt of a list comprehension: e <- (varGet myVar) I kind of understand the problem is that when I varGet myVar, I end up with type IO Layout, rather than type Layout, but I don't know what to do about it. Thanks, Jeff [1] https://github.com/jodonoghue/wxHaskell/blob/master/samples/wx/Layout.hs

On Tue, 31 Mar 2015 04:14:35 +0200, Jeffrey Brown
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 --

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 --

On Tue, 31 Mar 2015 22:11:47 +0200, Jeffrey Brown
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?
The expression f <$> x is equal to fmap f x . If f :: a -> b x :: IO a then fmap f :: IO a -> IO b . You could say, fmap lifts f to the IO monad (you could also use liftM for this). In your case, f is map (\e -> [hfill $ widget e]) and x is varGet myVar You stored a list of textEntrys in myVar, the lambda expression is mapped over this list. Another way to look at it: you can replace the line inputs <- map (\e -> [hfill $ widget e]) <$> varGet myVar with xs <- varGet myVar let inputs = map (\e -> [hfill $ widget e]) xs 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 --

No wonder Haskell won't catch on... I saw code that looked just like that
on the wall of a pyramid on an episode Ancient Aliens recently!
April fool by the way!
On 31 March 2015 at 23:51, Henk-Jan van Tuyl
On Tue, 31 Mar 2015 22:11:47 +0200, Jeffrey Brown
wrote: 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?
The expression f <$> x is equal to fmap f x . If f :: a -> b x :: IO a then fmap f :: IO a -> IO b . You could say, fmap lifts f to the IO monad (you could also use liftM for this).
In your case, f is map (\e -> [hfill $ widget e]) and x is varGet myVar You stored a list of textEntrys in myVar, the lambda expression is mapped over this list.
Another way to look at it: you can replace the line inputs <- map (\e -> [hfill $ widget e]) <$> varGet myVar with xs <- varGet myVar let inputs = map (\e -> [hfill $ widget e]) xs
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 -- _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Tue, 31 Mar 2015 04:14:35 +0200, Jeffrey Brown
On Github, Jeremy O'Donoghue provides wx example code. From his collection
Note, that this repository is not actively maintained anymore, the official repository is at https://github.com/wxHaskell/wxHaskell 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 --
participants (3)
-
emacstheviking
-
Henk-Jan van Tuyl
-
Jeffrey Brown