A Tour of Go in Haskell

GitHub Language
Select

Although Haskell does not have select, the stm library offers a combinator with a similar effect.

select in Go chooses one of a number of available communication operations. Similarly the stm package provides a function orElse :: STM a -> STM a -> STM a which chooses a available one of two stm operations. fold functions in Haskell can then be used to apply this to a whole list of operations. Note that when multiple operations are available simultaneously, this implimentation chooses an available operation in order from the beginning. (Go select chooses randomly in that case.) And, all stm operations of the list must be of the same type.

Actually STM is a instance of MonadPlus. So we can implement select as shown below.

// another implimentation of select
import Control.Monad (msum)
select = atomically . msum

Because the MonadPlus instance is implemented using orElse, we can write select in this way. But it is not documented explicitly that the MonadPlus instance is implemented by orElse. Therefore you should avoid implementing select using MonadPlus.

< 5 / 11 >