HaskellのData.Sequenceモジュールの操作ガイド

Data.Sequenceの基本操作

Prelude> import Data.Sequence as DS
Prelude DS> :set -XOverloadedLists
Prelude DS> 

シーケンスの構築

Prelude DS> DS.empty
fromList []
Prelude DS> DS.singleton 5
fromList [5]
Prelude DS> 5 <| [6,7]
fromList [5,6,7]
Prelude DS> [6,7] |> 8
fromList [6,7,8]
Prelude DS> [5,6] >< [7,8]
fromList [5,6,7,8]

繰り返し操作

Prelude DS> DS.replicate 4 7
fromList [7,7,7,7]
Prelude DS> DS.replicateA 4 (Right 7)
Right [7,7,7,7]
Prelude DS> DS.replicateM 4 (Right 7)
Right [7,7,7,7]
Prelude DS> DS.cycleTaking 10 [2,3,4]
fromList [2,3,4,2,3,4,2,3,4,2]

反復的な構築

Prelude DS> DS.iterateN 6 (*2) 1
fromList [1,2,4,8,16,32]
Prelude DS> DS.unfoldr (\x -> if x > 100 then Nothing else Just (x, x*2)) 1
fromList [1,2,4,8,16,32,64]

クエリ操作

Prelude DS> DS.null []
True
Prelude DS> DS.length [10,20,30]
3

ビュー操作

Prelude DS> DS.viewl [10,20,30]
10 :< fromList [20,30]
Prelude DS> DS.viewr [10,20,30]
fromList [10,20] :> 30

スキャン操作

Prelude DS> DS.scanl (*) 1 [2,3,4]
fromList [1,2,6,24]
Prelude DS> DS.scanl1 (*) [2,3,4]
fromList [2,6,24]
Prelude DS> DS.scanr (*) 1 [2,3,4]
fromList [24,12,4,1]
Prelude DS> DS.scanr1 (*) [2,3,4]
fromList [24,12,4]

部分列操作

Prelude DS> DS.tails [1..5]
fromList [fromList [1,2,3,4,5],fromList [2,3,4,5],fromList [3,4,5],fromList [4,5],fromList [5],fromList []]
Prelude DS> DS.inits [1..5]
fromList [fromList [],fromList [1],fromList [1,2],fromList [1,2,3],fromList [1,2,3,4],fromList [1,2,3,4,5]]
Prelude DS> DS.chunksOf 3 [1..8]
fromList [fromList [1,2,3],fromList [4,5,6],fromList [7,8]]
Prelude DS> DS.takeWhileL odd [3,5,4,7,9]
fromList [3,5]
Prelude DS> DS.takeWhileR odd [3,5,4,7,9]
fromList [7,9]
Prelude DS> DS.dropWhileL odd [3,5,4,7,9]
fromList [4,7,9]
Prelude DS> DS.dropWhileR odd [3,5,4,7,9]
fromList [3,5,4]
Prelude DS> DS.spanl odd [3,5,4,7,9]
[3,5],fromList [4,7,9]
Prelude DS> DS.spanr odd [3,5,4,7,9]
[7,9],fromList [3,5,4]
Prelude DS> DS.breakl odd [3,5,4,7,9]
[],fromList [3,5,4,7,9]
Prelude DS> DS.breakr odd [3,5,4,7,9]
[],fromList [3,5,4,7,9]
Prelude DS> DS.partition odd [2,3,4,5,6,7]
[3,5,7],fromList [2,4,6]
Prelude DS> DS.filter odd [2,3,4,5,6,7]
fromList [3,5,7]

ソート操作

Prelude DS> DS.sort ["apple","banana","cherry"]
fromList ["apple","banana","cherry"]
Prelude DS> DS.sortBy (compare `on` length) ["a","hello","hi"]
fromList ["a","hi","hello"]
Prelude DS> DS.unstableSort ["apple","banana","cherry"]
fromList ["apple","banana","cherry"]
Prelude DS> DS.unstableSortBy (compare `on` length) ["a","hello","hi"]
fromList ["a","hi","hello"]

インデックス操作

Prelude DS> DS.lookup 2 [10..15]
Just 13
Prelude DS> [10..15] !? 2
Just 13
Prelude DS> [10..15] `DS.index` 2
13
Prelude DS> DS.adjust (+5) 2 [10..15]
fromList [10,11,18,13,14,15]
Prelude DS> DS.update 2 99 [10..15]
fromList [10,11,99,13,14,15]
Prelude DS> DS.take 4 [10..15]
fromList [10,11,12,13]
Prelude DS> DS.drop 4 [10..15]
fromList [14,15]
Prelude DS> DS.insertAt 2 99 [10..15]
fromList [10,11,99,12,13,14,15]
Prelude DS> DS.deleteAt 2 [10..15]
fromList [10,11,13,14,15]
Prelude DS> DS.splitAt 3 [10..15]
[10,11,12],fromList [13,14,15]
Prelude DS> DS.elemIndexL 12 [10,11,12,12,12,13,14]
Just 2
Prelude DS> DS.elemIndexL 12 [10,11,12,12,15,12,12,13,14]
Just 2
Prelude DS> DS.elemIndicesL 12 [10,11,12,12,15,12,12,13,14]
[2,3,5,6]
Prelude DS> DS.elemIndexR 12 [10,11,12,12,15,12,12,13,14]
Just 6
Prelude DS> DS.elemIndicesR 12 [10,11,12,12,15,12,12,13,14]
[6,5,3,2]
Prelude DS> DS.findIndexL even [11,12,13,13,16,13,13,14,15]
Just 1
Prelude DS> DS.findIndicesL even [11,12,13,13,16,13,13,14,15]
[1,4,7]
Prelude DS> DS.findIndexR even [11,12,13,13,16,13,13,14,15]
Just 7
Prelude DS> DS.findIndicesR even [11,12,13,13,16,13,13,14,15]
[7,4,1]

畳み込み操作

Prelude DS> DS.foldMapWithIndex (\i x -> [x+i]) [1,2,3]
[1,3,5]
Prelude DS> DS.foldlWithIndex (\acc i x -> acc + i + x) 0 [1,2,3]
9
Prelude DS> DS.foldrWithIndex (\i x acc -> i + x + acc) 0 [1,2,3]
9

変換操作

Prelude DS> DS.mapWithIndex (+) [10,20,30]
fromList [10,21,32]
Prelude DS> DS.traverseWithIndex (\i x -> [x+i, x+i+1]) [1,2,3]
[fromList [1,3,5],fromList [1,3,6],fromList [1,4,5],fromList [1,4,6],fromList [2,3,5],fromList [2,3,6],fromList [2,4,5],fromList [2,4,6]]
Prelude DS> DS.reverse [10,20,30,40]
fromList [40,30,20,10]
Prelude DS> DS.intersperse 0 [10,20,30]
fromList [10,0,20,0,30]

結合操作

Prelude DS> DS.zip [10,20,30] [1,2,3]
fromList [(10,1),(20,2),(30,3)]
Prelude DS> DS.zipWith (+) [10,20,30] [1,2,3]
fromList [11,22,33]

タグ: Haskell data-sequence functional-programming list-processing

5月19日 04:06 投稿