Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace template<class> with template<typename> #309

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/content/1.1/category-the-essence-of-composition.tex
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ \section{Properties of Composition}
universally polymorphic. In C++ we could define it as a template:

\begin{snip}{cpp}
template<class T> T id(T x) { return x; }
template<typename T> T id(T x) { return x; }
\end{snip}
Of course, in C++ nothing is that simple, because you have to take into
account not only what you're passing but also how (that is, by value, by
Expand Down
2 changes: 1 addition & 1 deletion src/content/1.1/code/reason/snippet01.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module type Polymorphic_Function_F = {
type a;
type b;

let f: a => b;
};
2 changes: 1 addition & 1 deletion src/content/1.1/code/reason/snippet02.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module type Polymorphic_Function_G = {
type b;
type c;

let g: b => c;
};
2 changes: 1 addition & 1 deletion src/content/1.10/code/reason/snippet09.re
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
let safe_head =
let safe_head =
(fmap([x, ...xs])) == safe_head(f([x, ... xs])) == Some(f(x));
2 changes: 1 addition & 1 deletion src/content/1.10/code/reason/snippet12.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* ReasonML requires mutually recursive functions
/* ReasonML requires mutually recursive functions
* to be defined together */
let rec length: list('a) => const(int, 'a) = (
fun
Expand Down
2 changes: 1 addition & 1 deletion src/content/1.10/code/reason/snippet17.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Reader_Functor = (T: {type e;}) : Functor => {
type t('a) = reader(T.e, 'a);

let fmap: 'a 'b. ('a => 'b, t('a)) => t('b) =
f =>
fun
Expand Down
2 changes: 1 addition & 1 deletion src/content/1.10/natural-transformations.tex
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ \section{Polymorphic Functions}
syntax. A similar construct in C++ would be slightly more verbose:

\begin{snip}{cpp}
template<class A> G<A> alpha(F<A>);
template<typename A> G<A> alpha(F<A>);
\end{snip}
There is a more profound difference between Haskell's polymorphic
functions and C++ generic functions, and it's reflected in the way these
Expand Down
2 changes: 1 addition & 1 deletion src/content/1.2/types-and-functions.tex
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ \section{Examples of Types}
In C++ you would write this function as:

\begin{snip}{cpp}
template<class T>
template<typename T>
void unit(T) {}
\end{snip}
Next in the typology of types is a two-element set. In C++ it's called
Expand Down
6 changes: 3 additions & 3 deletions src/content/1.3/categories-great-and-small.tex
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ \section{Monoid as Set}
C++20 standard's concept feature.

\begin{snip}{cpp}
template<class T>
template<typename T>
struct mempty;

template<class T>
template<typename T>
T mappend(T, T) = delete;

template<class M>
template<typename M>
concept Monoid = requires (M m) {
{ mempty<M>::value() } -> std::same_as<M>;
{ mappend(m, m) } -> std::same_as<M>;
Expand Down
2 changes: 1 addition & 1 deletion src/content/1.3/code/reason/snippet01.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module type Monoid = {
type a;

let mempty: a;
let mappend: (a, a) => a;
};
2 changes: 1 addition & 1 deletion src/content/1.3/code/reason/snippet02.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module StringMonoid: Monoid = {
type a = string;

let mempty = "";
let mappend = (++);
};
2 changes: 1 addition & 1 deletion src/content/1.4/code/reason/snippet03.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ module type Kleisli = {
type a;
type b;
type c;

let (>=>): (a => writer(b), b => writer(c), a) => writer(c);
};
28 changes: 15 additions & 13 deletions src/content/1.4/kleisli-categories.tex
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
for (auto i = begin(s); i != end(s); ++i)
{
if (isspace(*i))
result.push_back("");
result.push_back("");
else
result.back() += *i;
}
Expand All @@ -120,7 +120,7 @@
\code{A} and the second component is a string:

\begin{snip}{cpp}
template<class A>
template<typename A>
using Writer = pair<A, string>;
\end{snip}
Here are the embellished functions:
Expand All @@ -130,10 +130,10 @@
string result;
int (*toupperp)(int) = &toupper;
transform(begin(s), end(s), back_inserter(result), toupperp);
return make_pair(result, "toUpper ");
return make_pair(result, "toUpper ");
}

Writer<vector<string>> toWords(string s) {
Writer<vector<string>> toWords(string s) {
return make_pair(words(s), "toWords ");
}
\end{snip}
Expand Down Expand Up @@ -226,22 +226,22 @@ \section{The Writer Category}
return a third embellished function:

\begin{snip}{cpp}
template<class A, class B, class C>
template<typename A, typename B, typename C>
function<Writer<C>(A)> compose(function<Writer<B>(A)> m1,
function<Writer<C>(B)> m2)
{
return [m1, m2](A x) {
auto p1 = m1(x);
auto p2 = m2(p1.first);
return make_pair(p2.first, p1.second + p2.second);
return make_pair(p2.first, p1.second + p2.second);
};
}
\end{snip}
Now we can go back to our earlier example and implement the composition
of \code{toUpper} and \code{toWords} using this new template:

\begin{snip}{cpp}
Writer<vector<string>> process(string s) {
Writer<vector<string>> process(string s) {
return compose<string, string, vector<string>>(toUpper, toWords)(s);
}
\end{snip}
Expand All @@ -251,8 +251,8 @@ \section{The Writer Category}
return type deduction (credit for this code goes to Eric Niebler):

\begin{snip}{cpp}
auto const compose = [](auto m1, auto m2) {
return [m1, m2](auto x) {
auto const compose = [](auto m1, auto m2) {
return [m1, m2](auto x) {
auto p1 = m1(x);
auto p2 = m2(p1.first);
return make_pair(p2.first, p1.second + p2.second);
Expand Down Expand Up @@ -281,7 +281,8 @@ \section{The Writer Category}
string to the log:

\begin{snip}{cpp}
template<class A> Writer<A> identity(A x) {
template<typename A>
Writer<A> identity(A x) {
return make_pair(x, "");
}
\end{snip}
Expand Down Expand Up @@ -409,10 +410,11 @@ \section{Challenge}
type \code{optional}:

\begin{snip}{cpp}
template<class A> class optional {
template<typename A>
class optional {
bool _isValid;
A _value;
public:
public:
optional() : _isValid(false) {}
optional(A v) : _isValid(true), _value(v) {}
bool isValid() const { return _isValid; }
Expand All @@ -424,7 +426,7 @@ \section{Challenge}

\begin{snip}{cpp}
optional<double> safe_root(double x) {
if (x >= 0) return optional<double>{sqrt(x)};
if (x >= 0) return optional<double>{sqrt(x)};
else return optional<double>{};
}
\end{snip}
Expand Down
2 changes: 1 addition & 1 deletion src/content/1.5/code/reason/snippet09.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module type Chapter5_Product = {
type a;
type c;
type b;

let p: c => a;
let q: c => b;
};
2 changes: 1 addition & 1 deletion src/content/1.5/code/reason/snippet10.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Chapter5_Product_Example:
Chapter5_Product
Chapter5_Product
with type a = int
and type b = bool
and type c = int = {
Expand Down
2 changes: 1 addition & 1 deletion src/content/1.5/code/reason/snippet11.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Chapter5_Product_Example2: Chapter5_Product = {
type a = int;
type b = bool;
type c = (int, int, bool);

let p = ((x, _, _)) => x;
let q = ((_, _, b)) => b;
};
2 changes: 1 addition & 1 deletion src/content/1.5/code/reason/snippet21.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module type CoProduct = {
type a;
type b;
type c;

let i: a => c;
let j: b => c;
};
10 changes: 5 additions & 5 deletions src/content/1.5/products-and-coproducts.tex
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,8 @@ \section{Products}
In C++, we would use template functions, for instance:

\begin{snip}{cpp}
template<class A, class B> A
fst(pair<A, B> const & p) {
template<typename A, typename B>
A fst(pair<A, B> const & p) {
return p.first;
}
\end{snip}
Expand Down Expand Up @@ -445,7 +445,7 @@ \section{Coproduct}
\code{char const *} could be implemented as:

\begin{snip}{cpp}
struct Contact {
struct Contact {
enum { isPhone, isEmail } tag;
union { int phoneNum; char const * emailAddr; };
};
Expand All @@ -455,7 +455,7 @@ \section{Coproduct}
\code{PhoneNum}:

\begin{snip}{cpp}
Contact PhoneNum(int n) {
Contact PhoneNum(int n) {
Contact c;
c.tag = isPhone;
c.phoneNum = n;
Expand Down Expand Up @@ -623,7 +623,7 @@ \section{Challenges}
Still continuing: What about these injections?

\begin{snip}{cpp}
int i(int n) {
int i(int n) {
if (n < 0) return n;
return n + 2;
}
Expand Down
4 changes: 2 additions & 2 deletions src/content/1.6/code/haskell/snippet15.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
data Element = Element { name :: String
, symbol :: String
data Element = Element { name :: String
, symbol :: String
, atomicNumber :: Int }
4 changes: 2 additions & 2 deletions src/content/1.6/code/haskell/snippet16.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
tupleToElem :: (String, String, Int) -> Element
tupleToElem (n, s, a) = Element { name = n
, symbol = s
tupleToElem (n, s, a) = Element { name = n
, symbol = s
, atomicNumber = a }
2 changes: 1 addition & 1 deletion src/content/1.6/code/reason/snippet31.re
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
type maybe('a) =
| Nothing
| Just('a);

let maybe_tail =
fun
| Nil => Nothing
Expand Down
6 changes: 3 additions & 3 deletions src/content/1.6/simple-algebraic-data-types.tex
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ \section{Sum Types}
empty list:

\begin{snip}{cpp}
template<class A>
class List {
template<typename A>
class List {
Node<A> * _head;
public:
List() : _head(nullptr) {} // Nil
Expand Down Expand Up @@ -472,7 +472,7 @@ \section{Challenges}
Here's a sum type defined in Haskell:

\begin{snip}{haskell}
data Shape = Circle Float
data Shape = Circle Float
| Rect Float Float
\end{snip}
When we want to define a function like \code{area} that acts on a
Expand Down
2 changes: 1 addition & 1 deletion src/content/1.7/code/reason/snippet03.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module type AtoB = {
type a;
type b;

let f: a => b;
};
2 changes: 1 addition & 1 deletion src/content/1.7/code/reason/snippet05.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module type Maybe_Functor = {
type a;
type b;

let fmap: (a => b, option(a)) => option(b);
};
2 changes: 1 addition & 1 deletion src/content/1.7/code/reason/snippet06.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module type Maybe_Functor = {
type a;
type b;

let fmap: (a => b, option(a)) => option(b);
};
2 changes: 1 addition & 1 deletion src/content/1.7/code/reason/snippet09.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Test_Functor_Id = (F: Functor) => {
open F;

let test_id = x => assert(fmap(id, x) == x);
};
2 changes: 1 addition & 1 deletion src/content/1.7/code/reason/snippet10.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Test_Functor_Compose = (F: Functor) => {

/* Compose */
let (<.>) = (f, g, x) => f(g(x));

let test_compose = (f, g, x) =>
assert(fmap(f <.> g, x) == fmap(f, fmap(g, x)));
};
2 changes: 1 addition & 1 deletion src/content/1.7/code/reason/snippet11.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module type Eq = {
type a;

let (===): (a, a) => bool;
};
2 changes: 1 addition & 1 deletion src/content/1.7/code/reason/snippet13.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Point_Eq = (E: Eq with type a = float) => {
type a = point;

let (===) = (Pt(p1x, p1y), Pt(p2x, p2y)) =>
E.(p1x === p2x) && E.(p2x === p2y);
};
2 changes: 1 addition & 1 deletion src/content/1.7/code/reason/snippet14.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module type Functor = {
type t('a);

let fmap: ('a => 'b, t('a)) => t('b);
};
2 changes: 1 addition & 1 deletion src/content/1.7/code/reason/snippet15.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Option_Functor: Functor with type t('a) = option('a) = {
type t('a) = option('a);

let fmap = f =>
fun
| None => None
Expand Down
2 changes: 1 addition & 1 deletion src/content/1.7/code/reason/snippet17.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module type List_Functor_Type = {
type t('a) = list('a);

let fmap: ('a => 'b, list('a)) => list('b);
};
Loading