User:Mchinth/sandbox
This is a user sandbox of Mchinth. You can use it for testing or practicing edits. This is not the sandbox where you should draft your assigned article for a dashboard.wikiedu.org course. To find the right sandbox for your assignment, visit your Dashboard course page and follow the Sandbox Draft link for your assigned article in the My Articles section. |
PureScript (/ˈpjʔr.skrʔpt/[1]) is a Functional programming language which compiles to JavaScript. JavaScript does not support powerful abstractions, as a result it cannot utilize full capability of Functional Programming. PureScript addresses this issue by allowing expressive, readable code.
PureScript is a statically typed language. Unlike in other dynamically typed languages, in PureScript, types exist only at compile time, and have no representation at runtime. Programs which are correct will be given a type by the compiler, which illustrates its behavior. Conversely, programs which are not given any type are incorrect.
Getting started
[edit]Firstly, Setup a working environment for PureScript development. Following are the tools required:
- psc - PureScript Compiler
- npm -Node package Manager
- Pulp - Command line tool
Download latest version of PureScript binary from Github [2] and setup the environment variables. Try executing the below command in command line to test:
$psc
Following command is to install the Pulp command line tool:
$ npm install -g pulp
Now you have all tools required to write first PureScript library.
Simple Hello World!
By using pulp init command , create project in an empty directory
$ mkdir my_pscript
$ cd my_pscript
$ pulp init
* Generating project skeleton in ~/my-pscript
$ ls
bower.json src test
Two directories src ,test and a configuration file bower.json are created. src directory contains source files and test directory contains tests performed.
Now Modify src/Main.purs file to create Hello World Program.
module Main where
import Control.Monad.Eff.Console
main = log "Hello, World!"
Now let's build and run the above code.
$ pulp run
* Building project in ~/my-pscript
* Build successful.
Hello, World!
Features
[edit]PureScript is a strong and statically typed compile-to-JS language with a number of interesting features, such as:
Types and Type Inference
[edit]Types in PureScript are inspired from ML and Haskell and are not similar to the types normally seen in languages like Java or C#, although they serve the same purpose at a high level. Type system in purescript supports type inference . Following is a simple example code which defines a number, but there is no reference to the Number type anywhere in the code. [3]
iAmANumber =
let square x = x * x
in square 42.0
Pattern matching
[edit]pattern matching is a technique that allows to code compact functions which express complex ideas. Algebraic data types is a similar feature and is closely related to pattern matching. Below is a Simple Pattern Matching function that calculates GCD of two numbers using Euclidean Algorithm.
gcd :: Int -> Int -> Int
gcd n 0 = n
gcd 0 m = m
gcd n m = if n > m then gcd (n - m) m else gcd n (m - n)
Pattern matching allows to code simple and declarative cases. Functions work by pairing sets with results. Each line in the function is a case, expressions on left of equals sign are patterns. Each case is evaluated and the first case which matches is returned.
There are various types of patterns like Integer literal, Variable, Wildcard and Boolean literal patterns.
Simple FFI
[edit]FFI(Foriegn Function Interface) is a feature provided by PureScript that enables communication between PureScript and JavaScript code. FFI is very flexible and it allows developer to choose between using foreign functions or type system. JavaScript functions might return not null values which have to be handled by the developer when designing these interfaces.
Call the above PureScript module from javascript, using FFI.
var Test = require('Test');
Test.gcd(15)(20);
Modules
[edit]Module keyword is used to refer to modules and all the code in PureScript is written in a module.
module A where
id x = x
When a module is imported(using import keyword) aliases are created for all values and types in it. Values, type constructors and data structures can be explicitly imported. Below is an example to import type constructors by passing a list of data constructors in parentheses.
module B where
import A (runFoo, Foo(..), Bar(Bar))
Modules can be exported by providing a set of names in parenthesis in the declaration similar to import.
Type Classes
[edit]Type classes is a powerful kind of abstraction provided by PureScript's type system. Following is the example of show, which is defined by type class in the Prelude module called Show.
class Show a where
show :: a -> String
Below are some some standard type classes defined in the Prelude and standard libraries.
Eq: Eq type class is used to test two values for equality.
class Eq a where
eq :: a -> a -> Boolean
Ord: The Ord type class is used to compare two values for types which support ordering.
data Ordering = LT | EQ | GT
class (Eq a) <= Ord a where
compare :: a -> a -> Ordering
Num: The Num type class is used to identify the types which support addition, subtraction, multiplication and division operations. class (DivisionRing a) <= Num a
DO Notation
[edit]Do notation is a special syntax which is used to improve the readability of the code. It uses the keyword Do to introduce a block of code which uses do notation. It makes the structure of algorithm clearer. Following is the factors function written using do notation.
factors :: Int -> Array (Array Int) factors n = filter (\xs -> product xs == n) $ do
i <- 1 .. n j <- i .. n return [i, j]
Syntax
[edit]Functions and Records
[edit]Functions and records are the building blocks of PureScript.
Simple Types
[edit]PureScript defines seven built-in types: numbers, strings, booleans, integers, arrays, records, and functions which are defined in Prim module (implicitly imported in every module). These can be seen in PSCi by using the :type command. Following are a few examples.
$ pulp psci
> :type 1.0
Prim.Number
> :type "test"
Prim.String
> :type true
Prim.Boolean
> :type 1
Prim.Int
Unlike JavaScript, all the elements of PureScript array should be of same type.
> :type [1, 2, 3]
Prim.Array Prim.Int
> :type [true, false]
Prim.Array Prim.Boolean
> :type [1, false]
Cannot unify Prim.Int with Prim.Boolean.
Records are PureScript's objects and the syntax of Record literals is same as that of JavaScript's Object literals.
> let author = { name: "Phil", interests: ["Functional Programming", "JavaScript"] }
> :type author
{ name :: Prim.String, interests :: Prim.Array Prim.String }
Dot is used to access the fields of records.
> author.interests
["Functional Programming","JavaScript"]
Functions
[edit]Functions can be defined inline ( using \ followed by space) or they can be defined at the top-level of file (by specifying arguments before equal sign).
> let
add :: Int -> Int -> Int
add = \x y -> x + y
or
add :: Int -> Int -> Int
add x y = x + y
See also
[edit]- JavaScript , dynamic , high level interpreted language
- CoffeeScript, a programming language that transcompiles to JavaScript
- GorillaScript, which offers additional type checking and syntactic sugar for common JS patterns such as inline callbacks
- LiveScript
- Amber Smalltalk
- Haskell,ML
- TypeScript, an open-source, strict superset of Javascript language from Microsoft
- RapydScript,[4] a translator with a more Pythonic syntax
Further reading
[edit]- Lee, Patrick (May 14, 2014). "CoffeeScript in Action" (First ed.). Manning Publications: 432. ISBN 978-1617290626.
{{cite journal}}
: Cite journal requires|journal=
(help) - Grosenbach, Geoffrey (May 12, 2011). "Meet CoffeeScript" (First ed.). PeepCode.
{{cite journal}}
: Cite journal requires|journal=
(help) - Bates, Mark (May 31, 2012). "Programming in CoffeeScript" (First ed.). Addison-Wesley: 350. ISBN 0-321-82010-X.
{{cite journal}}
: Cite journal requires|journal=
(help) - MacCaw, Alex (January 31, 2012). "The Little Book on CoffeeScript" (First ed.). O'Reilly Media: 62. ISBN 978-1449321055.
{{cite journal}}
: Cite journal requires|journal=
(help) - Burnham, Trevor (August 3, 2011). "CoffeeScript: Accelerated JavaScript Development" (First ed.). Pragmatic Bookshelf: 138. ISBN 978-1934356784.
{{cite journal}}
: Cite journal requires|journal=
(help)
Version History
[edit]Following table is based on the information provided in Github. [5]
Version | Release date | Equivalent to | Changes | Enhancements | Bug Fixes |
---|---|---|---|---|---|
Polymorph |
V0.5.0 Apr 27, 2014 | Block support has been removed Naming scheme of prelude modules is similar to haskell |
Pretty printing for row types has been improved | Overlapping variables in typeclass instances are rejected A bug in the optimizer related to inlining was fixed | |
V0.5.4 | Aug 3, 2014 | Provides bug fixes and features required to compile the latest core libraries | |||
V0.5.5 | Sep 2, 2014 | ||||
V0.5.6 | Sep 21, 2014 | ||||
V0.5.6.1 | Sep 22, 2014 | ||||
V0.5.6.2 | Sep 22, 2014 | ||||
V0.5.6.3 | Oct 6, 2014 | ||||
V0.5.7 | Oct 29, 2014 | ||||
V0.5.7.1 | Oct 30, 2014 | ||||
Holoship |
V0.5.6.0 Nov 8, 2014 | Alternative type class hierarchy has been refactored Runtime type checks has been removed |
Tail call optimization has been improved for functions using case expressions. Generated code for type class dictionaries was simplified |
Binding group errors in type class members are now caught at compile time. | |
V0.6.0.2 | Nov 9, 2014 | Prevent psci and psc-make from rebuilding everything on every build | |||
V0.6.0.1 | Nov 17, 2014 | The body of a guarded expression must now be indented past the guard. The body of a guarded expression must now be indented past the guard. |
The body of a guarded expression must now be indented past the guard. Import instances when referencing qualified values |
||
V0.6.1.1 | Nov 19, 2014 | The pipe symbol is now a reserved operator. The operators in the Bits type class have been renamed. |
Relax indentation requirements | ||
V0.6.1.2 | Nov 24, 2014 | ||||
V0.6.2 | Nov 28, 2014 | Command line options with multiplicity 1 now require an equals symbol | Use optparse-applicative instead of cmdtheline | ||
V0.6.3 | Jan 8, 2015 | There is a new "core functional representation", which will enable certain optimizations, and new features such as rewrite rules. | Case statement at end of Eff block not being executed. | ||
V0.6.4 | Jan 23, 2015 | Fix some precedence issues in the code generator. Fixed a bug in the typechecker. | |||
V0.6.4.1 | Feb 3, 2015 | Various small bug fixes. | |||
V0.6.5 | Feb 8, 2015 | Lightweight record constructors are now supported Syntactic sugar has been introduced for object update functions |
Some command line options were fixed in psc-make Some module import errors were fixed | ||
V0.6.6 | Feb 8, 2015 | The syntax of record getters was changed to _.prop | The record part of a record updater can now be made into a wildcard | Newline issues were fixed in executables | |
V0.6.7 | Feb 12, 2015 | ||||
V0.5.7.1 | Oct 30, 2014 | ||||
V0.5.7.1 | Oct 30, 2014 |
- ^ "PureScript". Collins English Dictionary – Complete & Unabridged 2012 Digital Edition. William Collins Sons & Co.
- ^ "PureScript Binary". Github.com.
- ^ "PureScript – Types". Learnpub.com.
- ^ "Rapydscript bitbucket repository". Atlassian Bitbucket. 3 Apr 2013. Retrieved 11 May 2014.
- ^ "PureScript – version history". Github.