Map

Prelude> import Data.Map as Map
Prelude Map> :set -XOverloadedLists
Prelude Map>

OverloadedLists

GHC 提供了语言扩展 OverloadedLists。

  • 不使用这个语言扩展,所有的列表字面量都属于 [] 类型。
  • 如果使用这个语言扩展,那么所有的列表字面量就都属于 IsList l => l 类型。
  • Map, Set, Vector, Text, Array 都是 IsList 类的实例。

Operators

Prelude Map> [(5,'a'), (3,'b')] ! 5
'a'
Prelude Map> [(5, 'a'), (3, 'b')] !? 1
Nothing
Prelude Map> [(5, 'a'), (3, 'b')] !? 5
Just 'a'

Query

Prelude Map> Map.null (singleton 1 'a')
False
Prelude Map> size [(1,'a'), (2,'c'), (3,'b')]
3
Prelude Map> member 5 [(5,'a'), (3,'b')]
True
Prelude Map> Map.lookup "John" [("John","Sales"), ("Bob","IT")]
Just "Sales"
Prelude Map> findWithDefault 'x' 1 [(5,'a'), (3,'b')]
'x'
Prelude Map> findWithDefault 'x' 5 [(5,'a'), (3,'b')]
'a'

Construction

Prelude Map> singleton 1 'a'
fromList [(1,'a')]
Prelude Map> empty
fromList []

Insertion

Prelude Map> insert 5 'x' [(5,'a'), (3,'b')]
fromList [(3,'b'),(5,'x')]
Prelude Map> insert 7 'x' [(5,'a'), (3,'b')]
fromList [(3,'b'),(5,'a'),(7,'x')]
Prelude Map> insert 5 'x' empty
fromList [(5,'x')]

Delete/Update

Prelude Map> delete 5 [(5,"a"), (3,"b")]
fromList [(3,"b")]
Prelude Map> adjust ("new " ++) 5 [(5,"a"), (3,"b")]
fromList [(3,"b"),(5,"new a")]
Prelude Map> let f x = if x == "a" then Just "new a" else Nothing
Prelude Map> update f 5 [(5,"a"), (3,"b")]
fromList [(3,"b"),(5,"new a")]
Prelude Map> let f _ = Nothing
Prelude Map> alter f 5 [(5,"a"), (3,"b")]
fromList [(3,"b")]
Prelude Map> let f _ = Just "c"
Prelude Map> alter f 7 [(5,"a"), (3,"b")]
fromList [(3,"b"),(5,"a"),(7,"c")]

Combine

Prelude Map> union [(5, "a"), (3, "b")] [(5, "A"), (7, "C")]
fromList [(3,"b"),(5,"a"),(7,"C")]
Prelude Map> unions [[(5, "a"), (3, "b")], [(5, "A"), (7, "C")], [(5, "A3"), (3, "B3")]]
fromList [(3,"b"),(5,"a"),(7,"C")]

Difference

Prelude Map> difference [(5, "a"), (3, "b")] [(5, "A"), (7, "C")]
fromList [(3,"b")]

Intersection

Prelude Map> intersection [(5, "a"), (3, "b")] [(5, "A"), (7, "C")]
fromList [(5,"a")]

Traversal

Prelude Map> Map.map (++ "x") [(5,"a"), (3,"b")]
fromList [(3,"bx"),(5,"ax")]
Prelude Map> traverseWithKey (\k v -> if odd k then Just (succ v) else Nothing) [(1, 'a'), (5, 'e')]
Just [(1,'b'),(5,'f')]
Prelude Map> let f a b = (a ++ b, b ++ "X")
Prelude Map> mapAccum f "Everything: " [(5,"a"), (3,"b")]
("Everything: ba",fromList [(3,"bX"),(5,"aX")])
Prelude Map> mapKeys (+ 1) [(5,"a"), (3,"b")]
fromList [(4,"b"),(6,"a")]

Folds

Prelude Map> let f a len = len + (length a)
Prelude Map> Map.foldr f 0 [(5,"a"), (3,"bbb")]
4
Prelude Map> let f len a = len + (length a)
Prelude Map> Map.foldl f 0 [(5,"a"), (3,"bbb")]
4

Conversion

Prelude Map> elems [(5,"a"), (3,"b")]
["b","a"]
Prelude Map> keys [(5,"a"), (3,"b")]
[3,5]
Prelude Map> assocs [(5,"a"), (3,"b")]
[(3,"b"),(5,"a")]

Lists

Prelude Map> toList [(5,"a"), (3,"b")]
[(3,"b"),(5,"a")]
Prelude Map> fromList [(5,"a"), (3,"b"), (5, "c")]
fromList [(3,"b"),(5,"c")]
Prelude Map> fromListWith (++) [(5,"a"), (5,"b"), (3,"b"), (3,"a"), (5,"a")]
fromList [(3,"ab"),(5,"aba")]

Ordered lists

Prelude Map> toAscList [(5,"a"), (3,"b")]
[(3,"b"),(5,"a")]
Prelude Map> toDescList [(5,"a"), (3,"b")]
[(5,"a"),(3,"b")]
Prelude Map> fromAscList [(3,"b"), (5,"a")]
fromList [(3,"b"),(5,"a")]
Prelude Map> fromDescList [(5,"a"), (3,"b")]
fromList [(3,"b"),(5,"a")]

Filter

