Skip to content

Instantly share code, notes, and snippets.

@joachifm
Created August 15, 2013 17:17
Show Gist options
  • Save joachifm/6242662 to your computer and use it in GitHub Desktop.
Save joachifm/6242662 to your computer and use it in GitHub Desktop.
General length
{-|
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