Discussion: traversals for Data.Set

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Discussion: traversals for Data.Set

David Feuer
Julian Ospald has opened a pull request[*] for containers to add a traversal operation to Data.Set. Julian specifically requested

    forM :: (Ord b, Monad m) => Set a -> (a -> m b) -> m (Set b)

Personally, I think it would be better to offer the unflipped versions:

    -- The most general version
    traverse :: (Ord b, Applicative f) => (a -> f b) -> Set a -> f (Set b)
    traverse f = fmap fromList . traverse f . toList

    -- A more efficient, strictly accumulating version for "strict"
    -- monads like IO, strict State, etc.
    mapM :: (Ord b, Monad m) => (a -> m b) -> Set a -> m (Set b)
    mapM f s0 = foldr go return s0 empty
      where
        go x r s = f x >>= \y -> r $! insert y s

The latter can also be written, perhaps less efficiently, as

    mapM :: (Ord b, Monad m) => (a -> m b) -> Set a -> m (Set b)
    mapM f = foldM go empty
      where
        go s x = f x >>= \y -> pure $! insert y s

As usual, the main question is what names are appropriate. Typically, containers assumes that users import its modules qualified, and therefore we should just use `traverse` and `mapM`. But clashing with well-known and widely-used names often causes breakage anyway, so I figured I should bring the matter to the libraries list.

Thanks,
David Feuer


_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries
Reply | Threaded
Open this post in threaded view
|

Re: Discussion: traversals for Data.Set

Henning Thielemann

On Mon, 21 Jan 2019, David Feuer wrote:

> But clashing with well-known and widely-used names often causes breakage
> anyway, so I figured I should bring the matter to the libraries list.

Programmers from the pre-module era call it "clashing", modern programmers
call it "consistent naming". :-) I am happy with any of mapM, traverse,
forM.
_______________________________________________
Libraries mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/libraries