安装 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的更多相关文章

  1. Haskell语言学习笔记(88)语言扩展(1)

    ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

  2. Haskell语言学习笔记(77)Data.HashSet

    安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...

  3. Haskell语言学习笔记(69)Yesod

    Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

  4. Haskell语言学习笔记(20)IORef, STRef

    IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

  5. Haskell语言学习笔记(79)lambda演算

    lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

  6. Haskell语言学习笔记(39)Category

    Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

  7. Haskell语言学习笔记(28)Data.Map

    Map Prelude> import Data.Map as Map Prelude Map> :set -XOverloadedLists Prelude Map> Overlo ...

  8. Haskell语言学习笔记(93)Data.Text

    Data.Text.Read Prelude> :set -XOverloadedStrings Prelude> :m +Data.Text.Read Prelude Data.Text ...

  9. Haskell语言学习笔记(81)Data.Typeable

    Data.Typeable 利用 Data.Typeable,可以打印动态类型信息. class Typeable (a :: k) where typeRep# :: TypeRep a typeR ...

随机推荐

  1. Spring 集成开发工具(STS)安装及配置

    安装 spring 集成开发工具,下载地址:https://spring.io/tools 下载后,解压,双击 STS ,运行. 如果提示: 去oracle的网站上下载了1.8版本的jdk,下载地址如 ...

  2. Angular 4 重定向路由

    重定向路由: 用户访问一个指定的地址时,将其重新向到另一个指定的地址 (接前面的一节) 配置如下: 之前http://localhost:4200 会进入主页,现在将主页导航改为home,http:/ ...

  3. centOS 6.5关闭防火墙步骤

    centOS 6.5关闭防火墙步骤 关闭命令:  service iptables stop         永久关闭防火墙:chkconfig iptables off 两个命令同时运行,运行完成后 ...

  4. 常用Web框架

    1.Aliceui Aliceui是支付宝的样式解决方案,是一套精选的基于 spm 生态圈的样式模块集合,是 Arale 的子集,也是一套模块化的样式命名和组织规范,是写 CSS 的更好方式. git ...

  5. 什么是JavaBean、bean? 什么是POJO、PO、DTO、VO、BO ? 什么是EJB、EntityBean?

    什么是JavaBean.bean?  什么是POJO.PO.DTO.VO.BO ?  什么是EJB.EntityBean?   前言:   在Java开发中经常遇到这些概念问题,有的可能理解混淆,有的 ...

  6. ALGO-1_蓝桥杯_算法训练_区间k大数查询

    问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二行包含n个正整数,表示给定的序列. 第三个包含一个正整数m,表示询问个数 ...

  7. C# winfrom ComboBox 调整下拉菜单的高度

    1.设置属性 // 1.属性设置 DrawMode ->OwnerDrawVariable this.cboBoxPostID.DrawMode = System.Windows.Forms.D ...

  8. 常用命名_html

    以下为于页面模块的常用命名 头:header 内容:content/container 尾:footer 导航:nav 侧栏:sidebar 栏目:column 页面外围控制整体布局宽度:wrappe ...

  9. CentOS 6.2图形界面允许root用户登录

    测试环境中,很多操作需要root用户权限,为了提高测试效率,因此有时希望直接使用root用户登陆系统,但是CentOS的图形界面默认不允许root用户登陆,这是由于pam的限制,需要进行如下设定: 1 ...

  10. virtualenv之python虚拟环境

    virtualEnv可以方便的解决不同项目中,对类库的依赖问题.首先将常用的类库安装在系统环境中,然后为每个项目安装独立的类库环境.这样子可以保证每个项目都运行在独立的类库环境中. virtualen ...