-
Notifications
You must be signed in to change notification settings - Fork 3
/
typeclasses.tex
191 lines (145 loc) · 7.33 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
\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]{https://typelevel.org/cats/api/cats/#1.html}
\newcommand{\sdocHref}[1]{\href{\sdocUrl{#1}}{#1}}
\title{cats Typeclass Cheat Sheet}
\author[Adam Rosien]{Adam Rosien (\href{mailto:adam@rosien.net}{adam@rosien.net})}
% 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.typelevel" \%\% "cats-core" \% "1.0.0-MF"
\end{quote}
% \end{fullwidth}
\noindent Then in your \texttt{.scala} files:
\begin{quote}
\ttfamily import cats.\_
\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{FlatMap}\footnotemark & \fThree{\fa}{(A => F[B])}{\fb} \\
\sdocHref{CoFlatMap} & \fThree{\fa}{(F[A] => B)}{\fb} \\
\sdocHref{Traverse}\footnotemark & \fThree{\fa}{(A => G[B])}{G[F[B]]} \\
\sdocHref{Foldable} & \fThree{\fa}{(B, (B, A) => B)}{B} \\
\sdocHref{SemigroupK} & \fThree{\fa}{\fa}{\fa} \\
\sdocHref{Cartesian} & \fThree{\fa}{\fb}{F[(A, 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.}
\footnotetext[3]{\texttt{FlatMap} has a (broader) subtype \texttt{Monad}.}
% hrm need manual numbering here
\footnotetext[4]{\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]]}.}
\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{6}{*}{\sdocHref{Functor}}
& \fThree{\multirow{6}{*}{\fa}}{(A => B)}{\fb} & \textbf{map} \\
& \fThree{}{(A => B)}{F[(A, B)]} & fproduct \\
& \fThree{}{B}{\fb} & as \\
& \fThree{}{B}{F[(B, A)]} & tupleLeft \\
& \fThree{}{B}{F[(A, B)]} & tupleRight \\
& \fTwo{}{F[Unit]} & void \\[.5cm]
\multirow{1}{*}{\sdocHref{Contravariant}}
& \fThree{\fa}{(B => A)}{\fb} & \textbf{contramap} \\[.5cm]
\multirow{2}{*}{\sdocHref{Apply}\footnotemark}
& \fThree{\multirow{2}{*}{\fa}}{F[A => B]}{\fb} & \textbf{ap} \\
& \fThree{}{F[B] => ((A, B) => C)}{F[C]} & \texttt{map2} \\[.5cm]
\multirow{4}{*}{\sdocHref{Applicative}}
& \fThree{\multirow{4}{*}{\fa}}{F[A => B]}{\fb} & \textbf{ap} \\
& \fThree{}{Boolean}{F[Unit]} & unlessA \\
& \fThree{}{Boolean}{F[Unit]} & whenA \\
& \fThree{}{Int}{F[List[A]]} & replicateA \\[.5cm]
\multirow{5}{*}{\sdocHref{FlatMap}}
& \fThree{\multirow{4}{*}{\fa}}{(A => F[B])}{\fb} & \textbf{flatMap} \\
& \fThree{}{\fb}{\fb} & followedBy \\
& \fThree{}{\fb}{\fa} & forEffect \\
& \fThree{}{(A => \fb)}{F[(A, B)]} & mproduct \\
& \fThree{F[F[A]]}{}{\fa} & flatten \\[.5cm]
\multirow{2}{*}{\sdocHref{CoFlatMap}}
& \fThree{\multirow{2}{*}{\fa}}{(\fa => B)}{\fb} & \textbf{coflatMap} \\
& \fThree{}{}{F[F[A]]} & coflatten \\[.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{pure} method to lift a value into the \texttt{Applicative}.}
\begin{table}[ht]
\centering
\fontfamily{ppl}\selectfont
\begin{tabular}{rrcclll}
Typeclass & \multicolumn{5}{c}{Signature} & Function \\
\midrule
\multirow{4}{*}{\sdocHref{Traverse}}
& \fThree{\multirow{3}{*}{\fa}}{(A => G[B])}{G[F[B]]} & \textbf{traverse} \\
& \fThree{}{((A, Int) => B)}{\fb} & mapWithIndex \\
& \fThree{}{}{F[(A, Int)]} & zipWithIndex \\
& \fTwo{F[G[A]]}{G[F[A]]} & sequence \\[.5cm]
\multirow{11}{*}{\sdocHref{Foldable}}
& \fThree{\multirow{11}{*}{\fa}}{B => ((B, A) => B)}{B} & \textbf{foldLeft} \\
& \fThree{}{Eval[B] => ((A, Eval[B]) => Eval[B])}{Eval[B]} & \textbf{foldRight} \\
& \fThree{}{(A => B)}{B} & foldMap\footnotemark \\
& \fTwo{}{A} & combineAll\footnotemark \\
& \fThree{}{(A => Boolean)}{Option[A]} & find \\
& \fThree{}{(A => Boolean)}{Boolean} & exists \\
& \fThree{}{(A => Boolean)}{Boolean} & forall \\
& \fTwo{}{List[A]} & toList \\
& \fTwo{}{Boolean} & isEmpty \\
& \fTwo{}{Boolean} & nonEmpty \\
& \fTwo{}{Int} & size \\[.5cm]
\sdocHref{SemigroupK}
& \fThree{\fa}{\fa}{\fa} & \textbf{combine} \\[.5cm]
\sdocHref{Cartesian}
& \fThree{\fa}{\fb}{F[(A, B)]} & \textbf{product} \\[.5cm]
\end{tabular}
\end{table}
\footnotetext[6]{If \texttt{B} has a \texttt{Monoid}}
\footnotetext[7]{If \texttt{A} has a \texttt{Monoid}}
\bibliography{sample-handout}
\bibliographystyle{plainnat}
\fancyfoot[C]{\copyright 2017 Adam S. Rosien (\href{mailto:adam@rosien.net}{\texttt{adam@rosien.net}}) \\
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/cats-cheatsheets}{\texttt{https://github.com/arosien/cats-cheatsheets}}}
\end{document}