[commit: containers] master: Add forgotten findWithDefault to IntMap.Strict. (2738ff2)
Ian Lynagh
igloo at earth.li
Thu Feb 23 01:15:11 CET 2012
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 <fox at ucw.cz>
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
More information about the Cvs-libraries
mailing list