Skip to content

Commit 75e3e75

Browse files
authored
Incorporate getting-started content from book (#314)
* Incorporate getting-started content from book * Review feedback - grammar and typos
1 parent 4071a7a commit 75e3e75

File tree

1 file changed

+93
-6
lines changed

1 file changed

+93
-6
lines changed

guides/Getting-Started.md

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,17 @@ We'll start with the installation of the compiler and Spago build tool, and then
66

77
#### Installing the Compiler
88

9-
You'll need [Node.js and npm](https://docs.npmjs.com/getting-started/installing-node) and to be [able to install global packages](https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-1-change-the-permission-to-npm-s-default-directory) to proceed.
9+
You'll need to install [Node.js and npm](https://docs.npmjs.com/getting-started/installing-node). We recommend installing [Node.js and npm via a node version manager](https://docs.npmjs.com/getting-started/installing-node) to avoid issues with installing packages globally. If you choose to install it manually, you might experience the [`EACCES` error when installing packages globally](https://docs.npmjs.com/getting-started/fixing-npm-permissions#option-1-change-the-permission-to-npm-s-default-directory).
1010

11-
The Purescript compiler (`purs`) can be installed with npm:
11+
Install the Purescript compiler (`purs`) with npm:
1212

1313
npm install -g purescript
1414

15-
(It can also be installed from [Hackage](http://hackage.haskell.org/package/purescript), or by downloading the latest [binary bundle](https://github.com/purescript/purescript/releases) for your OS. If you do so, make sure the `purs` executable is on your `$PATH`.)
15+
Try running the PureScript compiler on the command line to verify that the PureScript compiler executables are available on your `$PATH`:
16+
17+
purs
18+
19+
It can also be installed from [Hackage](http://hackage.haskell.org/package/purescript), or by downloading the latest [binary bundle](https://github.com/purescript/purescript/releases) for your OS. If you do so, make sure the `purs` executable is on your `$PATH`.
1620

1721
#### Setting up the Development Environment
1822

@@ -24,6 +28,8 @@ If you don't have Spago installed, install it now:
2428

2529
Create a new project in an empty directory using `spago init`:
2630

31+
mkdir my-project
32+
cd my-project
2733
spago init
2834

2935
Your directory should now contain the following files:
@@ -50,10 +56,12 @@ If everything was built successfully, and the tests ran without problems, then t
5056

5157
#### Installing Dependencies
5258

53-
Dependencies can be installed using Spago. We will be using the `purescript-lists` library shortly, so install it now:
59+
Dependencies can be installed using Spago. We will be using the `lists` library shortly, so install it now:
5460

5561
spago install lists
5662

63+
The `lists` library sources should now be available in the `.spago/lists/{version}/` subdirectory, and will be included when you compile your project.
64+
5765
#### Working in PSCI
5866

5967
PSCi is the interactive mode of PureScript. It is useful for working with pure computations, and for testing ideas.
@@ -172,6 +180,11 @@ multiples = filter (\n -> mod n 3 == 0 || mod n 5 == 0) ns
172180
answer = sum multiples
173181
```
174182

183+
This sample illustrates a few key ideas regarding modules:
184+
185+
- Every file begins with a module header. A module name consists of one or more capitalized words separated by dots. In this case, only a single word is used, but `My.First.Module` would be an equally valid module name.
186+
- Modules are imported using their full names, including dots to separate the parts of the module name. Here, we import the `Prelude` module, which provides `mod`, `==`, and many other common functions. We also import `Data.List` which provides the explicitly-listed `range` and `filter` functions. We can either import all functions in a module implicitly, as is done with `Prelude`, or list them explicitly. Guidelines are to only have one module with implicit imports.
187+
175188
It is possible to load this file directly into the REPL and to continue working:
176189

177190
spago repl
@@ -191,7 +204,7 @@ The compiler will display several warnings about missing type declarations. In g
191204

192205
#### Writing a Test Suite
193206

194-
To test our code, we'll use the `purescript-assert` library:
207+
To test our code, we'll use the `assert` library:
195208

196209
spago install assert
197210

@@ -231,10 +244,84 @@ main = do
231244

232245
The `spago run` command can be used to compile and run the `Main` module:
233246

234-
> spago run
247+
$ spago run
235248
[info] Build succeeded.
236249
The answer is 233168
237250

251+
252+
#### Compiling for the Browser
253+
254+
Spago can be used to turn our PureScript code into JavaScript suitable for use in the web browser by using the `spago bundle-app` command:
255+
256+
$ spago bundle-app
257+
...
258+
Build succeeded.
259+
Bundle succeeded and output file to index.js
260+
261+
All the code in the `src` directory and any project dependencies have been compiled to JavaScript. The resulting code is bundled as `index.js` and has also had any unused code removed, a process known as dead code elimination. This `index.js` file can now be included in an HTML document. If you try this, you should see the words "Hello, World!" printed to your browser's console.
262+
263+
If you open `index.js`, you should see a few compiled modules which look like this:
264+
265+
```javascript
266+
// Generated by purs bundle 0.13.6
267+
var PS = {};
268+
269+
// ...
270+
271+
(function($PS) {
272+
"use strict";
273+
$PS["Euler"] = $PS["Euler"] || {};
274+
var exports = $PS["Euler"];
275+
var Data_EuclideanRing = $PS["Data.EuclideanRing"];
276+
var Data_Foldable = $PS["Data.Foldable"];
277+
var Data_List = $PS["Data.List"];
278+
var Data_List_Types = $PS["Data.List.Types"];
279+
var Data_Semiring = $PS["Data.Semiring"];
280+
var ns = Data_List.range(0)(999);
281+
var multiples = Data_List.filter(function (n) {
282+
return Data_EuclideanRing.mod(Data_EuclideanRing.euclideanRingInt)(n)(3) === 0 || Data_EuclideanRing.mod(Data_EuclideanRing.euclideanRingInt)(n)(5) === 0;
283+
})(ns);
284+
var answer = Data_Foldable.sum(Data_List_Types.foldableList)(Data_Semiring.semiringInt)(multiples);
285+
exports["answer"] = answer;
286+
})(PS);
287+
288+
(function($PS) {
289+
// Generated by purs version 0.13.6
290+
"use strict";
291+
$PS["Main"] = $PS["Main"] || {};
292+
var exports = $PS["Main"];
293+
var Data_Show = $PS["Data.Show"];
294+
var Effect_Console = $PS["Effect.Console"];
295+
var Euler = $PS["Euler"];
296+
var main = Effect_Console.log("The answer is " + Data_Show.show(Data_Show.showInt)(Euler.answer));
297+
exports["main"] = main;
298+
})(PS);
299+
300+
PS["Main"].main();
301+
```
302+
303+
This illustrates a few points about the way the PureScript compiler generates JavaScript code:
304+
305+
- Every module gets turned into an object, created by a wrapper function, which contains the module's exported members.
306+
- PureScript tries to preserve the names of variables wherever possible.
307+
- Function applications in PureScript get turned into function applications in JavaScript.
308+
- The main method is run after all modules have been defined and is generated as a simple method call with no arguments.
309+
- PureScript code does not rely on any runtime libraries. All of the code that is generated by the compiler originated in a PureScript module somewhere which your code depended on.
310+
311+
These points are important since they mean that PureScript generates simple, understandable code. The code generation process, in general, is quite a shallow transformation. It takes relatively little understanding of the language to predict what JavaScript code will be generated for a particular input.
312+
313+
#### Compiling CommonJS Modules
314+
315+
Spago can also be used to generate CommonJS modules from PureScript code. This can be useful when using NodeJS, or just when developing a larger project which uses CommonJS modules to break code into smaller components.
316+
317+
To build CommonJS modules, use the `spago build` command:
318+
319+
$ spago build
320+
...
321+
Build succeeded.
322+
323+
The generated modules will be placed in the `output` directory by default. Each PureScript module will be compiled to its own CommonJS module, in its own subdirectory.
324+
238325
#### What Next?
239326

240327
If you're new to typed functional programming, your next stop should be [PureScript by Example](https://book.purescript.org/), which will walk you through learning PureScript by solving practical problems.

0 commit comments

Comments
 (0)