OMap - 順序付きMap
insertした順序を保ったMap
パッケージ
shell > emerge dev-haskell/ordered-containers
使用方法
ghciからモジュールの読み込み
Prelude> :m +Data.Map.Ordered
Prelude Data.Map.Ordered> let m = fromList [("key1", 10), ("key2", 9), ("key3", 9)] :: OMap String Int
Prelude Data.Map.Ordered> m
"key1",10),("key2",9),("key3",9)] fromList [(
最初にinsertする
Prelude Data.Map.Ordered> ("key4", 8) |< m
"key4",8),("key1",10),("key2",9),("key3",9)] fromList [(
最後にappendする
Prelude Data.Map.Ordered> m |> ("key4", 7)
"key1",10),("key2",9),("key3",9),("key4",7)] fromList [(
indexの位置で取得
indexは入れた順で0からn-1で指定
Prelude Data.Map.Ordered> elemAt m 0
Just ("key1",10)
指定のkeyで削除
Prelude Data.Map.Ordered> delete "key2" m
"key1",10),("key3",9)] fromList [(
2つのOMapをconcatする
Prelude Data.Map.Ordered> let m' = fromList [("key5", 30), ("key6", 31)] :: OMap String Int
Prelude Data.Map.Ordered> m |<> m'
"key1",10),("key2",9),("key3",9),("key5",30),("key6",31)] fromList [(
共通なOMapをかえす
keyが共通なものお左側のOMapを返してくる
Prelude Data.Map.Ordered> m |/\ (fromList [("k", 0), ("v", 222)])
fromList []
Prelude Data.Map.Ordered> m |/\ (fromList [("key1", 323), ("key2", 2222)])
fromList [("key1",10),("key2",9)]
Prelude Data.Map.Ordered> (fromList [("key1", 323), ("key2", 2222)]) |/\ m
fromList [("key1",323),("key2",2222)]
keyで値を取得
Maybe a で返してくる
Prelude Data.Map.Ordered> Data.Map.Ordered.lookup "key" m
Nothing
Prelude Data.Map.Ordered> Data.Map.Ordered.lookup "key1" m
Just 10
Prelude Data.Map.Ordered>
OMapから配列に戻す
入れた順にリストへまとめる
Prelude Data.Map.Ordered> assocs m
[("key1",10),("key2",9),("key3",9)]
キーの昇順に戻してくる
Prelude Data.Map.Ordered> let am = fromList [("z",10),("y", 123),("x", 20)] :: OMap String Int
Prelude Data.Map.Ordered> toAscList am
[("x",20),("y",123),("z",10)]
empty判定
Prelude Data.Map.Ordered> Data.Map.Ordered.null empty
True
その他ありますが、とりあえずここまで
Posted on 2021-08-13 07:26:33