Haskell语言学习笔记(65)Data.HashMap
安装 unordered-containers
$ cabal install unordered-containers
Installed unordered-containers-0.2.9.0
Prelude> import Data.HashMap.Lazy as HashMap
Prelude HashMap> :set -XOverloadedLists
Prelude HashMap>
Construction
Prelude HashMap> empty
fromList []
Prelude HashMap> singleton "a" 1
fromList [("a",1)]
Basic interface
Prelude HashMap> HashMap.null (singleton "a" 1)
False
Prelude HashMap> HashMap.null empty
True
Prelude HashMap> size [(1,'a'), (2,'c'), (3,'b')]
3
Prelude HashMap> member 5 [(5,'a'), (3,'b')]
True
Prelude HashMap> HashMap.lookup "John" [("John","Sales"), ("Bob","IT")]
Just "Sales"
Prelude HashMap> lookupDefault 'x' 1 [(5,'a'), (3,'b')]
'x'
Prelude HashMap> lookupDefault 'x' 5 [(5,'a'), (3,'b')]
'a'
Prelude HashMap> [(5,'a'), (3,'b')] ! 5
'a'
Prelude HashMap> insert 5 'x' [(5,'a'), (3,'b')]
fromList [(3,'b'),(5,'x')]
Prelude HashMap> insert 7 'x' [(5,'a'), (3,'b')]
fromList [(3,'b'),(5,'a'),(7,'x')]
Prelude HashMap> insert 5 'x' empty
fromList [(5,'x')]
Prelude HashMap> delete 5 [(5,"a"), (3,"b")]
fromList [(3,"b")]
Prelude HashMap> adjust ("new " ++) 5 [(5,"a"), (3,"b")]
fromList [(3,"b"),(5,"new a")]
Prelude HashMap> let f x = if x == "a" then Just "new a" else Nothing
Prelude HashMap> update f 5 [(5,"a"), (3,"b")]
fromList [(3,"b"),(5,"new a")]
Prelude HashMap> let f _ = Nothing
Prelude HashMap> alter f 5 [(5,"a"), (3,"b")]
fromList [(3,"b")]
Prelude HashMap> let f _ = Just "c"
Prelude HashMap> alter f 7 [(5,"a"), (3,"b")]
fromList [(3,"b"),(5,"a"),(7,"c")]
Union
Prelude HashMap> union [(5, "a"), (3, "b")] [(5, "A"), (7, "C")]
fromList [(3,"b"),(5,"a"),(7,"C")]
Prelude HashMap> unions [[(5, "a"), (3, "b")], [(5, "A"), (7, "C")], [(5, "A3"), (3, "B3")]]
fromList [(3,"b"),(5,"a"),(7,"C")]
Transformations
Prelude HashMap> HashMap.map (++ "x") [(5,"a"), (3,"b")]
fromList [(3,"bx"),(5,"ax")]
Difference and intersection
Prelude HashMap> difference [(5, "a"), (3, "b")] [(5, "A"), (7, "C")]
fromList [(3,"b")]
Prelude HashMap> intersection [(5, "a"), (3, "b")] [(5, "A"), (7, "C")]
fromList [(5,"a")]
Folds
Prelude HashMap> let f len a = len + (length a)
Prelude HashMap> HashMap.foldl' f 0 [(5,"a"), (3,"bbb")]
4
Prelude HashMap> let f a len = len + (length a)
Prelude HashMap> HashMap.foldr f 0 [(5,"a"), (3,"bbb")]
4
Filter
Prelude HashMap> HashMap.filter (> "a") [(5,"a"), (3,"b")]
fromList [(3,"b")]
Prelude HashMap> let f x = if x == "a" then Just "new a" else Nothing
Prelude HashMap> mapMaybe f [(5,"a"), (3,"b")]
fromList [(5,"new a")]
Conversions
Prelude HashMap> keys [(5,"a"), (3,"b")]
[3,5]
Prelude HashMap> elems [(5,"a"), (3,"b")]
["b","a"]
Lists
Prelude HashMap> toList [(5,"a"), (3,"b")]
[(3,"b"),(5,"a")]
Prelude HashMap> fromList [(5,"a"), (3,"b"), (5, "c")]
fromList [(3,"b"),(5,"c")]
Haskell语言学习笔记(65)Data.HashMap的更多相关文章
- Haskell语言学习笔记(88)语言扩展(1)
ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...
- Haskell语言学习笔记(77)Data.HashSet
安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...
- 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语言学习笔记(39)Category
Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...
- Haskell语言学习笔记(28)Data.Map
Map Prelude> import Data.Map as Map Prelude Map> :set -XOverloadedLists Prelude Map> Overlo ...
- Haskell语言学习笔记(93)Data.Text
Data.Text.Read Prelude> :set -XOverloadedStrings Prelude> :m +Data.Text.Read Prelude Data.Text ...
- Haskell语言学习笔记(81)Data.Typeable
Data.Typeable 利用 Data.Typeable,可以打印动态类型信息. class Typeable (a :: k) where typeRep# :: TypeRep a typeR ...
随机推荐
- FineUI 3升级4.1.1时,SingleClickExpand属性改什么了? (树控件单击展开)
private Tree InitTreeMenu(List<Menu> menus) { Tree treeMenu = new Tree(); treeMenu.ID = " ...
- MFC信号量使用指南
DEMO程序:TestSemaphore_DEMO.rar 源代码:TestSemaphore_SRC.rar ---关键代码如下--- void CTestSemaphoreDlg::OnBnCli ...
- .Net和SqlServer的事务处理实例
1,SqlServer存储过程的事务处理一种比较通用的出错处理的模式大概如下:Create procdure prInsertProducts( @intProductId int, @chvProd ...
- dede模块管理一片空白或没有列表内容的解决办法
为什么dede后台模块管理,打开之后一片空白,又或者没有列表,插件与其他模块的使用也是正常的. 这主要是因为我们在安装模块,然后又卸载模块,卸载的时候选择了删除安装文件,就会出这个问题. 这里面分为两 ...
- win xp 关闭动画屏幕角色,那只小狗
这个动画屏幕角色每次会占用两秒左右的时间. 在搜索选项中打开"改变首选项(G)" -选择 “不使用动画屏幕角色(S)”. - -
- JpGraph使用详解http://5ydycm.blog.51cto.com/115934/177498 http://www.cnblogs.com/txw1958/archive/2013/08/18/php-charts.html
下载 在官方网站 http://www.aditus.nu/jpgraph/ 下载jpgraph,其中1.X系列是用于PHP4的,2.X系列是用于PHP5的. 安装 将下载的得到的jpgraph压缩文 ...
- python 模块被引用多次但是里面的全局表达式总共只会执行一次
python 模块被引用多次但是里面的全局表达式总共只会执行一次
- El中调用静态方法
最近在项目中遇到需要调用静态方法的问题,形如: <c:forEach items="beans" var="bean"> <p>总数:$ ...
- ESB初步配置文件认识
每个项目的都有各自的场景,但是其实往小处说,场景的处理基本都是很相似,之前做copy文件的程序,其实就是一种很常见的ETL的过程(转移文件,异构系统通过文件系统交换数据,存在数据同步). 了解一下ET ...
- 小朋友学C语言(7)
数组 一.数组简介 C 语言支持数组数据结构,它可以存储一个固定大小的相同类型元素的顺序集合.数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量. 数组的声明并不是声明一个个单独的变量,比 ...