Haskell语言学习笔记(53)Data.Sequence
Data.Sequence
Prelude> import Data.Sequence as Seq
Prelude Seq> :set -XOverloadedLists
Prelude Seq>
Construction
Prelude Seq> empty
fromList []
Prelude Seq> singleton 3
fromList [3]
Prelude Seq> 3 <| [4,5]
fromList [3,4,5]
Prelude Seq> [4,5] |> 6
fromList [4,5,6]
Prelude Seq> [3,4] >< [5,6]
fromList [3,4,5,6]
Repetition
Prelude Seq> Seq.replicate 3 3
fromList [3,3,3]
Prelude Seq> Seq.replicateA 3 (Just 3)
Just [3,3,3]
Prelude Seq> Seq.replicateM 3 (Just 3)
Just [3,3,3]
Prelude Seq> cycleTaking 9 [3,4,5]
fromList [3,4,5,3,4,5,3,4,5]
Iterative construction
Prelude Seq> iterateN 5 (+1) 1
fromList [1,2,3,4,5]
Prelude Seq> unfoldr (\b -> if b < 10 then Just (b, b+1) else Nothing) 1
fromList [1,2,3,4,5,6,7,8,9]
Queries
Prelude Seq> Seq.null []
True
Prelude Seq> Seq.length [1,2,3]
3
Views
Prelude Seq> viewl [1,2,3]
1 :< fromList [2,3]
Prelude Seq> viewr [1,2,3]
fromList [1,2] :> 3
Scans
Prelude Seq> Seq.scanl (+) 0 [1,2,3]
fromList [0,1,3,6]
Prelude Seq> Seq.scanl1 (+) [1,2,3]
fromList [1,3,6]
Prelude Seq> Seq.scanr (+) 0 [1,2,3]
fromList [6,5,3,0]
Prelude Seq> Seq.scanr1 (+) [1,2,3]
fromList [6,5,3]
Sublists
Prelude Seq> Seq.tails [1..6]
fromList [fromList [1,2,3,4,5,6],fromList [2,3,4,5,6],fromList [3,4,5,6],fromList [4,5,6],fromList [5,6],fromList [6],fromList []]
Prelude Seq> Seq.inits [1..6]
fromList [fromList [],fromList [1],fromList [1,2],fromList [1,2,3],fromList [1,2,3,4],fromList [1,2,3,4,5],fromList [1,2,3,4,5,6]]
Prelude Seq> chunksOf 2 [1..6]
fromList [fromList [1,2],fromList [3,4],fromList [5,6]]
Prelude Seq> takeWhileL even [2,4,5,6,8]
fromList [2,4]
Prelude Seq> takeWhileR even [2,4,5,6,8]
fromList [6,8]
Prelude Seq> dropWhileL even [2,4,5,6,8]
fromList [5,6,8]
Prelude Seq> dropWhileR even [2,4,5,6,8]
fromList [2,4,5]
Prelude Seq> spanl even [2,4,5,6,8]
[2,4],fromList [5,6,8]
Prelude Seq> spanr even [2,4,5,6,8]
[6,8],fromList [2,4,5]
Prelude Seq> breakl even [2,4,5,6,8]
[],fromList [2,4,5,6,8]
Prelude Seq> breakr even [2,4,5,6,8]
[],fromList [2,4,5,6,8]
Prelude Seq> Seq.partition even [2,4,5,6,8]
[2,4,6,8],fromList [5]
Prelude Seq> Seq.filter even [2,4,5,6,8]
fromList [2,4,6,8]
Sorting
Prelude Seq Data.Function> Seq.sort ["one","two","three","four"]
fromList ["four","one","three","two"]
Prelude Seq Data.Function> Seq.sortBy (compare `on` Prelude.length) ["one","two","three","four"]
fromList ["one","two","four","three"]
Prelude Seq Data.Function> unstableSort ["one","two","three","four"]
fromList ["four","one","three","two"]
Prelude Seq Data.Function> unstableSortBy (compare `on` Prelude.length) ["one","two","three","four"]
fromList ["one","two","four","three"]
Indexing
Prelude Seq> Seq.lookup 3 [1..6]
Just 4
Prelude Seq> [1..6] !? 3
Just 4
Prelude Seq> [1..6] `index` 3
4
Prelude Seq> adjust (+1) 3 [1..6]
fromList [1,2,3,5,5,6]
Prelude Seq> update 3 1 [1..6]
fromList [1,2,3,1,5,6]
Prelude Seq> Seq.take 3 [1..6]
fromList [1,2,3]
Prelude Seq> Seq.drop 3 [1..6]
fromList [4,5,6]
Prelude Seq> insertAt 3 3 [1..6]
fromList [1,2,3,3,4,5,6]
Prelude Seq> deleteAt 3 [1..6]
fromList [1,2,3,5,6]
Prelude Seq> Seq.splitAt 3 [1..6]
[1,2,3],fromList [4,5,6]
Prelude Seq> elemIndexL 3 [1,2,3,3,3,4,5]
Just 2
Prelude Seq> elemIndexL 3 [1,2,3,3,6,3,3,4,5]
Just 2
Prelude Seq> elemIndicesL 3 [1,2,3,3,6,3,3,4,5]
[2,3,5,6]
Prelude Seq> elemIndexR 3 [1,2,3,3,6,3,3,4,5]
Just 6
Prelude Seq> elemIndicesR 3 [1,2,3,3,6,3,3,4,5]
[6,5,3,2]
Prelude Seq> findIndexL even [1,2,3,3,6,3,3,4,5]
Just 1
Prelude Seq> findIndicesL even [1,2,3,3,6,3,3,4,5]
[1,4,7]
Prelude Seq> findIndexR even [1,2,3,3,6,3,3,4,5]
Just 7
Prelude Seq> findIndicesR even [1,2,3,3,6,3,3,4,5]
[7,4,1]
Folds
Prelude Seq> foldMapWithIndex (\n a -> fmap (+n) a) [[1],[2]]
[1,3]
Prelude Seq> foldlWithIndex (\a b c -> a+b+c) 0 [1..3]
9
Prelude Seq> foldrWithIndex (\a b c -> a+b+c) 0 [1..3]
9
Transformations
Prelude Seq> mapWithIndex (+) [1..3]
fromList [1,3,5]
Prelude Seq> traverseWithIndex (\a b -> [b+a,b+a+1]) [1..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 Seq> Seq.reverse [1..4]
fromList [4,3,2,1]
Prelude Seq> Seq.intersperse 9 [1..3]
fromList [1,9,2,9,3]
Zips
Prelude Seq> Seq.zip [1..3] [1..3]
fromList [(1,1),(2,2),(3,3)]
Prelude Seq> Seq.zipWith (*) [1..3] [1..3]
fromList [1,4,9]
Haskell语言学习笔记(53)Data.Sequence的更多相关文章
- Haskell语言学习笔记(88)语言扩展(1)
ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...
- Haskell语言学习笔记(20)IORef, STRef
IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...
- Haskell语言学习笔记(69)Yesod
Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...
- Haskell语言学习笔记(79)lambda演算
lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...
- Haskell语言学习笔记(39)Category
Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...
- Haskell语言学习笔记(28)Data.Map
Map Prelude> import Data.Map as Map Prelude Map> :set -XOverloadedLists Prelude Map> Overlo ...
- Haskell语言学习笔记(48)Data.Tuple
Data.Tuple fst :: (a,b) -> a fst (x,_) = x snd :: (a,b) -> b snd (_,y) = y curry :: ((a, b) -& ...
- Haskell语言学习笔记(93)Data.Text
Data.Text.Read Prelude> :set -XOverloadedStrings Prelude> :m +Data.Text.Read Prelude Data.Text ...
- Haskell语言学习笔记(81)Data.Typeable
Data.Typeable 利用 Data.Typeable,可以打印动态类型信息. class Typeable (a :: k) where typeRep# :: TypeRep a typeR ...
随机推荐
- Angularjs 事件指令
1. 点击事件 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...
- 详细说明 配置 Sublime Text 开发node.js(windows)包括sub2和sub3的区别
1.先安装Sublime Text 2或者3皆可 2.下载 sublime Text 的nodejs插件 得到那个zip包(后面会介绍用Package Control安装) 3.下载后解压 直接改名 ...
- linux中uptime命令查看linux系统负载
阅读目录 uptime cat /proc/loadavg 何为系统负载呢? 进阶参考 uptime 另外还有一个参数 -V(大写),是用来查询版本的 [appdeploy@CNSZ22PL0088: ...
- python unittest单元测试框架-2discover
基于TestLoader提供的discover方法实现用例执行 当用例达达到数十条后,在runtest.py文件中通过addTest()添加/删除测试用例就非常麻烦.此时可以使用discover方法找 ...
- WAP网站WML或HTML页面自适应手机屏幕实现方法
把图片和div的宽度都设置成:width:100%就可以了
- 1113 Integer Set Partition (25 分)
1113 Integer Set Partition (25 分) Given a set of N (>1) positive integers, you are supposed to pa ...
- API网关Kong系列(二)部署
部署环境: [OS] centos 6.8(如果是centos6.5,请自行先升级到6.8,否则不支持docker) [Docker] Client version: 1.7.1 Client API ...
- javascript节点操作appendChild()
cloneNode(a)方法接受一个布尔值参数,表示是否深拷贝 true:表示执行深拷贝,复制本节点以及整个子节点树. false:浅拷贝.只复制节点本身. 复制后返回的节点副本属于文档所有,但是并没 ...
- javascript面向对象之Object.defineProperty(a,b,c)
/* Object.defineProperty(a,b,c);介绍 a:需要属性设置的对象 b:需要设置的属性名,(键值) c:是一个用于描述属性值得json数据.这个json数据有configur ...
- 6.19-response(响应),session(会话技术,服务器端技术) 内置对象,application(内置对象),pageContext (内置对象),cookie(客户端技术)
一.response(响应) 页面重定向 response.sendRedirect(""); 转发: request.getRequestDispatcher("&qu ...