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 ...
随机推荐
- 使用js提交form表单的两种方法
提交form表单的时候瑶族一些简单的验证,验证完后才能提交,避免无效提交. 1.当输入用户名和密码为空的时候,需要判断.这时候就用到了校验用户名和密码,这个需要在前端页面写:有两种方法,一种是用sub ...
- linux php添加ftp扩展模块
linux php添加ftp扩展模块 进入源码目录cd php-5.2.13/ext/ftp#运行phpize生成configure/usr/local/php/bin/phpize./configu ...
- C++进阶--const和函数(const and functions)
// const和函数一起使用的情况 class Dog { int age; string name; public: Dog() { age = 3; name = "dummy&quo ...
- mac电脑链接安卓手机的方法
https://blog.csdn.net/liubin9043/article/details/78928253/ 我用了个爱莫 不错 http://web.airmore.com
- JSON: JSON 用法
ylbtech-JSON: JSON 用法 1. JSON Object creation in JavaScript返回顶部 1. <!DOCTYPE html> <html> ...
- sublime text 3 3143
下载链接:https://download.sublimetext.com/Sublime%20Text%20Build%203143%20x64%20Setup.exe 注册信息:sublime t ...
- commons.httpclient-3.X.jar 和 httpclient-4.x.jar是个什么关系?
最近看项目的代码,看到工程中有两个jar包张的很像,一个是commons.httpclient-3.1.jar,一个是httpclient4.2.1.jar,很纳闷,而且这两个包里都有HttpClie ...
- shell 6基本运算符
shell支持多种运算符: * 算数运算符 * 关系运算符 * 布尔运算符 * 字符串运算符 * 文件测试运算符 算数运算符 + 加 `expr $a + $b` 结果为 30 - 减 `expr $ ...
- HTTP发包工具 -HTTPie
原文: https://zm8.sm-tc.cn/?src=l4uLj8XQ0IuekZWWi5bRk5CZi5qN0ZyQktCPkIyL0M6cnMmcx8qdoM7PnMrIyMnI&u ...
- linux安装oracle的官方文档
1:https://docs.oracle.com/cd/E11882_01/install.112/e47689/toc.htm 2:https://oracle-base.com/articles ...