Skip to content

Commit

Permalink
Line editing for Functors (#1127)
Browse files Browse the repository at this point in the history
  • Loading branch information
christinerose committed May 3, 2023
1 parent 5176e68 commit bad21fe
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions data/tutorials/lg_06_functors.md
Expand Up @@ -10,20 +10,20 @@ date: 2021-05-27T21:07:30-00:00
# Functors

Functors are probably one of the most complex features of OCaml, but you don't
have to use them extensively to be a successful OCaml programmer. Actually,
have to use them extensively to be a successful OCaml programmer. Actually,
you may never have to define a functor yourself, but you will surely encounter
them in the standard library. They are the only way of using the Set and Map
modules, but using them is not so difficult.

## What Are Functors and Why Do We Need Them?

A functor is a module that is parametrized by another module, just like a
function is a value which is parametrized by other values, the arguments.
A functor is a module that is parametrised by another module, just like a
function is a value which is parametrised by other values, the arguments.

It allows one to parametrize a type by a value, which is not possible directly
It allows one to parametrise a type by a value, which is not possible directly
in OCaml without functors. For example, we can define a functor that takes an
int n and returns a collection of array operations that work exclusively on
arrays of length n. If by mistake the programmer passes a regular array to one
`int n` and returns a collection of array operations that work exclusively on
arrays of length `n`. If by mistake the programmer passes a regular array to one
of those functions, it will result in a compilation error. If we were not using
this functor but the standard array type, the compiler would not be able to
detect the error, and we would get a runtime error at some undetermined date in
Expand All @@ -37,7 +37,7 @@ things: the type of elements, given as `t` and the comparison function given as
`compare`. The point of the functor is to ensure that the same comparison
function will always be used, even if the programmer makes a mistake.

For example, if we want to use sets of ints, we would do this:
For example, if we want to use sets of `ints`, we would do this:

```ocaml
# module Int_set =
Expand Down Expand Up @@ -96,8 +96,8 @@ module Int_set :

For sets of strings, it is even easier because the standard library provides a
`String` module with a type `t` and a function `compare`. If you were following
carefully, by now you must have guessed how to create a module for the
manipulation of sets of strings:
carefully, by now you must have guessed how to create a module to
manipulate string sets:

```ocaml
# module String_set = Set.Make (String);;
Expand Down Expand Up @@ -177,7 +177,7 @@ struct
end
```

or by specifying this in the .mli file:
or by specifying this in the `.mli` file:

<!-- $MDX skip -->
```ocaml
Expand Down

0 comments on commit bad21fe

Please sign in to comment.