[Haskell-cafe] How to write a Monad instance for this type

Will Yager will.yager at gmail.com
Fri Sep 6 12:35:00 UTC 2019


Hi Frederic,

Lenses (http://hackage.haskell.org/package/lens) are a powerful and well-supported way of peering into data structures. They are a bit of work to learn, but well worth it. 

Will

On Sep 6, 2019, at 4:58 PM, PICCA Frederic-Emmanuel <frederic-emmanuel.picca at synchrotron-soleil.fr> wrote:

>> You don't need a monad instance for this. First of all, you don't even need do syntax to make something "pretty" similar to this.
> 
>>        hdf5 $
>>            group "name" $
>>                [ dataset "name1" array1
>>                , dataset "name2" array2
>>                , group "other-name" $ [
>>> 
> This is for now the solution I decide to keep., nevertheless thanks for the monad ;).
> 
> Now I would like you opinion about a type which allows me to select a node in this tree.
> 
> I need to extract a bunch of values from these hdf5 files.
> So I discribe a location in the tree, like this with another type quite similar to the first but with only
> one child per group. This way there is only one dataset extracted.
> 
> (maybe later, I will discuss about extracting  multiple dataset ;).
> 
> data Hdf5Path sh e
>  = H5RootPath (Hdf5Path sh e)
>  | H5GroupPath ByteString (Hdf5Path sh e)
>  | H5DatasetPath ByteString
> 
> hdf5p $ group "name" $ group "otherName" $ dataset "myDataset"
> 
> Then I need to write something like this.
> 
> withDataset :: File -> Hdf5Path sh e -> (Dataset -> IO r) -> IO r
> 
> BUT, I would like to express  this path like this
> 
> hdf5p :: group <the first encounted> :: group "otherName" $ dataset "mydataset"
> 
> So should I define a type and modify the group constructor like this
> 
> H5GroupPath BytString (Hdf5Path sh e)
> 
> GroupDesc = ByName ByteString
>                    | ByPosition Int
>                    | etc...
> 
> H5GroupPath GropDesc  (Hdf5Path sh e)
> 
> the only problem I see with this is that I need to encode all the strategies in this type.
> 
> Is there a better way to do this sort of things.
> 
> thanks
> 
> Frederic
> _______________________________________________
> Haskell-Cafe mailing list
> To (un)subscribe, modify options or view archives go to:
> http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
> Only members subscribed via the mailman list are allowed to post.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20190906/c65c0ec2/attachment.html>


More information about the Haskell-Cafe mailing list