Getting Started with PureScript

36
Getting Started with PureScript John A. De Goes

Transcript of Getting Started with PureScript

Page 1: Getting Started with PureScript

Getting Started with PureScript

John A. De Goes

Page 2: Getting Started with PureScript

Agenda• Introduction

• What is PureScript

• Syntax & Semantics

• Who Uses PureScript

• Why PureScript

• Ecosystem

• Getting Started

• Node.js

• NPM

• Bower

• PureScript

• Pulp

• Hello World

Page 3: Getting Started with PureScript

What is PureScript?PureScript is a strongly-typed, purely-functional programming language that compiles to Javascript1, and is written in and inspired by Haskell.

1 And C++!

Page 4: Getting Started with PureScript

Syntaximport Control.Applyimport Graphics.Canvas.Freescene = filled $ closed do moveTo 0 0 lineTo 50 0 lineTo 25 50 where closed path = beginPath *> path <* closePath filled shape = shape <* fill

Page 5: Getting Started with PureScript

Semantics• Type Inference

• Higher-Kinded Polymorphism

• Support for basic Javascript types

• Extensible records

• Extensible effects

• Optimizer rules for generation of efficient Javascript

• Pattern matching

• Simple FFI

• Modules

• Rank N Types

• Do Notation

• Tail-call elimination

• Type Classes

Page 6: Getting Started with PureScript

Who Uses PureScript?2

• SlamData

• Xamarin

• DICOM Grid

• Middlebury Interactive Languages

• DICE.fm

• McGraw Hill Financial

2 Cobbled together from various online sources.*

Page 7: Getting Started with PureScript

Why PureScript?Motivation

• You want to or are forced to do front-end or node.js

• You like static typing

• You like functional programming, of the pure variety

• You prefer expressive power over no-frills, opinionated simplicity

• You want to crush Javascript/CoffeeScript/TypeScript/Scala3 beneath your heel... BWAHAHA!

3 OK, not quite yet.

Page 8: Getting Started with PureScript

Why PureScript?..instead of GHCJS?

• "Haskell in hindsight"

• Strict versus lazy

• Zero runtime

• Clean, easy FFI

• Great re-use of third-party JS

• Simpler language than GHC's quadrillion dialects of Haskell

Page 9: Getting Started with PureScript

Ecosystem

Page 12: Getting Started with PureScript

EcosystemPreludes4

• purescript-prelude• purescript-preface• purescript-batteries

4 Yes, there are multiple!

Page 14: Getting Started with PureScript

EcosystemEditor / IDE Support

• Atom

• purescript-contrib/atom-language-purescript

• nwolverson/atom-ide-purescript

• Emacs

• dysinger/purescript-mode

• emacs-pe/purescript-mode

• ardumont/psci-mode

• spion/purscheck

• emacs-pe/flycheck-purescript

• epost/psc-ide-emacs

• Sublime Text 2 - PureScript package

• Vim

• purescript-vim

• FrigoEU/psc-ide-vim

• IntelliJ IDEA - ikarienator/pure-idea

• Visual Studio - nwolverson/vscode-ide-purescript

• General

• kRITZCREEK/psc-ide

• To generate TAGS files, use psc-docs --format etags (or --format ctags)

Page 15: Getting Started with PureScript

EcosystemLibrary Search

Pursuit

Page 16: Getting Started with PureScript

Getting StartedPrerequisites

1. Node.js

2. NPM

3. Bower

4. PureScript

5. Pulp

Page 17: Getting Started with PureScript

Getting StartedPrerequisites: Node.js

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient. Node.js' package ecosystem, npm, is the largest ecosystem of open source libraries in the world.

Node.js allows full-featured, browser-less Javascript programs.

Page 18: Getting Started with PureScript

Getting StartedPrerequisites: Node.js

• Many Javascript dev tools are written in Javascript and run on Node.js

• PureScript dev tools are written in PureScript, compiled to JavaScript

• Bottom Line: You can't live without it (even if you want to!).

Page 19: Getting Started with PureScript

Getting StartedPrerequisites: Node.js

• Installers

https://nodejs.org/en/download/

• Homebrew

brew install node

• MacPorts

port install nodejs

• pkgsrc

pkgin -y install nodejs

• Debian / Ubunutu (4.x)

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -

sudo apt-get install -y nodejs

• Debian / Ubuntu (6.x)

curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

sudo apt-get install -y nodejs

Page 20: Getting Started with PureScript

Getting StartedPrerequisites: NPM

• NPM = Node Package Manager

• Used by Node to manage packages

• Many tools and libraries are distributed through NPM

• Bottom Line: Another thing you can't live without...

Page 21: Getting Started with PureScript

Getting StartedPrerequisites: NPM

• NPM comes pre-installed with Node.js but version may be old

• Self-Updating NPM:

sudo npm install npm -g

Page 22: Getting Started with PureScript

Getting StartedPrerequisites: Bower

• A package manager for the web

• Maintains global registry of name -> URL

• Supports repositories & tags

• Supports all types of dependencies (binary, PureScript, etc.)

• Dependencies specified in bower.json file

• Bottom Line: Almost all PureScript libraries are registered with bower, and almost all PureScript projects maintain dependencies with bower!

Page 23: Getting Started with PureScript

Getting StartedPrerequisites: Bower

{ "name": "purescript-library", "description": "A PureScript library", "authors": [ "John A. De Goes <[email protected]>" ], "license": "Apache 2", "version": "0.1.0", "ignore": [ "**/.*", "node_modules", "bower_components", "output" ], "dependencies": { "purescript-profunctor-lenses": "^1.0.0-rc.1", "purescript-free": "^1.0.0-rc.1", "purescript-console": "^v1.0.0-rc.1", "purescript-either": "^v1.0.0-rc.1", "purescript-maybe": "^1.0.0-rc.1", "purescript-foldable-traversable": "^1.0.0-rc.1", "purescript-monoid": "^1.0.0-rc.2", "purescript-bifunctors": "^1.0.0-rc.1", "purescript-invariant": "^1.0.0-rc.1", "purescript-prelude": "^1.0.0-rc.4", "purescript-control": "^1.0.0-rc.1", "purescript-transformers": "^1.0.0-rc.1" }}

Page 24: Getting Started with PureScript

Getting StartedPrerequisites: Bower

• Install with NPMnpm install -g bower

Page 25: Getting Started with PureScript

Getting StartedPrerequisites: PureScript Compiler

• psc — PureScript compiler

• psc-docs — PureScript documentation generator

• psc-bundle — Bundler & dead-code eliminator

• psci — PureScript Read-Eval-Print-Loop (REPL)

• psc-ide-server — IDE Server

• psc-ide-client — IDE Client

Page 26: Getting Started with PureScript

Getting StartedPrerequisites: PureScript Compiler

• Installers

https://github.com/purescript/purescript/releases/latest

• Install with NPM

npm install -g purescript

Page 27: Getting Started with PureScript

Getting StartedPrerequisites: Pulp

• Pulp: Popular build tool for PureScript projects

• Knows how to perform magical incantations of psc & related tools

• Bottom Line: If you can use it to build your project, then do it!

Page 28: Getting Started with PureScript

Getting StartedPrerequisites: Pulp

• Install with NPMnpm install -g pulp

Page 29: Getting Started with PureScript

Getting StartedHello World

1. Create Directory Structure

2. Create Bower File

3. Install Bower Dependencies

4. Write PureScript Main

5. Build & Run

Page 30: Getting Started with PureScript

Getting StartedHello World: Create Directory Structure

bower.json/src/ /Main.purs

Page 31: Getting Started with PureScript

Getting StartedHello World: Create Bower File

{ "name": "hello world", "dependencies": { "purescript-console": "^0.1.1", "purescript-eff": "^0.1.2", "purescript-prelude": "^0.1.5" }}

Page 32: Getting Started with PureScript

Getting StartedHello World: Install Bower Dependencies

bower install

Page 33: Getting Started with PureScript

Getting StartedHello World: Write PureScript Main

module Main whereimport Prelude(Unit)import Control.Monad.Eff (Eff)import Control.Monad.Eff.Console (CONSOLE, log)main :: forall e. Eff (console :: CONSOLE | e) Unitmain = do log "Hello World!"

Page 34: Getting Started with PureScript

Getting StartedHello World: Build & Run

pulp buildpulp run

Page 35: Getting Started with PureScript

Congratulations, You've Started Your Journey Into The World of PureScript!

Don't Forget the Easy Peasy PureScript Workshop at LambdaConf 2016!

More Resources: http://purescript.org

Page 36: Getting Started with PureScript

THE END