-
Notifications
You must be signed in to change notification settings - Fork 4
/
typeclasses.tex
203 lines (170 loc) · 9.24 KB
/
typeclasses.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
\documentclass{tufte-handout}
%\geometry{showframe}% for debugging purposes -- displays the margins
\usepackage{amsmath}
% Set up the images/graphics package
\usepackage{graphicx}
\setkeys{Gin}{width=\linewidth,totalheight=\textheight,keepaspectratio}
\graphicspath{{graphics/}}
% The following package makes prettier tables. We're all about the bling!
\usepackage{booktabs}
% The units package provides nice, non-stacked fractions and better spacing
% for units.
\usepackage{units}
% The fancyvrb package lets us customize the formatting of verbatim
% environments. We use a slightly smaller font.
\usepackage{fancyvrb}
\fvset{fontsize=\normalsize}
% Small sections of multiple columns
\usepackage{multicol}
\usepackage{multirow}
\newcommand{\fa}{F[A]}
\newcommand{\fb}{F[B]}
\newcommand{\rarr}{\texttt{=>}}
\newcommand{\fTwo}[2]{\texttt{#1} & \rarr & & & \texttt{#2}}
\newcommand{\fThree}[3]{\texttt{#1} & \rarr & \texttt{#2} & \rarr & \texttt{#3}}
\newcommand{\sdocUrl}[1]{http://docs.typelevel.org/api/scalaz/stable/7.0.4/doc/\#scalaz.#1}
\newcommand{\sdocHref}[1]{\href{\sdocUrl{#1}}{#1}}
\title{scalaz Typeclass Cheat Sheet}
\author[Adam Rosien]{Adam Rosien (\href{mailto:[email protected]}{[email protected]})}
% if the \date{} command is left out, the current date will be used
% \date{2 December 2014}
\begin{document}
\maketitle% this prints the handout title, author, and date
\section{Installation}\label{sec:installation}
\noindent In your \texttt{build.sbt} file:
% \begin{fullwidth}
\begin{quote}
\ttfamily libraryDependencies += "org.scalaz" \%\% "scalaz-core" \% "7.0.4"
\end{quote}
% \end{fullwidth}
\noindent Then in your \texttt{.scala} files:
\begin{quote}
\ttfamily import scalaz.\_
\end{quote}
\section{Defining Signatures}
Each typeclass is defined by a particular function signature and a set of laws\footnotemark (invariants) that the typeclass must obey.
\footnotetext{Typeclass laws are not listed here. See each typeclass' scaladoc link for more information.}
\begin{table}[ht]
\centering
\fontfamily{ppl}\selectfont
\setlength{\tabcolsep}{5pt}
\begin{tabular}{rcrcccl}
Typeclass & \multicolumn{5}{c}{Signature} \\
\midrule
\sdocHref{Functor} & \fThree{\fa}{(A => B)}{\fb} \\
\sdocHref{Contravariant} & \fThree{\fa}{(B => A)}{\fb} \\
\sdocHref{Apply}\footnotemark & \fThree{\fa}{F[A => B]}{\fb} \\
\sdocHref{Bind} & \fThree{\fa}{(A => F[B])}{\fb} \\
\sdocHref{Traverse}\footnotemark & \fThree{\fa}{(A => G[B])}{G[F[B]]} \\
\sdocHref{Foldable}\footnotemark & \fThree{\fa}{(A => B)}{B} \\
\sdocHref{Plus} & \fThree{\fa}{\fa}{\fa} \\
\sdocHref{Cobind} & \fThree{\fa}{(F[A] => B)}{\fb} \\
\sdocHref{Zip} & \fThree{\fa}{\fb}{F[(A, B)]} \\
\sdocHref{Unzip} & \fTwo{F[(A, B)]}{(F[A], F[B])} \\
\end{tabular}
\label{tab:normaltab}
\end{table}
% hrm need manual numbering here
\footnotetext[2]{\texttt{Apply} has a (broader) subtype \texttt{Applicative}. See the expanded tables below.}
% hrm need manual numbering here
\footnotetext[3]{\texttt{Traverse} requires that the target type constructor \texttt{G} have an implicit \texttt{Applicative} instance available; that is, an implicit \texttt{Applicative[G]} must be in scope. \\ Informally, traversing a structure maps each value to some effect, which are combined into a single effect that produces a value having the original structure. For example, by transforming every \texttt{A} of a \texttt{List[A]} into a \texttt{Future[B]}, the traversal would return a \texttt{Future[List[B]]}.}
\footnotetext{\texttt{Foldable} requires that the target type \texttt{B} have an implicit \texttt{Monoid} instance available; that is, an implicit \texttt{Monoid[B]} must be in scope. \\ Informally, you're folding something up, so you need to know how to squash things together!}
\newpage
\section{Derived Functions}
For each typeclass, its defining function is marked in \textbf{bold} and each derived function listed below it.
\begin{table}[ht]
\centering
\fontfamily{ppl}\selectfont
\begin{tabular}{rrcclll}
Typeclass & \multicolumn{5}{c}{Signature} & Function \\
\midrule
\multirow{8}{*}{\sdocHref{Functor}}
& \fThree{\multirow{8}{*}{\fa}}{(A => B)}{\fb} & \textbf{map} \\
& \fThree{}{B}{\fb} & as \\
& \fTwo{}{F[(A, A)]} & fpair \\
& \fThree{}{G[\_]}{F[G[A]]} & fpoint \\
& \fThree{}{(A => B)}{F[(A, B)]} & fproduct \\
& \fThree{}{B}{F[(B, A)]} & strengthL \\
& \fThree{}{B}{F[(A, B)]} & strengthR \\
& \fTwo{}{F[Unit]} & void \\[.5cm]
\sdocHref{Contravariant} & \fThree{\fa}{(B => A)}{\fb} & \textbf{contramap} \\[.5cm]
\multirow{5}{*}{\sdocHref{Apply}\footnotemark}
& \fThree{\multirow{5}{*}{\fa}}{F[A => B]}{\fb} & \textbf{ap} \\
& \fThree{}{\fb}{F[(A,B)]} & tuple \\
& \fThree{}{\fb}{\fb} & \verb$*>$ \\
& \fThree{}{\fb}{\fa} & \verb$<*$ \\
& \fThree{}{F[B] => ((A, B) => C)}{F[C]} & \texttt{apply2}\footnotemark \\[.5cm]
% & \fThree{}{F[B] => F[C] => ((A, B, C) => D)}{F[D]} & apply3 \\[.5cm]
\multirow{5}{*}{\sdocHref{Applicative}}
& \fThree{\multirow{5}{*}{\fa}}{F[A => B]}{\fb} & \textbf{ap} \\
& \fThree{}{Boolean}{F[Unit]} & unlessM \\
& \fThree{}{Boolean}{F[Unit]} & whenM \\
& \fThree{}{Int}{F[List[A]]} & replicateM \\
& \fThree{}{Int}{F[Unit]} & replicateM\_ \\[.5cm]
\multirow{3}{*}{\sdocHref{Bind}}
& \fThree{\multirow{2}{*}{\fa}}{(A => F[B])}{\fb} & \textbf{flatMap} \\
& \fThree{}{\fb}{\fb} & \verb$>>$ \\
& \fThree{F[F[A]]}{}{\fa} & join \\[.5cm]
\end{tabular}
\end{table}
\footnotetext[5]{Both the \texttt{Apply} and \texttt{Applicative} typeclasses implement the \texttt{ap} method; \texttt{Applicative} is a subtype of \texttt{Apply}, with an additional \texttt{point} method to lift a value into the \texttt{Applicative}.}
\footnotetext{In addition to \texttt{apply2}, there is \texttt{apply3}, etc., up to \texttt{apply12}. That is, \texttt{applyN} takes N \texttt{F}'s and a function that tranforms an N-tuple into a single value.}
\begin{table}[ht]
\centering
\fontfamily{ppl}\selectfont
\begin{tabular}{rrcclll}
Typeclass & \multicolumn{5}{c}{Signature} & Function \\
\midrule
\multirow{6}{*}{\sdocHref{Traverse}}
& \fThree{\multirow{5}{*}{\fa}}{(A => G[B])}{G[F[B]]} & \textbf{traverse} \\
& \fThree{}{(A => G[F[B]])}{G[F[B]]} & traverseM \\
& \fTwo{}{\fa} & reverse \\
& \fThree{}{\fb}{F[(A, Option[B])]} & zipL \\
& \fThree{}{\fb}{F[(Option[A], B)]} & zipR \\
% & & \texttt{& F[B] => ((A, Option[B]) => C) => (List[B], F[C])} & zipWith \\
% & & \texttt{& F[B] => ((A, Option[B]) => C) => F[C]} & zipWithL \\
% & & \texttt{& F[B] => ((Option[A], B) => C) => F[C]} & zipWithR \\
% & & \texttt{& S => ((S,A) => (S,B)) => (S, F[B])} & mapAccumL \\
% & & \texttt{& S => ((S,A) => (S,B)) => (S, F[B])} & mapAccumR \\
% & & \texttt{& S => (A => State[S, B]) => (S, F[B])} & runTraverseS \\
% & & \texttt{& (A => State[S, B]) => State[S, F[B]]} & traverseS \\
% & & \texttt{& (A => State[S, G[B]]) => State[S, G[F[B]]]} & traverseSTrampoline \\
% & & \texttt{& (A => Kleisli[G, S, B]) => Kleisli[G, S, F[B]]} & traverseKTrampoline \\
& \fTwo{F[G[A]]}{G[F[A]]} & sequence \\[.5cm]
\multirow{16}{*}{\sdocHref{Foldable}}
& \fThree{\multirow{16}{*}{\fa}}{(A => B)}{B} & \textbf{foldMap} \\
& \fThree{}{B => ((A, B) => B)}{B} & foldRight \\
& \fThree{}{B => ((B, A) => B)}{B} & foldLeft \\
& \fTwo{}{A} & fold \\
& \fTwo{}{Int} & length \\
& \fThree{}{Int}{Option[A]} & index \\
& \fThree{}{(A, Int)}{A} & indexOr \\
& \fTwo{}{A} & suml \\
& \fTwo{}{A} & sumr \\
& \fTwo{}{List[A]} & toList \\
& \fTwo{}{Set[A]} & toSet \\
& \fTwo{}{Stream[A]} & toStream \\
& \fThree{}{(A => Boolean)}{Boolean} & all \\
& \fThree{}{(A => Boolean)}{Boolean} & any \\
& \fTwo{}{Boolean} & empty \\[.5cm]
\sdocHref{Plus}
& \fThree{\fa}{\fa}{\fa} & \textbf{plus} \\[.5cm]
\multirow{2}{*}{\sdocHref{Cobind}}
& \fThree{\multirow{2}{*}{\fa}}{(F[A] => B)}{\fb} & \textbf{cobind} \\
& \fTwo{}{F[\fa]} & cojoin \\[.5cm]
\multirow{3}{*}{\sdocHref{Zip}}
& \fThree{\multirow{3}{*}{\fa}}{\fb}{F[(A, B)]} & \textbf{zip} \\
& \fThree{}{F[B] => ((A, B) => C)}{F[C]} & zipWith \\
& \fThree{}{(F[A] => F[B])}{F[(A, B)]} & apzip \\[.5cm]
\multirow{3}{*}{\sdocHref{Unzip}}
& \fTwo{\multirow{3}{*}{F[(A, B)]}}{(\fa, \fb)} & \textbf{unzip} \\
& \fTwo{}{\fa} & firsts \\
& \fTwo{}{\fb} & seconds \\[.5cm]
\end{tabular}
\end{table}
\bibliography{sample-handout}
\bibliographystyle{plainnat}
\fancyfoot[C]{\copyright 2015 Adam S. Rosien (\href{mailto:[email protected]}{\texttt{[email protected]}}) \\
This work is licensed under a \href{http://creativecommons.org/licenses/by/4.0/}{Creative Commons Attribution 4.0 International License}. \\
Issues and suggestions welcome at \href{https://github.com/arosien/scalaz-cheatsheets}{\texttt{https://github.com/arosien/scalaz-cheatsheets}}}
\end{document}