[Haskell-cafe] where to put general-purpose utility functions

Joey Hess joey at kitenet.net
Mon Jan 23 22:01:58 CET 2012

David Fox wrote:
> I try to create a workflow for this sort of thing.  I create a package
> with a name like set-extra, with one module Data.Set.Extra and an
> alternative Data.Set module that exports both the old Data.Set and the
> symbols in Data.Set.Extra.  Then I email the maintainers of the
> Containers package with a suggestion.  After a while I upload
> set-extra to hackage if I need to use set-extra in another hackage
> package.

Thanks, that's the most helpful hint.
It matches MissingH's use of .Utils modules too.

Jean-Marie Gaillourdet wrote:
> Personally, I've always been avoiding those grab-bags of functionality like
> MissingH and other libraries. Not because I think they don't provide anything
> useful. But, because their level of maintenance is not clear to me. A rather
> large library of utility functions tends to need many dependencies on other
> hackage packages. That makes the question of maintenance even more important.

It's not clear to me either. I used MissingH starting out because I
personally know and trust John and/or he cowrote RWH.
Don't know that I would have otherwise.

(And I only use Data.String.Utils, System.Cmd.Utils, and Data.Bits.Utils
from it.)

> As others have pointed out some of your functions may already exist in some
> widely used package. And other might be easy to be replaced by some idiom. Don't
> underestimate the depth of Haskell and it's well thought libraries. I am
> regularly amazed by finding some new way to combine seemingly trivial functions
> to do some non-trivial task. Every time that happens I can remove some of my
> utility functions.

Well, this is certianly true, on the other hand then you end up with a
pattern of repeatedly combining some trivial functions in a certian way,
and it then makes sense to formalize that. It's better to have `fromMaybe`
than to repeatedly use `id` with `maybe`.

Tristan Ravitch wrote:
> >   whenM :: Monad m => m Bool -> m () -> m ()   -- also >>?
> >   unlessM :: Monad m => m Bool -> m () -> m () -- also >>!
> >   firstM :: Monad m => (a -> m Bool) -> [a] -> m (Maybe a)
> IfElse (http://hackage.haskell.org/packages/archive/IfElse/0.85/doc/html/Control-Monad-IfElse.html)
> has a few of these.

whenM is in a dozen packages, the others fewer but scattered here and
there. I also found >>? and >>! somewhere on hackage once but not sure

> >   Various path manipulation functions such as:
> >   absPath :: FilePath -> IO FilePath
> Is this different from canonicalizePath in directory?

Yes; it doesn't require the path to exist.

> >   Other stuff:
> >
> >   separate :: (a -> Bool) -> [a] -> ([a], [a])
> Is this partition from Data.List?

No; it's like break but does not include the separating character in the
snd list.

> >   format :: Format -> Variables -> String
> This looks like it might be similar to HStringTemplate

This particular format allows for things like "${foo} ${bar;10} ${baz;-10}\n"
so it's sort of printf like, but not entirely.

> >   withTempFile :: Template -> (FilePath -> Handle -> IO a) -> IO a
> temporary (http://hackage.haskell.org/packages/archive/temporary/
> has a few variants of this one

Indeed, however all its functions can fail if getTemporaryDirectory
fails; this one puts the temp file in "." in that case.

Simon Hengel wrote:
> >   headMaybe :: [a] -> Maybe a
> Is this the same as Data.Maybe.maybeToList?

Rather listToMaybe.. it is the same as that in fact.
Though I also have a lastMaybe that does not have an equivilant in Data.Maybe.

> >   readMaybe :: Read a => String -> Maybe a
> This has been added to base recently [1].

Great! Although there are multiple ways to choose to implement this. 
I found it useful to make it succeed even if not all the string was
consumed, or when there are multiple valid results. I've renamed
mine readish.

eee shy jo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 828 bytes
Desc: Digital signature
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20120123/d66b8cfb/attachment.pgp>

More information about the Haskell-Cafe mailing list