Repository : ssh://darcs.haskell.org//srv/darcs/packages/containers On branch : master
http://hackage.haskell.org/trac/ghc/changeset/2738ff2712919346b3167dab6fbe4a286a7a36af >--------------------------------------------------------------- commit 2738ff2712919346b3167dab6fbe4a286a7a36af Author: Milan Straka <[email protected]> Date: Fri Nov 25 14:07:24 2011 +0100 Add forgotten findWithDefault to IntMap.Strict. >--------------------------------------------------------------- Data/IntMap/Strict.hs | 19 ++++++++++++++++++- 1 files changed, 18 insertions(+), 1 deletions(-) diff --git a/Data/IntMap/Strict.hs b/Data/IntMap/Strict.hs index 80b4432..cd28d75 100644 --- a/Data/IntMap/Strict.hs +++ b/Data/IntMap/Strict.hs @@ -193,7 +193,8 @@ module Data.IntMap.Strict ( import Prelude hiding (lookup,map,filter,foldr,foldl,null) import Data.IntMap.Base hiding - ( singleton + ( findWithDefault + , singleton , insert , insertWith , insertWithKey @@ -253,6 +254,22 @@ import Data.StrictPair -- > map (\ v -> undefined) m == undefined -- m is not empty -- > mapKeys (\ k -> undefined) m == undefined -- m is not empty +{-------------------------------------------------------------------- + Query +--------------------------------------------------------------------} + +-- | /O(min(n,W))/. The expression @('findWithDefault' def k map)@ +-- returns the value at key @k@ or returns @def@ when the key is not an +-- element of the map. +-- +-- > findWithDefault 'x' 1 (fromList [(5,'a'), (3,'b')]) == 'x' +-- > findWithDefault 'x' 5 (fromList [(5,'a'), (3,'b')]) == 'a' + +findWithDefault :: a -> Key -> IntMap a -> a +findWithDefault def k m + = def `seq` case lookup k m of + Nothing -> def + Just x -> x {-------------------------------------------------------------------- Construction _______________________________________________ Cvs-libraries mailing list [email protected] http://www.haskell.org/mailman/listinfo/cvs-libraries
