Haskell语言学习笔记(32)Data.Maybe, Data.Either
Data.Maybe
Prelude Data.Maybe> maybe "" show (Just 5)
"5"
Prelude Data.Maybe> maybe "" show Nothing
""
Prelude Data.Maybe> isJust (Just 3)
True
Prelude Data.Maybe> isJust Nothing
False
Prelude Data.Maybe> isNothing (Just 3)
False
Prelude Data.Maybe> isNothing Nothing
True
Prelude Data.Maybe> fromJust (Just 1)
1
Prelude Data.Maybe> 2 * (fromJust Nothing)
*** Exception: Maybe.fromJust: Nothing
Prelude Data.Maybe> fromMaybe "" (Just "Hello, World!")
"Hello, World!"
Prelude Data.Maybe> fromMaybe "" Nothing
""
Prelude Data.Maybe> listToMaybe []
Nothing
Prelude Data.Maybe> listToMaybe [1,2,3]
Just 1
Prelude Data.Maybe> maybeToList (Just 7)
[7]
Prelude Data.Maybe> maybeToList Nothing
[]
Prelude Data.Maybe> catMaybes [Just 1, Nothing, Just 3]
[1,3]
Prelude Data.Maybe> let readMaybeInt = Text.Read.readMaybe :: String -> Maybe Int
Prelude Data.Maybe> mapMaybe readMaybeInt ["1", "Foo", "3"]
[1,3]
Data.Either
Prelude Data.Either> either length (*2) $ Left "foo"
3
Prelude Data.Either> either length (*2) $ Right 3
6
Prelude Data.Either> lefts [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]
["foo","bar","baz"]
Prelude Data.Either> rights [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]
[3,7]
Prelude Data.Either> isLeft (Left "foo")
True
Prelude Data.Either> isLeft (Right 3)
False
Prelude Data.Either> isRight (Left "foo")
False
Prelude Data.Either> isRight (Right 3)
True
Prelude Data.Either> fromLeft 1 (Left 3)
3
Prelude Data.Either> fromLeft 1 (Right "foo")
1
Prelude Data.Either> fromRight 1 (Right 3)
3
Prelude Data.Either> fromRight 1 (Left "foo")
1
Prelude Data.Either> partitionEithers [ Left "foo", Right 3, Left "bar", Right 7, Left "baz" ]
(["foo","bar","baz"],[3,7])
Haskell语言学习笔记(32)Data.Maybe, Data.Either的更多相关文章
- Haskell语言学习笔记(93)Data.Text
Data.Text.Read Prelude> :set -XOverloadedStrings Prelude> :m +Data.Text.Read Prelude Data.Text ...
- Haskell语言学习笔记(76)Data.Tree
Data.Tree data Tree a = Node { rootLabel :: a, subForest :: Forest a } deriving (Eq, Read, Show) typ ...
- 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语言学习笔记(81)Data.Typeable
Data.Typeable 利用 Data.Typeable,可以打印动态类型信息. class Typeable (a :: k) where typeRep# :: TypeRep a typeR ...
- Haskell语言学习笔记(77)Data.HashSet
安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...
- Haskell语言学习笔记(65)Data.HashMap
安装 unordered-containers $ cabal install unordered-containers Installed unordered-containers-0.2.9.0 ...
- Haskell语言学习笔记(55)Data.Vector
Data.Vector Construction Prelude V> import Data.Vector as V Prelude V> V.empty [] Prelude V> ...
- Haskell语言学习笔记(54)Data.Set
Data.Set Prelude> import Data.Set as Set Prelude Set> :set -XOverloadedLists Construction Prel ...
- Haskell语言学习笔记(53)Data.Sequence
Data.Sequence Prelude> import Data.Sequence as Seq Prelude Seq> :set -XOverloadedLists Prelude ...
随机推荐
- C# 中取绝对值的函数
System.Math.Abs(float value); System.Math.Abs(decimal value); System.Math.Abs(int value); System.Mat ...
- Maven 项目报告插件
Maven 项目报告插件,都是对于前面生成的项目站点的内容丰富,因此都是基于项目站点的,生成的命令和生成项目站点一致(mvn site),项目报告插件的配置和一般插件不同,是在 project-> ...
- 【python】class之类的定义
使用class定义类,可以提供一个可选的父类或者基类,如果没有合适的基类,那就使用object作为基类,也可以不写.class FooClass (object)或者class FooClass: v ...
- 【python】实例-把两个无规则的序列连接成一个序列,并删除重复的元素,新序列按照升序排序
list_one=[3,6,2,17,7,33,11,7] list_two=[1,2,3,7,4,2,17,33,11] list_new=list_one+list_two list=[] i=0 ...
- WCF揭秘学习笔记(4):可信赖会话、会话管理、队列、事务
可信赖会话 WCF的可信赖会话在绑定层保证消息只会被传输一次,并且保证消息间的顺序.当使用TCP通信时,协议本身保证了可靠性,但它只在两点间的网络 包这个层面提供了这样的保证.WCF的可信赖会话特性保 ...
- HttpClient连接池
HttpClient连接池,发现对于高并发的请求,效率提升很大.虽然知道是因为建立了长连接,导致请求效率提升,但是对于内部的原理还是不太清楚.后来在网上看到了HTTP协议的发展史,里面提到了一个属性C ...
- Android应用程序如何调用shell脚本(一)
转自: Android应用程序如何调用shell脚本(一) 一般来说, Android 下的应用程序可以“直接”得到的最大的权限为 system ,但是如果我们需要在程序中执行某些需要 root 权限 ...
- Network Emulator Toolkit (NEWT) 网络限速工具 (手机和电脑方面)
下载地址: https://blog.mrpol.nl/2010/01/14/network-emulator-toolkit/ 参考博客: http://blog.csdn.net/lluozh20 ...
- Web API 源码剖析之全局配置
Web API 源码剖析之全局配置 Web API 均指Asp.net Web API .本节讲述的是基于Web API 系统在寄宿于IIS. 本节主要讲述Web API全局配置.它是如何优雅的实现 ...
- sqlserver操作命令
启动命令:Net Start MSSqlServer 暂停命令:Net Pause MSSqlServer 重新启动暂停的命令:Net Continue MSSqlServer 停止命令:Net st ...