Arrows
Function compositions with multiple channels
Section titled “Function compositions with multiple channels”Arrow is, vaguely speaking, the class of morphisms that compose like functions, with both serial composition and “parallel composition”. While it is most interesting as a generalisation of functions, the Arrow (->) instance itself is already quite useful. For instance, the following function:
spaceAround :: Double -> [Double] -> DoublespaceAround x ys = minimum greater - maximum smaller where (greater, smaller) = partition (>x) yscan also be written with arrow combinators:
spaceAround x = partition (>x) >>> minimum *** maximum >>> uncurry (-)This kind of composition can best be visualised with a diagram:
──── minimum ──── ╱ * ╲──── partition (>x) >>> * >>> uncurry (-) ─── ╲ * ╱ ──── maximum ────Here,
partition (>x) :: [Double] -> ([Double], [Double])splits up the flow in two [Double] channels, whereas
uncurry (-) :: (Double,Double) -> Doublemerges two Double channels.
(***) :: (b->c) -> (β->γ) -> (b,β)->(c,γ)†At least in the Hask category (i.e. in the Arrow (->) instance), f***g does not actually compute f and g in parallel as in, on different threads. This would theoretically be possible, though.