Created
August 15, 2013 17:17
-
-
Save joachifm/6242662 to your computer and use it in GitHub Desktop.
General length
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-| | |
General 'length' for any 'Foldable' structure. | |
> foldLength [undefined, undefined] = 2 | |
-} | |
module FoldLength (foldLength1, foldLength2) where | |
import Data.Functor | |
import qualified Data.Foldable as F | |
------------------------------------------------------------------------ | |
foldLength1 :: (Num i, F.Foldable t) => t a -> i | |
foldLength1 = F.foldl' (\z _ -> 1 + z) 0 | |
foldLength2 :: (Functor t, F.Foldable t, Num i) => t a -> i | |
foldLength2 = F.sum . (1 <$) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment