Skip to content

Commit

Permalink
Obey horizontal space restrictions in Reason snippets.
Browse files Browse the repository at this point in the history
  • Loading branch information
fhammerschmidt committed Mar 30, 2020
1 parent ba86c7b commit 84cf2cc
Show file tree
Hide file tree
Showing 49 changed files with 123 additions and 125 deletions.
2 changes: 1 addition & 1 deletion src/content/1.1/code/reason/snippet03.re
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Compose_Example =
F: Polymorphic_Function_F,
G: Polymorphic_Function_G with type b = F.b,
) => {
/** ReasonML/OCaml doesn't have a compose operator. So, creating one. **/
/** ReasonML doesn't have a compose operator. So, creating one. **/
let (>>) = (g, f, x) => g(f(x));

let compose: 'a => 'c = (G.g >> F.f: 'a => 'c);
Expand Down
3 changes: 2 additions & 1 deletion src/content/1.10/code/reason/snippet09.re
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
let safe_head = (fmap([x, ...xs])) == safe_head(f([x, ... xs])) == Some(f(x));
let safe_head =
(fmap([x, ...xs])) == safe_head(f([x, ... xs])) == Some(f(x));
3 changes: 2 additions & 1 deletion src/content/1.10/code/reason/snippet12.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* ReasonML/OCaml requires mutually recursive functions to be defined together */
/* ReasonML requires mutually recursive functions
* to be defined together */
let rec length: list('a) => const(int, 'a) = (
fun
| [] => Const(0)
Expand Down
5 changes: 4 additions & 1 deletion src/content/1.5/code/reason/snippet10.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
module Chapter5_Product_Example:
Chapter5_Product with type a = int and type b = bool and type c = int = {
Chapter5_Product
with type a = int
and type b = bool
and type c = int = {
type a = int;
type b = bool;
type c = int;
Expand Down
2 changes: 1 addition & 1 deletion src/content/1.6/code/reason/snippet20.re
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// ReasonML/OCaml only allows special characters in the infix operator.
// ReasonML only allows special characters in the infix operator.
// So, the above function name cannot be applied as infix.
3 changes: 2 additions & 1 deletion src/content/1.7/code/reason/snippet28.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module Const_Functor = (T: T) : Functor => {
type t('a) = const(T.t, 'a);

let fmap = (f, Const(c)) => Const(c); /* or even let fmap = (_ c) => c */
// or even let fmap = (_, c) => c;
let fmap = (f, Const(c)) => Const(c);
};
3 changes: 2 additions & 1 deletion src/content/1.8/code/reason/snippet01.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/** You can represent bifunctor defintion in two forms and implement just and derive the other from it. */
/** You can represent bifunctor defintion in two forms
* and implement just and derive the other from it. */
module type BifunctorCore = {
type t('a, 'b);

Expand Down
3 changes: 2 additions & 1 deletion src/content/1.8/code/reason/snippet03.re
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
let bimap: ('a => 'c, 'b => 'd, Bifunctor_Product.t('a, 'b)) => Bifunctor_Product.t('c, 'd)
let bimap: ('a => 'c, 'b => 'd, Bifunctor_Product.t('a, 'b)) =>
Bifunctor_Product.t('c, 'd)
4 changes: 2 additions & 2 deletions src/content/1.8/code/reason/snippet08.re
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* ReasonML/OCaml doesn't have a built in Const type */
/* ReasonML doesn't have a built in Const type */
type const('a, 'b) =
| Const('a);

/* ReasonML/OCaml doesn't have a built in either type */
/* ReasonML doesn't have a built in either type */
type either('a, 'b) =
| Left('a)
| Right('b);
Expand Down
5 changes: 4 additions & 1 deletion src/content/1.8/code/reason/snippet09.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/** ReasonML/OCaml doesn't support higher kinded types. So, we have to use module functors to emulate the behavior higher kinded types. There's less verbose options using type defunctionalization but it's more advanced and obscures the flow of this book */
/** ReasonML doesn't support higher kinded types.
* So, we have to use module functors to emulate the behavior higher kinded types.
* There's less verbose options using type defunctionalization
* but it's more advanced and obscures the flow of this book */
module type BiComp =
(
BF: {type t('a, 'b);},
Expand Down
3 changes: 2 additions & 1 deletion src/content/1.8/code/reason/snippet13.re
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
let bimap: (FU.t(a) => FU.t(a')) => (GU.t(b) => GU.t(b')) => (FU.t(a), GU.t(b)) => (FU.t(a'), GU.t(b'))
let bimap: (FU.t(a) => FU.t(a')) => (GU.t(b) => GU.t(b')) =>
(FU.t(a), GU.t(b)) => (FU.t(a'), GU.t(b'))
4 changes: 3 additions & 1 deletion src/content/1.8/code/reason/snippet14.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/** Deriving a functor in ReasonML/OCaml is not available as a language extension. You could try experimental library like ocsigen to derive functors.*/
/** Deriving a functor in ReasonML is not available as a
* language extension. You could try experimental library
* like ocsigen to derive functors.*/
type tree('a) =
| Leaf('a)
| Node(tree('a), tree('a));
2 changes: 1 addition & 1 deletion src/content/1.9/code/reason/snippet11.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ module Exp_Sum_Impl: Exponential_Of_Sums_Example = {
let f =
fun
| Left(n) => n < 0 ? "Negative int" : "Positive int"
| Right(x) => Float.compare(x, 0.4) < 0 ? "Negative double" : "Positive double"
| Right(x) => x < 0.0 ? "Negative double" : "Positive double"
};
3 changes: 2 additions & 1 deletion src/content/2.2/code/reason/snippet02.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
let contramap: ('c_prime => 'c, 'c => 'limD, 'c_prime) => 'limD = (
(f, u) => compose(u, f): ('c_prime => 'c, 'c => 'limD, 'c_prime) => 'limD
(f, u) => compose(u, f):
('c_prime => 'c, 'c => 'limD, 'c_prime) => 'limD
);
2 changes: 1 addition & 1 deletion src/content/2.2/code/reason/snippet06.re
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/* Pseudo ReasonML/OCaml expressing function equality */
/* Pseudo ReasonML expressing function equality */
compose(f, p) == compose(g, p);
2 changes: 1 addition & 1 deletion src/content/2.2/code/reason/snippet09.re
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/* Pseudo ReasonML/OCaml expressing function equality */
/* Pseudo ReasonML expressing function equality */
compose(f, p') == compose(g, p');
4 changes: 2 additions & 2 deletions src/content/2.3/code/reason/snippet02.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module ListMonoid = (T1: {type a;}):
(Monoid with type m = list(T1.a)) => {
module ListMonoid = (T1: {type a;}) :
(Monoid with type m = list(T1.a)) => {
type m = list(T1.a);

let mempty = [];
Expand Down
1 change: 0 additions & 1 deletion src/content/3.10/code/reason/snippet04.re
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/* There is no compose operator in ReasonML */

compose(dimap(id, f), alpha) == compose(dimap(f, id), alpha);
4 changes: 2 additions & 2 deletions src/content/3.10/code/reason/snippet09.re
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module EndsEqualizer = (P: Profunctor) => {
let lambda: (P.p('a, 'a), 'a => 'b) => P.p('a, 'b) = (
(paa, f) => P.dimap(id, f, paa): (P.p('a, 'a), 'a => 'b) => P.p('a, 'b)
(paa, f) => P.dimap(id, f, paa)
);

let rho: (P.p('b, 'b), 'a => 'b) => P.p('a, 'b) = (
(pbb, f) => P.dimap(f, id, pbb): (P.p('b, 'b), 'a => 'b) => P.p('a, 'b)
(pbb, f) => P.dimap(f, id, pbb)
);
};
15 changes: 7 additions & 8 deletions src/content/3.10/code/reason/snippet12.re
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
module EndsWithDiaProd =
(
P: Profunctor,
D: DiaProd with type p('a, 'b) = P.p('a, 'b),
PP: ProdP with type p('a, 'b) = P.p('a, 'b),
) => {
module EndsWithDiaProd = (
P: Profunctor,
D: DiaProd with type p('a, 'b) = P.p('a, 'b),
PP: ProdP with type p('a, 'b) = P.p('a, 'b),
) => {
module E = EndsEqualizer(P);

let lambdaP: D.diaprod('a) => PP.prod_p('a, 'b) = (
(DiaProd(paa)) => E.lambda(paa): D.diaprod('a) => PP.prod_p('a, 'b)
(DiaProd(paa)) => E.lambda(paa)
);

let rhoP: D.diaprod('b) => PP.prod_p('a, 'b) = (
(DiaProd(pbb)) => E.rho(pbb): D.diaprod('b) => PP.prod_p('a, 'b)
(DiaProd(pbb)) => E.rho(pbb)
);
};
9 changes: 4 additions & 5 deletions src/content/3.10/code/reason/snippet16.re
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,19 @@ module CoEndImpl = (P: Profunctor) => {
type b;

module type Sum_P =
SumP with type p('a, 'b) = P.p('a, 'b) and type a = a and type b = b;
SumP with type p('a, 'b) = P.p('a, 'b)
and type a = a and type b = b;

let lambda = (module S: Sum_P): (module DiagSum) =>
(module
{
(module {
type a = S.b;
type p('a, 'b) = P.p('a, 'b);

let paa = P.dimap(S.f, id, S.pab);
});

let rho = (module S: Sum_P): (module DiagSum) =>
(module
{
(module {
type a = S.a;
type p('a, 'b) = P.p('a, 'b);

Expand Down
29 changes: 12 additions & 17 deletions src/content/3.11/code/reason/snippet05.re
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@ let fold_map = (type i, module M: Monoid with type m = i, xs, f) =>
List.fold_left((acc, a) => M.mappend(acc, f(a)), M.mempty, xs);

let to_lst = (type x, xs: list(x)) => {
module LM: Monoid with type m = list(x) =
ListMonoid({
type a = x;
});
module LM: Monoid with type m = list(x) =
ListMonoid({type a = x;});

(
(module
{
type a = x;
type m = list(x);
(module {
type a = x;
type m = list(x);

module M = LM;
module M = LM;

type lst = (a => LM.m) => LM.m;
type lst = (a => LM.m) => LM.m;

let f = g => fold_map((module LM), xs, g);
}): (module Lst with type a = x)
);
let f = g => fold_map((module LM), xs, g);
});
};

let from_lst =
(type x, module LstImpl: Lst with type a = x and type m = list(x)) =>
LstImpl.f(x => [x]);
let from_lst = (type x,
module LstImpl: Lst with type a = x and type m = list(x)
) => LstImpl.f(x => [x]);
4 changes: 3 additions & 1 deletion src/content/3.11/code/reason/snippet07.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ module type Exp = {
type k('a) = ('a, a);

include
Lan with type k('a) := (a, 'a) and type d('a) := d('a) and type a := b;
Lan with type k('a) := (a, 'a)
and type d('a) := d('a)
and type a := b;
};
13 changes: 6 additions & 7 deletions src/content/3.11/code/reason/snippet08.re
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
let to_exp =
(type a', type b', f): (module Exp with type a = a' and type b = b') =>
(module
{
let to_exp = (type a', type b', f) :
(module Exp with type a = a' and type b = b') =>
(module {
type a = a';
type b = b';
type d('a) =
Expand All @@ -11,10 +10,10 @@ let to_exp =

let fk = ((a, _)) => f(a);
let di = I();
});
});

let from_exp =
(type a', type b', module E: Exp with type a = a' and type b = b', a) => {
let from_exp = (type a', type b',
module E: Exp with type a = a' and type b = b', a) => {
let I(i) = E.di;
E.fk((a, i));
};
10 changes: 5 additions & 5 deletions src/content/3.11/code/reason/snippet10.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ module FreeFunctor = (F: {type f('a);}) : Functor => {
type t('a) = (module F with type a = 'a);

let fmap =
(type a', type b', f: a' => b', module FF: F with type a = a')
: (module F with type a = b') =>
(module
{
(type a', type b',
f: a' => b', module FF: F with type a = a') :
(module F with type a = b') =>
(module {
type f('a) = F.f('a);
type a = b';
type i = FF.i;

let h = i => f(FF.h(i));
let fi = FF.fi;
});
});
};
5 changes: 2 additions & 3 deletions src/content/3.11/code/reason/snippet12.re
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ module FreeFunctorAlt = (F: {type f('a);}) : Functor => {
let fmap =
(type a', type b', f, module FF: F with type a = a')
: (module F with type a = b') =>
(module
{
(module {
type a = b';
type f('a) = F.f('a);

let freeF = bi => FF.freeF(a => bi(f(a)));
});
});
};
9 changes: 5 additions & 4 deletions src/content/3.2/code/reason/snippet06.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* Putting it all together to show the equivalence between unit/counit and left_adjunct/right_adjunct */
/* Putting it all together to show the equivalence between
* unit & counit and left_adjunct & right_adjunct */
module type Adjunction =
(F: Functor, U: Representable) => {
let unit: 'a => U.t(F.t('a));
Expand All @@ -7,7 +8,7 @@ module type Adjunction =
let right_adjunct: ('a => U.t('b), F.t('a)) => 'b;
};

/* Adjunction via unit/counit */
/* Adjunction via unit & counit */
module type Adjunction_Unit_Counit =
(F: Functor, U: Representable) => {
let unit: 'a => U.t(F.t('a));
Expand All @@ -21,7 +22,7 @@ module type Adjunction_Hom_Set =
let right_adjunct: ('a => U.t('b), F.t('a)) => 'b;
};

/* Implementing unit/counit from left and right adjoint definitions */
/* Implementing unit & counit from left and right adjoint definitions */
module Adjunction_From_Hom_Set = (A: Adjunction_Hom_Set) : Adjunction =>
(F: Functor, U: Representable) => {
type f('t) = F.t('t);
Expand All @@ -34,7 +35,7 @@ module Adjunction_From_Hom_Set = (A: Adjunction_Hom_Set) : Adjunction =>
let counit: 'a. f(u('a)) => 'a = fua => M.right_adjunct(idty, fua);
};

/* Implementing left and right adjunct from unit/counit Definitions */
/* Implementing left and right adjunct from unit & counit Definitions */
module Adjunction_From_Unit_Counit = (A: Adjunction_Unit_Counit) : Adjunction =>
(F: Functor, U: Representable) => {
type f('t) = F.t('t);
Expand Down
3 changes: 2 additions & 1 deletion src/content/3.4/code/reason/snippet01.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Depends on OCaml library Core */
module Vlen = (F: Functor with type t('a) = list('a)) => {
let summable: module Base__.Container_intf.Summable with type t = float =
let summable:
module Base__.Container_intf.Summable with type t = float =
(module Float);

let vlen =
Expand Down
5 changes: 2 additions & 3 deletions src/content/3.4/code/reason/snippet02.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module WriterInstance =
(W: {type w;})
: (Functor with type t('a) = writer(W.w, 'a)) => {
module WriterInstance = (W: {type w;}) :
(Functor with type t('a) = writer(W.w, 'a)) => {
type t('a) = writer(W.w, 'a);

let fmap = (f, Writer(a, w)) => Writer(f(a), w);
Expand Down
3 changes: 2 additions & 1 deletion src/content/3.4/code/reason/snippet05.re
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module WriterMonad = (W: Monoid) : (Monad with type m('a) = writer(W.a, 'a)) => {
module WriterMonad = (W: Monoid) :
(Monad with type m('a) = writer(W.a, 'a)) => {
type m('a) = writer(W.a, 'a);

let (>=>) = (f, g, a) => {
Expand Down
3 changes: 2 additions & 1 deletion src/content/3.4/code/reason/snippet17.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* Import Str module using this - #require "str" */
let to_words = s => Writer(Str.split(Str.regexp("\b"), s), "to_words");

module Writer_Process = (W: Monad with type m('a) = writer(string, 'a)) => {
module Writer_Process = (W: Monad with type m('a) =
writer(string, 'a)) => {
let process = W.(up_case >=> to_words);
};
5 changes: 3 additions & 2 deletions src/content/3.4/code/reason/snippet18.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Process_Do = (W: Monad_Bind with type m('a) = writer(string, 'a)) => {
module Process_Do = (W: Monad_Bind with type m('a) =
writer(string, 'a)) => {
/* Needs Reason parser >= 3.6.0 */
let ( let* ) = W.(>>=);
let (let*) = W.(>>=);

let process = s => {
let* up_str = up_case(s);
Expand Down
5 changes: 3 additions & 2 deletions src/content/3.4/code/reason/snippet22.re
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Process_Tell = (W: Monad_Bind with type m('a) = writer(string, 'a)) => {
module Process_Tell = (W: Monad_Bind with type m('a) =
writer(string, 'a)) => {
/* Needs Reason parser >= 3.6.0 */
let ( let* ) = W.(>>=);
let (let*) = W.(>>=);
let tell = w => Writer((), w);

let process = s => {
Expand Down
2 changes: 1 addition & 1 deletion src/content/3.5/code/reason/snippet03.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Pythagorean = {
let ( let* ) = Fn.flip(Gen.flat_map);
let (let*) = Fn.flip(Gen.flat_map);
let (let+) = (x, f) => Gen.map(f, x);
let guard = b => b ? Gen.return() ? Gen.empty;

Expand Down
2 changes: 1 addition & 1 deletion src/content/3.5/code/reason/snippet05.re
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Pythagorean = {
let ( let* ) = Fn.flip(Gen.flat_map);
let (let*) = Fn.flip(Gen.flat_map);
let (let+) = (x, f) => Gen.map(f, x);
let guard = b => b ? Gen.return() : Gen.empty;

Expand Down
Loading

0 comments on commit 84cf2cc

Please sign in to comment.