Haskell语言学习笔记(28)Data.Map
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的更多相关文章
- Haskell语言学习笔记(88)语言扩展(1)
ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...
- Go语言学习笔记十三: Map集合
Go语言学习笔记十三: Map集合 Map在每种语言中基本都有,Java中是属于集合类Map,其包括HashMap, TreeMap等.而Python语言直接就属于一种类型,写法上比Java还简单. ...
- Haskell语言学习笔记(38)Lens(1)
Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...
- Haskell语言学习笔记(69)Yesod
Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...
- Haskell语言学习笔记(20)IORef, STRef
IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...
- Haskell语言学习笔记(79)lambda演算
lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...
- Haskell语言学习笔记(66)Aeson
Data.Aeson 安装 aeson $ cabal install aeson Installed aeson-1.2.3.0 Prelude> :m +Data.Aeson Prelude ...
- Haskell语言学习笔记(39)Category
Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...
- Haskell语言学习笔记(77)Data.HashSet
安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...
随机推荐
- bzoj 3622 已经没有什么好害怕的了——二项式反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3622 令 f[i] 表示钦定 i 对 a[ ]>b[ ] 的关系的方案数:g[i] 表 ...
- [转]Nginx反向代理和负载均衡部署指南
Nginx反向代理和负载均衡部署指南 1. 安装 1) 从Nginx官网下载页面(http://nginx.org/en/download.html)下载Nginx最 ...
- Django Ajax登录 防止CSRF
什么是CSRF 维基百科: 跨站请求伪造(英语:Cross-site request forgery),也被称为 one-click attack 或者 session riding,通常缩写为 CS ...
- java操作Excel之POI(1)
一.新建工作簿.sheet.单元格 public static void main(String[] args) throws Exception { Workbook wb = new HSSFWo ...
- [转]SQL SERVER 的排序规则
如何更改SQL SERVER 2000的排序规则 -- 增加复合主键语句 Alter Table tableName Add primary key (field1,field2) Alter dat ...
- golang web框架 beego 学习 (三) beego获取参数
直接上常用的例子吧: A: 获取URL中的参数 router func init() { beego.Router("/task/?:id/?:name", &co ...
- 递归神经网络(Recursive Neural Network, RNN)
信息往往还存在着诸如树结构.图结构等更复杂的结构.这就需要用到递归神经网络 (Recursive Neural Network, RNN),巧合的是递归神经网络的缩写和循环神经网络一样,也是RNN,递 ...
- ZooKeeper系列(2) 安装部署 (转)
原文地址:http://www.cnblogs.com/wuxl360/p/5817489.html 一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模 ...
- 【Linux_Unix系统编程】Chapter9 进程凭证
chapter9 进程凭证 每个进程都有一套用数字表示的用户ID(UID)和组ID(GID).有时也将这些ID称子为进程凭证. 1:实际用户ID和实际组ID 2:有效用户ID和有效组ID 3:保存的s ...
- web基础 (一) http协议
一.什么是web服务 浏览器与网页服务端发起的请求与回应(返回的是一堆字符串,浏览器去渲染生成页面!)都是 标准的CS模式 ---- bs模式:客户端用浏览器即可,服务端需要自己去写 http协议是建 ...