zoukankan      html  css  js  c++  java
  • 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")]
    
  • 相关阅读:
    软件开的目录规范+sys,os,time模块
    模块与包
    匿名函数+函数递归+二分法+面向过程编程
    快捷键
    补充叠加多个装饰器:加载顺序与执行顺序+迭代器+自定义迭代器的方式:生成器+三元表达式
    闭包函数的应用+内置函数
    函数对象+函数嵌套+名称空间与作用域+闭包函数
    SP15637 Mr Youngs Picture Permutations 高维动态规划
    LG3825/BZOJ4945/LOJ2305 「NOI2017」游戏 dfs+2-SAT
    LG1198/BZOJ1012 「JSOI2008」最大数 线段树+离线
  • 原文地址:https://www.cnblogs.com/zwvista/p/8046035.html
Copyright © 2011-2022 走看看