Jump straight into toying with PureScript!
Also, if you know JavaScript already, you might find this video interesting, which demonstrates the neat ability to look directly at the very readable generated JavaScript.
We need various tools to get started developing PureScript in a modern fashion. Let's first tackle the npm
installable ones,
$ npm install -g psvm psc-package pulp
Then we are going to setup the PureScript compiler with psvm
,
$ psvm install v0.12.0 # installs PureScript v0.12.0
$ psvm use v0.12.0 # sets v0.12.0 as the currently used compiler
NOTE: You'll need to add $HOME/.psvm/current/bin
to your path, as the currently used PureScript compiler, managed by psvm, will be put there, along with its tools.
- psvm helps us manage multiple installs of the PureScript compiler.
- psc-package is a package manager, aiming to replace bower, that manages dependencies in a much more reliable way, by using curated package sets.
To create a new project, we are going to use pulp
, which is the canonical build tool for PureScript. We want it to use psc-package
, so our init is slightly more verbose, but that is the only place we'll explicitly specify this.
$ mkdir HelloWorld && cd HelloWorld
$ pulp --psc-package init
$ pulp run
which will,
- create a simple boilerplate project with
psc-package.json
, and some other files, based on the current compiler version, - add the Prelude as a dependency,
- and sync the local package database (under the
.psc-package/
directory) by cloning any necessary repositories.
Your starter project will look something like,
.
├── output
│ ├── ...
│ ...
├── psc-package.json
├── src
│ └── Main.purs
└── test
└── Main.purs
which should be pretty self-explanatory.
Creating a JavaScript bundle is quite simple,
pulp build --to hello-world.js
You can inspect the generated JavaScript, which is honestly not that daunting :).
There are a couple of commands that are neat for iterative development, and quick feedback.
The first one is,
$ pulp repl
which drops us into a REPL with the project. We can now import our modules and play around,
$ pulp repl
...
> import Main
> main
Hello sailor!
unit
> :type [1,2,3]
Array Int
You can check out the available commands using :help
.
Next up, using the --watch
flag, we can get pulp to rerun its command on filechanges. For example, to run pulp run
every time your project changes,
$ pulp --watch run
and similarly for pulp test
, via,
$ pulp --watch test
There's also more flags, such as --before
, --then
and --else
, which can be combined like,
$ pulp --watch --before clear --then "say $(basename `pwd`) succeeded." --else 'say $(basename `pwd`) failed.' build
To clear the screen before a build, report that it succeeded if it compiled (using --then
), and reporting it failed if it didn't (via --else
).
I'd advise to read the pulp README for more extensive info on what is possible.
Instead of bower, we'll be using psc-package, which is supposed to be like stack/stackage (i.e. guarenteeing reproducible builds).
You can now install new packages with psc-package install <package name>
or simply add it to you psc-package.json
, and run psc-package install
. Check out psc-package for more documentation.
NOTE: If you run into needing a bower dependency, you can do psc-package add-from-bower <purescript-something>
to add it (assuming bower is installed on the system).
There are a lot of choices, most seem to be based on React though. Of interest, I've found,
- Halogen which does not use React, but instead uses it's own VDOM. It's said to be the most advanced, and perhaps therefore most complex to get started with (but supposedly pays off).
- Pux which follows the Elm architecture, but uses React. It's currently slightly slower, since it has to first render it's own VDOM and then let React render (Smolder Markup -> React Virtual DOM -> DOM patch).
- Spork seems newer, but also interesting and is based on the Elm architecture. It lacks some guide material, but has a lot of module documentation, and some examples.
I very much recommend getting familiar with both the FFI and Eff (effects):
Other than that, knowing the tools you work with is smart:
Finally, some good Youtube videos:
- For why you should jump into PureScript instead of JavaScript/TypeScript https://www.youtube.com/watch?v=JTEfpNtEoSA
Unfortunately the canonical PureScript by Example book is not yet updated for version 0.12, so it's in a bit of an akward stage. There are some updating the examples https://github.com/paf31/purescript-book/pull/135/files. One notable thing was the move from Effect rows (with Eff e a
) to Effect a
, which is in IO
, like Haskell. You can still use effect rows, but they are now meant for more specialized needs, instead of being the default.