Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: How to "cast" FromJSON instance value (Jan von Löwenstein) 2. Re: How to "cast" FromJSON instance value (Baa) 3. Grappling with State Monad get (Olumide) 4. Re: Grappling with State Monad get (Theodore Lief Gannon) ---------------------------------------------------------------------- Message: 1 Date: Tue, 01 Aug 2017 12:38:46 +0000 From: Jan von Löwenstein <jan.loewenst...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] How to "cast" FromJSON instance value Message-ID: <CAJtP4w6HC+b=z7zw0qewbm+v2cwrsmo8ef+6+b20ejsdvzu...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hi, getValue :: (FromJSON a) => Text -> Map Text a -> Maybe a getValue key map = (fromResult.fromJSON) $ lookup key map fromResult (Success a) = Just a fromResult (Error _) = Nothing I am not at a computer so I wouldn't be surprised if this doesn't compile. But you should see the picture. Best Jan Baa <aqua...@gmail.com> schrieb am Di., 1. Aug. 2017, 13:48: > Ohh, pardon! Yes, signature was > > class GetValue a where > getValue :: Text -> Map Text Value -> Maybe a > > with `Maybe` sure. But I didn't understand you: what do you mean "to > use aeson directly"? Problem is to save `FromJSON a` instances in map > but get from the map `Text`, `Bool` and etc values. Sure, it possible > to parameterize all related functions/types with `FromJSON a => .. a` > but in this case I hit 2 problems: > > 1. rigid type error when I have `FromJSON a =>` in some signature and > try to use this `a` as `Text` value, for example > 2. big refactoring (many signatures, types) > > So, I decided to save in the map `Value`s instead of `FromJSON a`s. > De-facto, this map is heterogenous and keeps values of different types. > And question for me was: to hide this fact under `FromJSON` or under > `Value`. I'm not sure what is the best solution and I don't know how to > solve 1. > > > > You'd still have to handle the case that the Value is not what you > > expected. Otherwise you'll have a partial function (i.e. error in > > case of certain inputs). > > > > So maybe you'd best use aeson directly (which spares you the GetValue > > instances as well). > > > > getValue :: (FromJSON a) => Text -> Map Text a -> Maybe a > > > > Best > > Jan > > P.S. There is no such thing as a stupid question. > > > > Baa <aqua...@gmail.com> schrieb am Di., 1. Aug. 2017, 13:02: > > > > > Hello, Jan! I "remove" the question :) > > > Problem was: reading of HTML form values, which can be any type and > > > saving them in a map. I done it w/ `Map Text Value` (instead of > > > previous `Map Text Text`). To get values from the map, I > > > implemented class and several instances which returns values of > > > different types, something like: > > > > > > class GetValue a where > > > getValue :: Text -> Map Text Value -> a > > > > > > (promoting of `a` anywhere in the existing code needs big > > > refactoring, so map's value is `::Value` but not `::a`). Cons is a > > > need to implement N instances of `GetValue`: one per getting type. > > > > > > Anywhere, Jan, thanks for quick answer, I'm sorry for this stupid > > > question. > > > > > > > > > > Hi, > > > > > > > > You don't know anything about your a's but that they have FromJSON > > > > instances. That in turn means you can get values from aeson > > > > parsing functions. Think parseJSON :: Value -> Parser a > > > > Could you be more specific about the problem you try to solve? > > > > > > > > Best > > > > Jan > > > > > > > > Baa <aqua...@gmail.com> schrieb am Di., 1. Aug. 2017, 10:32: > > > > > > > > > Hello, List! > > > > > > > > > > I have, for example, function with signature like > > > > > > > > > > fn :: FromJSON a => Map Text a -> BlahBlah > > > > > > > > > > and I want to treat this `a` as String (or `Text`), to read it > > > > > with `readMaybe`. Am I right that it's impossible? I get error > > > > > about "...a is rigid type and could not be matched with > > > > > Text...". But `Text` is `FromJSON` instance. Is it possible to > > > > > do it (cast, convert, unwrap, etc)? > > > > > > > > > > The problem's source is that early I worked with `Text` values > > > > > only (getting from HTML form) but now they become any type: > > > > > int's, bool's, etc). > > > > > > > > > > === > > > > > Best regards, > > > > > Paul > > > > > _______________________________________________ > > > > > Beginners mailing list > > > > > Beginners@haskell.org > > > > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > > > > > > > > _______________________________________________ > > > Beginners mailing list > > > Beginners@haskell.org > > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170801/4ef51c1b/attachment-0001.html> ------------------------------ Message: 2 Date: Tue, 1 Aug 2017 16:37:37 +0300 From: Baa <aqua...@gmail.com> To: beginners@haskell.org Subject: Re: [Haskell-beginners] How to "cast" FromJSON instance value Message-ID: <20170801163737.73d22d96@Pavel> Content-Type: text/plain; charset=US-ASCII Jan, this is the exactly what I'm looked for! Thank you again! :) getValue :: FromJSON a => T.Text -> M.Map T.Text a -> Maybe a getValue k m = do j <- fromJSON <$> M.lookup k m case j of Success a -> return a _ -> Nothing > Hi, > > getValue :: (FromJSON a) => Text -> Map Text a -> Maybe a > getValue key map = (fromResult.fromJSON) $ lookup key map > > fromResult (Success a) = Just a > fromResult (Error _) = Nothing > > I am not at a computer so I wouldn't be surprised if this doesn't > compile. But you should see the picture. > > Best > Jan > > Baa <aqua...@gmail.com> schrieb am Di., 1. Aug. 2017, 13:48: > > > Ohh, pardon! Yes, signature was > > > > class GetValue a where > > getValue :: Text -> Map Text Value -> Maybe a > > > > with `Maybe` sure. But I didn't understand you: what do you mean "to > > use aeson directly"? Problem is to save `FromJSON a` instances in > > map but get from the map `Text`, `Bool` and etc values. Sure, it > > possible to parameterize all related functions/types with `FromJSON > > a => .. a` but in this case I hit 2 problems: > > > > 1. rigid type error when I have `FromJSON a =>` in some signature > > and try to use this `a` as `Text` value, for example > > 2. big refactoring (many signatures, types) > > > > So, I decided to save in the map `Value`s instead of `FromJSON a`s. > > De-facto, this map is heterogenous and keeps values of different > > types. And question for me was: to hide this fact under `FromJSON` > > or under `Value`. I'm not sure what is the best solution and I > > don't know how to solve 1. > > > > > > > You'd still have to handle the case that the Value is not what you > > > expected. Otherwise you'll have a partial function (i.e. error in > > > case of certain inputs). > > > > > > So maybe you'd best use aeson directly (which spares you the > > > GetValue instances as well). > > > > > > getValue :: (FromJSON a) => Text -> Map Text a -> Maybe a > > > > > > Best > > > Jan > > > P.S. There is no such thing as a stupid question. > > > > > > Baa <aqua...@gmail.com> schrieb am Di., 1. Aug. 2017, 13:02: > > > > > > > Hello, Jan! I "remove" the question :) > > > > Problem was: reading of HTML form values, which can be any type > > > > and saving them in a map. I done it w/ `Map Text Value` > > > > (instead of previous `Map Text Text`). To get values from the > > > > map, I implemented class and several instances which returns > > > > values of different types, something like: > > > > > > > > class GetValue a where > > > > getValue :: Text -> Map Text Value -> a > > > > > > > > (promoting of `a` anywhere in the existing code needs big > > > > refactoring, so map's value is `::Value` but not `::a`). Cons > > > > is a need to implement N instances of `GetValue`: one per > > > > getting type. > > > > > > > > Anywhere, Jan, thanks for quick answer, I'm sorry for this > > > > stupid question. > > > > > > > > > > > > > Hi, > > > > > > > > > > You don't know anything about your a's but that they have > > > > > FromJSON instances. That in turn means you can get values > > > > > from aeson parsing functions. Think parseJSON :: Value -> > > > > > Parser a Could you be more specific about the problem you try > > > > > to solve? > > > > > > > > > > Best > > > > > Jan > > > > > > > > > > Baa <aqua...@gmail.com> schrieb am Di., 1. Aug. 2017, 10:32: > > > > > > > > > > > Hello, List! > > > > > > > > > > > > I have, for example, function with signature like > > > > > > > > > > > > fn :: FromJSON a => Map Text a -> BlahBlah > > > > > > > > > > > > and I want to treat this `a` as String (or `Text`), to read > > > > > > it with `readMaybe`. Am I right that it's impossible? I get > > > > > > error about "...a is rigid type and could not be matched > > > > > > with Text...". But `Text` is `FromJSON` instance. Is it > > > > > > possible to do it (cast, convert, unwrap, etc)? > > > > > > > > > > > > The problem's source is that early I worked with `Text` > > > > > > values only (getting from HTML form) but now they become > > > > > > any type: int's, bool's, etc). > > > > > > > > > > > > === > > > > > > Best regards, > > > > > > Paul > > > > > > _______________________________________________ > > > > > > Beginners mailing list > > > > > > Beginners@haskell.org > > > > > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > > > > > > > > > > > _______________________________________________ > > > > Beginners mailing list > > > > Beginners@haskell.org > > > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > > > > > > > _______________________________________________ > > Beginners mailing list > > Beginners@haskell.org > > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners > > ------------------------------ Message: 3 Date: Wed, 2 Aug 2017 00:16:04 +0100 From: Olumide <50...@web.de> To: beginners@haskell.org Subject: [Haskell-beginners] Grappling with State Monad get Message-ID: <fb761fdf-324b-e615-c08d-661b00e01...@web.de> Content-Type: text/plain; charset=utf-8; format=flowed Ahoy Haskellers, In the section "Getting and Setting State" (http://learnyouahaskell.com/for-a-few-monads-more#state) in LYH get is defined as get = state $ \s -> (s, s) How does does get determine the type s, is considering that it has no argument as per the definition given above? Or is the definition written in some sort of point-free notation where an argument has been dropped? I find the line stackNow <- get in the the function(?) stackyStack confusing for the same reason. I guess my difficulty is that state $ \s ->(s , s) has a generic type (s) whereas the stackyStack has a concrete type. Is the type of s determined from the type of the stateful computation/do notation? Regards, - Olumide ------------------------------ Message: 4 Date: Tue, 1 Aug 2017 16:35:14 -0700 From: Theodore Lief Gannon <tan...@gmail.com> To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Subject: Re: [Haskell-beginners] Grappling with State Monad get Message-ID: <cajopsub7dhjztz_reqaaytpuwf84_hd3gasx5ewm+yldnwx...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" 's' here is not a type variable, it's an actual variable. \s -> (s, s) defines a lambda function which takes any value, and returns a tuple with that value in both positions. So yes, get is point-free, but the missing argument is right there in-line. And yes, its type is simply implied from context. On Aug 1, 2017 4:17 PM, "Olumide" <50...@web.de> wrote: Ahoy Haskellers, In the section "Getting and Setting State" (http://learnyouahaskell.com/f or-a-few-monads-more#state) in LYH get is defined as get = state $ \s -> (s, s) How does does get determine the type s, is considering that it has no argument as per the definition given above? Or is the definition written in some sort of point-free notation where an argument has been dropped? I find the line stackNow <- get in the the function(?) stackyStack confusing for the same reason. I guess my difficulty is that state $ \s ->(s , s) has a generic type (s) whereas the stackyStack has a concrete type. Is the type of s determined from the type of the stateful computation/do notation? Regards, - Olumide _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.haskell.org/pipermail/beginners/attachments/20170801/6484bcbb/attachment.html> ------------------------------ Subject: Digest Footer _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners ------------------------------ End of Beginners Digest, Vol 110, Issue 3 *****************************************