Prelude Map> Map.filter (> "a") [(5,"a"), (3,"b")]
fromList [(3,"b")]
Prelude Map> partition (> "a") [(5,"a"), (3,"b")]
[(3,"b")],fromList [(5,"a")]
Prelude Map> let f x = if x == "a" then Just "new a" else Nothing
Prelude Map> mapMaybe f [(5,"a"), (3,"b")]
fromList [(5,"new a")]
Prelude Map> split 4 [(5,"a"), (3,"b")]
[(3,"b")],fromList [(5,"a")]

Submap

Prelude Map> isSubmapOfBy (==) [('a',1)] [('a',1),('b',2)]
True
Prelude Map> isSubmapOfBy (<=) [('a',1)] [('a',1),('b',2)]
True
Prelude Map> isSubmapOfBy (<) [('a',1)] [('a',1),('b',2)]
False

Indexed

Prelude Map> lookupIndex 2 [(5,"a"), (3,"b")]
Nothing
Prelude Map> lookupIndex 3 [(5,"a"), (3,"b")]
Just 0
Prelude Map> findIndex 3 [(5,"a"), (3,"b")]
0
Prelude Map> elemAt 0 [(5,"a"), (3,"b")]
(3,"b")
Prelude Map> elemAt 1 [(5,"a"), (3,"b")]
(5,"a")
Prelude Map> updateAt (\ _ _ -> Just "x") 0 [(5,"a"), (3,"b")]
fromList [(3,"x"),(5,"a")]
Prelude Map> deleteAt 0 [(5,"a"), (3,"b")]
fromList [(5,"a")]

Min/Max

Prelude Map> lookupMin [(5,"a"), (3,"b")]
Just (3,"b")
Prelude Map> lookupMax [(5,"a"), (3,"b")]
Just (5,"a")
Prelude Map> findMin [(5,"a"), (3,"b")]
(3,"b")
Prelude Map> findMax [(5,"a"), (3,"b")]
(5,"a")
Prelude Map> deleteMin [(5,"a"), (3,"b"), (7,"c")]
fromList [(5,"a"),(7,"c")]
Prelude Map> updateMax (\ a -> Just ("X" ++ a)) [(5,"a"), (3,"b")]
fromList [(3,"b"),(5,"Xa")]

Debugging

Prelude Map> valid (fromAscList [(3,"b"), (5,"a")])
True
Prelude Map> valid (fromAscList [(5,"a"), (3,"b")])
False

Haskell语言学习笔记(28)Data.Map的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Go语言学习笔记十三: Map集合

    Go语言学习笔记十三: Map集合 Map在每种语言中基本都有,Java中是属于集合类Map,其包括HashMap, TreeMap等.而Python语言直接就属于一种类型,写法上比Java还简单. ...

  3. Haskell语言学习笔记(38)Lens(1)

    Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...

  4. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  5. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  6. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  7. Haskell语言学习笔记(66)Aeson

    Data.Aeson 安装 aeson $ cabal install aeson Installed aeson-1.2.3.0 Prelude> :m +Data.Aeson Prelude ...

  8. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  9. Haskell语言学习笔记(77)Data.HashSet

    安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...

随机推荐

  1. ZedGraph使用经验(转帖)

     更改背景色  myPane.Fill = new Fill(Color.Black); Zedgraph 柱状图的宽度   gp.BarSettings.ClusterScaleWidth = 2; ...

  2. vqmod for opencart插件制作进阶与技巧

    FROM: http://www.sdtclass.com/4799.html 15年的时候,我写过一篇文章<略谈 vqmod for opencart 插件制作过程>,也不知道被哪些人抄 ...

  3. js搞定网页的简繁转换

    对网页进行简繁字体转换的方法一般有两种:一是使用<简繁通>这样的专业软件,另外一种是制作两套版本的网页.显然,这两种方法都较为麻烦,而且专业软件一般不能用于免费的空间.笔者在这里给大家提供 ...

  4. php中is_null,empty,isset,unset 的区别详细介绍

    is_null, empty, isset, unset 我们先来看看这4个函数的描述. isset 判断变量是否已存在(配置)unset 把变量删除(释放)掉empty 判断变量是否为空is_nul ...

  5. phper必知必会之数组指针(四)

    数组指针 1.介绍几个数组指针的函数 current() - 返回数组中的当前单元 end() - 将数组的内部指针指向最后一个单元 prev() - 将数组的内部指针倒回一位 reset() - 将 ...

  6. asp.net 网站 发布时 去掉.cs文件

    VS2013在WIN8下扁平的UI和我今天锈垢的大脑,让找这个设置找了好半天!!!   OK,言归正传.   在要发布的网站上右键,选择"发布网站".   在发布窗口中,会让你选择 ...

  7. 【Spring学习笔记-MVC-8】SpringMVC之类型转换Converter

    作者:ssslinppp       1. 摘要 在spring 中定义了3中类型转换接口,分别为: Converter接口              :使用最简单,最不灵活: ConverterFa ...

  8. 在Linux 系统 Latex安装 使用入门教程

    来源: http://blog.chinaunix.net/u/25605/showart_2100398.html 入门介绍好文:TeX.LaTeX.TeXLive 小结 笔记详情:http://v ...

  9. eclipse 常用jar包总结

    BeanUtils: DbUtils: FileUpload: IO: Lang: Logging: cglib: mysql-connector: Pool:[datasource] DBCP:[d ...

  10. ElasticSearch 索引模块——全文检索

    curl -XPOST http://master:9200/djt/user/3/_update -d '{"doc":{"name":"我们是中国 ...