NonEmpty(非空列表)

infixr 5 :|

data NonEmpty a = a :| [a]
deriving (Eq, Ord) instance Functor NonEmpty where
fmap f ~(a :| as) = f a :| fmap f as
b <$ ~(_ :| as) = b :| (b <$ as) instance Applicative NonEmpty where
pure a = a :| []
(<*>) = ap
liftA2 = liftM2 instance Monad NonEmpty where
~(a :| as) >>= f = b :| (bs ++ bs')
where b :| bs = f a
bs' = as >>= toList . f
toList ~(c :| cs) = c : cs

NonEmpty是一个非空的list类型,支持大多数 list 类型的操作。

Prelude> import Data.List.NonEmpty as NE
Prelude NE> a = 5 :| []
Prelude NE> :t a
a :: Num a => NonEmpty a
Prelude NE> NE.head a
5
Prelude NE> NE.tail a
[]
Prelude NE> b = 6 <| a
Prelude NE> b
6 :| [5]
Prelude NE> NE.map (+2) b
8 :| [7]
Prelude NE> 3 <$ b
3 :| [3]
Prelude NE> fromList [1,2,3]
1 :| [2,3]
Prelude NE> toList it
[1,2,3]
  • Haskell语言学习笔记(70)NonEmpty的更多相关文章

    1. Haskell语言学习笔记(88)语言扩展(1)

      ExistentialQuantification {-# LANGUAGE ExistentialQuantification #-} 存在类型专用的语言扩展 Haskell语言学习笔记(73)Ex ...

    2. Haskell语言学习笔记(79)lambda演算

      lambda演算 根据维基百科,lambda演算(英语:lambda calculus,λ-calculus)是一套从数学逻辑中发展,以变量绑定和替换的规则,来研究函数如何抽象化定义.函数如何被应用以 ...

    3. Haskell语言学习笔记(69)Yesod

      Yesod Yesod 是一个使用 Haskell 语言的 Web 框架. 安装 Yesod 首先更新 Haskell Platform 到最新版 (Yesod 依赖的库非常多,版本不一致的话很容易安 ...

    4. Haskell语言学习笔记(20)IORef, STRef

      IORef 一个在IO monad中使用变量的类型. 函数 参数 功能 newIORef 值 新建带初值的引用 readIORef 引用 读取引用的值 writeIORef 引用和值 设置引用的值 m ...

    5. Haskell语言学习笔记(39)Category

      Category class Category cat where id :: cat a a (.) :: cat b c -> cat a b -> cat a c instance ...

    6. Haskell语言学习笔记(72)Free Monad

      安装 free 包 $ cabal install free Installed free-5.0.2 Free Monad data Free f a = Pure a | Free (f (Fre ...

    7. Haskell语言学习笔记(44)Lens(2)

      自定义 Lens 和 Isos -- Some of the examples in this chapter require a few GHC extensions: -- TemplateHas ...

    8. Haskell语言学习笔记(38)Lens(1)

      Lens Lens是一个接近语言级别的库,使用它可以方便的读取,设置,修改一个大的数据结构中某一部分的值. view, over, set Prelude> :m +Control.Lens P ...

    9. Haskell语言学习笔记(92)HXT

      HXT The Haskell XML Toolbox (hxt) 是一个解析 XML 的库. $ cabal install hxt Installed hxt-9.3.1.16 Prelude&g ...

    随机推荐

    1. android切换前后台状态监听

      public class BaseApplication extends Application { private static BaseApplication instance; /** * 当前 ...

    2. redis作为mysql的缓存服务器(读写分离)

      转自:https://www.iyunv.com/thread-52670-1-1.html 一.redis简介Redis是一个key-value存储系统.和Memcached类似,为了保证效率,数据 ...

    3. 使用CacheCloud管理Redis实例

      转载来源:http://www.ywnds.com/?p=10610 一.CacheCloud是什么? 最近在使用CacheCloud管理Redis,所以简单说一下,这里主要说一下我碰到的问题.Cac ...

    4. RedHat使用Centos的yum仓库

      RedHat使用Centos的yum仓库 卸载红帽yum源 [root@zhouwanchun ~]# rpm -qa | grep yum [root@zhouwanchun ~]# rpm -e ...

    5. 高通9X07模块QMI架构使用入门

      QMI(Qualcomm Message Interface) 高通用来替代OneRPC/DM的协议,用来与modem通信.本文是摸索高通QMI机制一点经验,重点解读了如果建立拨号连接,仅供参考.qm ...

    6. MCC MNC in china

      A mobile country code (MCC) is used in combination with a mobile network code (MNC) (a combination k ...

    7. Hook技术之API拦截(API Hook)

      一.实现过程 1.钩子实际上是一个处理消息的程序段,通过系统调用,把它挂入系统. 2.在消息没有到达目的窗口前,钩子就捕获消息(即钩子函数先得到控制权). 3.钩子可以加工处理该消息,即钩子机制允许应 ...

    8. ZooKeeper的安装和API

      Zookeeper的分布式安装和API介绍: 安装教程 在datanode1.datanode2和datanode3三个节点上部署Zookeeper. 步骤 解压zookeeper安装包到/opt/m ...

    9. es6(16)--Decorator

      //Decorator:修饰器,是一个函数用来修改类的行为 { //只读 let readonly=function(target,name,descriptor){ descriptor.writa ...

    10. Access 分页

      access分页 pageSize 每页显示多少条数据 pageNumber 页数 从客户端传来 pages) SQL语句 select top pageSize * from 表名 where id ...