You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Fix typo in higher kinded data pattern
* Upgrade to PS 0.15.2 and package set
* Add recursive type workaround with newtype example
* Document typed holes on bindings tip/trick
* Add entry for `purs-backend-es` and its library
* Update example CI files
* Cleanup immutability & persistence section via MermaidJs
* Update project to 0.15.4; update esbuild
* Also recommend `purs-backend-es` as optional tooling
* Cleanup the WriterT/ExceptT overviews
* Add another page for state-like transformers
* Update preface's version of PureScript
If you want to install a PureScript formatter, refer to their instructions. The history behind these tools will be covered in the `Build Tools` folder:
54
54
55
55
-[purs-tidy](https://github.com/natefaubion/purescript-tidy) - A self-contained formatter written in PureScript
56
56
-[pose](https://pose.rowtype.yoga/) - A plugin written in PureScript for the [`Prettier`](https://prettier.io/) formatter
57
57
58
+
If you want to produce optimized JavaScript for your production environment (rather than a developer environment), install [`purs-backend-es`](https://github.com/aristanetworks/purescript-backend-optimizer):
@@ -7,37 +7,138 @@ In order to abide by the principle of pure functions, FP Data Types tend to adhe
7
7
1. Immutable - the data does not change once created. To modify the data, one must create a copy of the original that includes the update.
8
8
2. Persistent - Rather than creating the entire structure again when updating, an update should create a new 'version' of a data structure that includes the update
9
9
10
-
For example...
11
-
```haskell
12
-
{-
13
-
Given a linked-list type where
14
-
"Nil" is a placeholder representing the end of the list
15
-
"←" in "left ← right" is a pointer that points from the
16
-
right element to the left element
17
-
"=" in "list = x" binds the 'x' name to the 'list' value -}
18
-
Nil←1←2←3= x
19
-
{-
20
-
To change x's `2` to `4`, we would create a new 'version' of 'x'
We'll use a linked-list (see below) to demonstrate the above two ideas.
11
+
12
+
```mermaid
13
+
flowchart RL
14
+
3 ---> 2
15
+
2 ---> 1
16
+
1 ---> Nil
17
+
```
18
+
19
+
Each list is one of two values:
20
+
- the end of the list (i.e. `Nil`)
21
+
- a node that stores one element in the list and a pointer to the rest of the list (i.e. `...<--[3]`)
22
+
23
+
## Mutability vs Immutability
24
+
25
+
Let's say we have a list that is assigned to the variable `list1`:
26
+
```mermaid
27
+
flowchart RL
28
+
subgraph x["list1"]
29
+
direction RL
30
+
3 ---> 2
31
+
2 ---> 1
32
+
1 ---> Nil
33
+
end
34
+
```
35
+
36
+
Let's say we want to change element `2` to `5`. There are a few ways we could do this modification to `list1`.
37
+
38
+
The first way is to mutate `list1` directly, so that `list1` refers to new list. A data type that gets modified like this is a **mutable data type**.
39
+
40
+
Before our modification, this is what `list1` looks like:
41
+
```mermaid
42
+
flowchart RL
43
+
subgraph x["list1"]
44
+
direction RL
45
+
3 ---> 2
46
+
2 ---> 1
47
+
1 ---> Nil
48
+
end
49
+
```
50
+
51
+
After our modification, this is what `list1` looks like:
52
+
53
+
```mermaid
54
+
flowchart RL
55
+
subgraph x["list1"]
56
+
direction RL
57
+
3 ---> 5
58
+
5 ---> 1
59
+
1 ---> Nil
60
+
end
61
+
```
62
+
63
+
The second way is to create a new version of `list1` called `list1Again`. A data type that gets modified like this is an **immutable data type**.
64
+
65
+
Before our modification, this is what `list1` looks like:
66
+
```mermaid
67
+
flowchart RL
68
+
subgraph x["list1"]
69
+
direction RL
70
+
3 ---> 2
71
+
2 ---> 1
72
+
1 ---> Nil
73
+
end
74
+
```
75
+
76
+
After our modification, `list1` is unchanged, but the "modified version" is now `list1Again`:
77
+
78
+
```mermaid
79
+
flowchart RL
80
+
subgraph x["list1"]
81
+
direction RL
82
+
3 ---> 2
83
+
2 ---> 1
84
+
1 ---> Nil
85
+
end
86
+
87
+
subgraph y["list1Again"]
88
+
direction RL
89
+
3'["3"] ---> 5'
90
+
5'["5"] ---> 1'
91
+
1'["1"] ---> Nil'["Nil"]
92
+
end
93
+
```
94
+
95
+
### Pros & Cons
96
+
97
+
| Topic | Mutable Data Types | Immutable Data Types |
98
+
|-|-|-|
99
+
| Reasoning | Code is harder to reason about: `list1` can refer to different values at different times | Code is easier to reason about: `list1` always refers to the same value |
100
+
| Memory Usage | Uses less memory; low pressure on garbage collector | Uses more memory; higher pressure on garbage collector |
101
+
| Multi-Threading | Risk of deadlocks, race conditions, etc. | No such risks |
102
+
103
+
## Immutable Data Types: Persistent or Not
104
+
105
+
There are two ways to modify an immutable data type.
106
+
107
+
First, one can choose NOT to share values across the copies. This is what was shown previously:
108
+
109
+
```mermaid
110
+
flowchart RL
111
+
subgraph x["list1"]
112
+
direction RL
113
+
3 ---> 2
114
+
2 ---> 1
115
+
1 ---> Nil
116
+
end
117
+
118
+
subgraph y["list1Again"]
119
+
direction RL
120
+
3'["3"] ---> 5'
121
+
5'["5"] ---> 1'
122
+
1'["1"] ---> Nil'["Nil"]
123
+
end
124
+
```
125
+
126
+
Second, one can choose to share values across the copies. This is an example of a **persistent immutable data structure**. Sharing is caring:
Copy file name to clipboardExpand all lines: 03-Build-Tools/Readme.md
+9Lines changed: 9 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -75,6 +75,14 @@ In PureScript `0.15.0`, we stopped compiling PureScript source code to CommonJS
75
75
76
76
See the [0.15.0 Migration Guide](https://github.com/purescript/documentation/blob/master/migration-guides/0.15-Migration-Guide.md) for more details.
77
77
78
+
### Phase 7: Producing Optimized JavaScript and Compiling to Other Languages
79
+
80
+
While the Purescript compiler produces correct JavaScript code, there were a number of optimizations that haven't been implemented in the compiler. However, the compiler was designed to output not just JavaScript, but an intermediate representation called `CoreFn`. This enables others to transform `CoreFn` to another language.
81
+
82
+
Soon after the time that PureScript `0.15.4` was released, a new project called `purs-backend-es` was released. This project works on the `CoreFn` representation and transforms it to JavaScript. However, it also optimizes the code significantly during this tranformation. For a few example, see [the `purs` and `purs-backend-es` comparison table in its README](https://github.com/aristanetworks/purescript-backend-optimizer#overview).
83
+
84
+
While this tool's main purpose is to produce optimized JavaScript code, it enables others to produce new backends. A backend is a target language to which PureScript can be compiled. Before this tool, every backend had to reinvent a lot of code to make it work for that language. With the underlying library, `purescript-backend-optimizer`, one can more easily produce a new backend.
85
+
78
86
## Overview of Tools
79
87
80
88
| Name | Type/Usage | Comments | URL |
@@ -84,6 +92,7 @@ See the [0.15.0 Migration Guide](https://github.com/purescript/documentation/blo
| pscid | `pulp --watch build` on steroids | Seems to be a more recent version of `psc-pane` (see below) and uses `psa` | https://github.com/kRITZCREEK/pscid
89
98
| psvm-js | PureScript Version Manager | -- | https://github.com/ThomasCrevoisier/psvm-js
0 commit comments