How am I meant to use sort?

178 Views Asked by At

In Haskell Turtle, sort has the type (Functor io, MonadIO io, Ord a) => Shell a -> io [a]. It takes a stream in the form of a Shell monad, sorts it into a list, but then wraps it in a MonadIO???

This seems counter-intuitive to me. My beginner eyes say that a MonadIO should only be used when interacting with the outside world, which sort doesn't need to do.

I would have expected sort to be (Ord a) => Shell a -> Shell a so it fits into a stream nicely.

Is this an oversight by the package authors, or am I not understanding how sort should be used?

1

There are 1 best solutions below

2
On

MonadIO io => io [a] is better than Shell a, because Shell is a MonadIO instance and select lifts lists to Shells. So, if you want, you can write your own more specific sort that has the type you like, implemented with the sort provided by turtle:

sortStream :: Ord a => Shell a -> Shell a
sortStream = sort >=> select