Skip to content

Commit 8743e76

Browse files
committed
Split “why this format” into a README.md
1 parent b7357ba commit 8743e76

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

examples/README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Examples
2+
3+
* [Using Prisms with Sum Types](src/PrismsForSumTypes.purs)
4+
5+
## Conventions and the "why" behind them
6+
7+
The examples are meant to be read from top to bottom.
8+
9+
Every example is self-contained and can be imported into the repl. The
10+
top of an example module will have a comment like this:
11+
12+
```purescript
13+
{- If you want to try out examples, paste the following into the repl.
14+
15+
import PrismsForSumTypes
16+
import Data.Lens
17+
18+
-}
19+
```
20+
21+
The modules contain both definitions of optics and sample uses. The
22+
sample uses are executable code that look like this:
23+
24+
```purescript
25+
s1 :: Maybe Color
26+
s1 = preview solidFocus (Solid Color.white) -- (Just rgba 255 255 255 1.0)
27+
```
28+
29+
Why?
30+
31+
1. The second line can be pasted into the repl without needing to use
32+
`:paste`. (I usually don't copy the `s1 =` because I prefer seeing
33+
the results immediately. All types in the examples implement `show`,
34+
making that painless.)
35+
36+
2. The sample uses are executable code so that the compiler checks
37+
them and -- more importantly -- so that they stand out from the
38+
surrounding commentary. That makes an example more easily scannable
39+
when you're refreshing your memory about a particular usage
40+
(assuming you use a syntax highlighter).
41+
42+
3. The name-value bindings (like `s1`) are clutter, but required by the
43+
compiler. Most of the type annotations are also clutter, but are required
44+
to prevent compiler warnings in, for example, `pulp build`.
45+

examples/src/PrismsForSumTypes.purs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ import Color as Color
99
import Data.Maybe
1010
import Data.Record.ShowRecord (showRecord)
1111
12-
Examples are written in this format:
13-
14-
s1 :: Maybe Color
15-
s1 = preview solidFocus (Solid Color.white) -- (Just rgba 255 255 255 1.0)
16-
17-
That's so that a typical syntax highlighter will make the executable
18-
code easy to spot among the commentary. (The name-value bindings and
19-
type annotations are unfortunate clutter, but needed to prevent noise
20-
from compiler warnings.)
12+
See `README.md` if you're wondering why code is formatted the way it is.
2113
2214
-}
2315

@@ -80,11 +72,11 @@ instance showFill :: Show Fill where
8072

8173

8274
{------ Making prisms with Maybe and `prism'` ------}
83-
{------ Basic usage ------}
75+
{------ Basic usage: `preview`, `review`, `is`, and `isn't` ------}
8476

8577
-- Two function arguments: a data constructor for the type in
86-
-- question, plus one that "substitutes" your desired case with `Just`
87-
-- and converts all other values to `Nothing`.
78+
-- question, plus one that converts your desired case to a
79+
-- `Just <wrapped values>` or `Nothing`.
8880

8981
solidFocus :: Prism' Fill Color
9082
solidFocus = prism' constructor focus

0 commit comments

Comments
 (0)