• 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")]
    
  • 相关阅读:
    PLSQL_Oracle Trigger触发器的基本概念和用法
    PLSQL_Oracle Object所有数据库对象类型汇总和简解(概念)
    OAF_EO系列2
    OAF_EO系列1
    OAF_OAF Debug And Log调试和记录工具的详解(案例)
    OAF_OAF Exception Handling异常处理(概念)
    DBA_FND Load程式迁移工具介绍和应用(案例)
    Form_Form标准控件Folder开发解析(案列)
    BEvent_标准BusinessEvent用以监控供应商的修改(案例)
    PLSQL_Oracle Logon Trigger的建立
  • 原文地址:https://www.cnblogs.com/zwvista/p/8046035.html
Copyright © 2020-2023  润新知