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 ...
随机推荐
- POJ3666序列最小差值
题目:http://poj.org/problem?id=3666 dp方程可以是 d [ i ] [ j ] = min ( d [ i - 1 ] [ k ] ) + abs ( a [ i ] ...
- android调节音量——AudioManager的应用
Android中可以通过程序获取系统手机的铃声和音量.同样,也可以设置铃声和音量.android中给出了AudioManager类来实现音量获取.音量控制. 本篇基于 Android API 中的 A ...
- mySQL 教程 第8章 视图
创建视图的目的 简单 隐藏数据复杂性 安全 可以对视图授权 数据独立 可以屏蔽表结构变化对用户的影响,比如增加列,更改列名 创建视图 1. 创建单表视图 以下视图显示JAVA班的学生姓名.身份证号和班 ...
- c 结构体的队列
头文件 lsg_queue.h #pragma once #include<stdbool.h> /* 链式栈接口的定义头文件 */ #define true 1 #define fals ...
- oracle schema 白话文详解
概述: (一)什么Oracle叫用户(user): A user is a name defined in the database that can connect to and access ob ...
- 【python】正则表达式-group和group的区别
__author__ = 'paul' import re a = "123abc456" print re.search("([0-9]*)([a-z]*)([0-9] ...
- Oracle学习操作(7)用户、权限、角色
一.oracle用户: 二.权限 1.系统权限: sys登陆创建c##test用户后,给用户c##test授权,并且带有传播性: SQL> create user c##test identif ...
- 高可用服务设计之二:Rate limiting 限流与降级
<高可用服务设计之二:Rate limiting 限流与降级> <nginx限制请求之一:(ngx_http_limit_conn_module)模块> <nginx限制 ...
- 长短时记忆网络(LSTM)
长短时记忆网络 循环神经网络很难训练的原因导致它的实际应用中很处理长距离的依赖.本文将介绍改进后的循环神经网络:长短时记忆网络(Long Short Term Memory Network, LSTM ...
- Java与C++语法的区别
1. 注释可以在Java程序中起到文档标记的作用 类文档标记: 1)@version 2)@author 3)@param 4)@return 5)@exception 2. Java的字符占两个字节 ...