[Haskell-beginners] Get rid of Maybes in complex types

Baa aquagnu at gmail.com
Thu Jul 6 11:22:59 UTC 2017


Unfortunately it's a lot of code and it's not open source :(
As I understood you, function `retrieveData` should make a final
decision: to be or not to be?

It's idea too, but possible will lead to more refactoring.


Thu, 06 Jul 2017 12:02:12 +0200
Frerich Raabe <raabe at froglogic.com> wrote:

> On 2017-07-06 10:12, Baa wrote:
> > Consider, I retrieve from external source some data. Internally it's
> > represented as some complex type with `Maybe` fields, even more,
> > some of fields are record types and have `Maybe` fields too. They
> > are Maybe's because some information in this data can be missing
> > (user error or it not very valuable and can be skipped):
> > 
> >   data A = A {
> >     a1 :: Maybe B
> >     ... }
> >   data B = B {
> >     b1 :: Maybe C
> >     ... }
> > 
> > I retrieve it from network, files, i.e. external world, then I
> > validate it, report errors of some missing fields, fix another one
> > (which can be fixed, for example, replace Nothing with `Just
> > default_value` or even I can fix `Just wrong` to `Just right`, etc,
> > etc). After all of this, I know that I have "clean" data, so all my
> > complex types now have `Just right_value` fields. But I need to
> > process them as optional, with possible Nothing case! To avoid it I
> > must create copies of `A`, `B`, etc, where `a1`, `b1` will be `B`,
> > `C`, not `Maybe B`, `Maybe C`. Sure, it's not a case.  
> 
> I understand that your data structures as sketched above are only
> needed for a short time, when reading/validating/fixing the input
> data - the rest of the program would either report an error (for
> unrecoverable problems) or process the validated data?
> 
> In that case, maybe you can avoid using Maybe in your data types for
> things which definitely must be present completely but rather
> returning a 'Maybe' value from your function which retrieves the
> data, i.e.:
> 
>    retrieveData :: ByteString -> Maybe A
> 
> Within 'retrieveData' you could then use different weakly-typed data 
> structures which maybe only represent subsets of the data as you 
> parse/validate/fix it. Not sure if/how that would work out, but maybe
> you could share some more concrete code (e.g. the actual logic for
> retrieving the data and the data types)?
> 



More information about the Beginners mailing list