[Haskell-cafe] Num nstances for Data.Monoid.{First,Last}

Oliver Charles ollie at ocharles.org.uk
Thu Mar 23 13:52:43 UTC 2017


We already have Num instances on things like Sum and Product, could we have
them on First and Last? I mostly want fromInteger on these types. To give
you a real world example:

    keySpeed :: L.Fold KeyboardEventData Double
    keySpeed =
      L.foldMap
        (\KeyboardEventData {keyboardEventKeyMotion} ->
           case keyboardEventKeyMotion of
             Pressed -> Last (Just 1)
             Released -> Last (Just 0))
        (fromMaybe 0 . getLast)

keySpeed folds (under Control.Foldl) events from a UI system into the
number 1 if the key has been pressed and 0 if the key has been released.
It's OK in the above form, but if Last had a Num instance, I could just have

             Pressed -> 1
             Released -> 0

Which I find preferable.

Any thoughts?
- Ollie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20170323/30e9e426/attachment.html>


More information about the Haskell-Cafe mailing list