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 ...
随机推荐
- vqmod for opencart插件制作进阶与技巧
FROM: http://www.sdtclass.com/4799.html 15年的时候,我写过一篇文章<略谈 vqmod for opencart 插件制作过程>,也不知道被哪些人抄 ...
- Microsoft Dynamics CRM 2011 新建实体 需要注意的细节
新建一个实体,需要红色框内的是否勾选的意义,可以进一步加深对CRM的理解.如图: 下面对部分的进行了自我的理解,不对的地方,还请大家指出来.互相学习. 1.CRM2011中,在活动方面加强的新特性包括 ...
- [boost] : asser库用法
基本用法 需要包含头文件#include <boost/assert.hpp> assert库定义了两个断言宏 BOOST_ASSERT BOOSE_ASSERT_MSG 第一种形式等价于 ...
- 转Python 和C#的交互
http://www.cnblogs.com/wilber2013/category/708919.html IronPython和C#交互 IronPython是一个.NET平台上的Python ...
- Jquery获取元素高度
第一步, 获取你要得到高度的那个div的jQuery对象, 获得方法有很多很多种, 具体你可以看一下jQuery API文档里的选择器部分, 在这里我只跟你说一个最直接的方法, 通过id获得: $(& ...
- <<APUE>> 线程
一个进程在同一时刻只能做一件事情,线程可以把程序设计成在同一时刻能够做多件事情,每个线程处理各自独立的任务.线程包括了表示进程内执行环境必需的信息,包括进程中标识线程的线程ID.一组寄存器值.栈.调度 ...
- Delphi调用网页美化SQL
百度搜索在线美化SQL语句的网站,为了加快解析速度,这里已下载到本地. 然后delphi用webbrowse载入本地的网页,然后把sql传进去,美化后取出来. 效果如下图 点击下载源码
- [转]判断是否 Win7 且需要管理员权限
public static void Load() { if (NeedAdmin()) { new Form().ShowDialog(); Environment.Exit(); } } publ ...
- linux shell 命令常用快捷键
下面是一些shell的常用快捷键,快捷键玩熟悉了在一定程度上是可以提高工作效率滴… Ctrl + a 切换到命令行开始 Ctrl + e 切换到命令行末尾 Ctrl + l 清除屏幕内容 Ctrl + ...
- 试讲DOCKER专用
内容概要: DOCKER简介 为什么要用DOCKER DOCKER的应用场景 DOCKER基础 一 DOCKER简介 Docker是Docker.Inc公司开源的一个基于轻量级虚拟化技术的容器引擎项目 ...