4PureScript is a small strongly, statically typed compile-to-JS language with a number of interesting features, such as:
7- Higher Kinded Polymorphism
8- Support for basic Javascript types
11- Optimizer rules for generation of efficient Javascript
17- Tail-call elimination
23I was looking for a simple functional language which would compile to JavaScript and have the following characteristics:
25- Generate simple readable Javascript
26- Provide the ability to compile to tight loops if necessary
27- Reasonable type system
28- Ideally, written in a programming language I would enjoy working in
29- Provides a simple interface to existing Javascript code
31I didn't find exactly what I was looking for, so I wrote PureScript. It doesn't have everything right now, but it should serve as a simple core on which to develop new ideas.
33PureScript was originally designed to implement purely functional core logic. However, recent additions to the compiler and libraries now also make it a good option for general-purpose programming.
35PureScript can also be seen as a trade-off between a theoretically ideal language and one which generates reasonably high performance code.
40As an introductory example, here is the usual "Hello World" written in PureScript::
46 main = trace "Hello, World!"
48which compiles to the following Javascript, ignoring the Prelude::
52 var main = trace("Hello, World!");
54 })(Main = Main || {});
56The following command will compile and execute the PureScript code above::
58 psc input.purs --main | node
63The following code defines a ``Person`` data type and a function to generate a string representation for a ``Person``::
65 data Person = Person { name :: String, age :: Number }
67 showPerson :: Person -> String
68 showPerson (Person o) = o.name ++ ", aged " ++ show o.age
70 examplePerson :: Person
71 examplePerson = Person { name: "Bonnie", age: 26 }
73Line by line, this reads as follows:
75- ``Person`` is a data type with one constructor, also called ``Person``
76- The ``Person`` constructor takes an object with two properties, ``name`` which is a ``String``, and ``age`` which is a ``Number``
77- The ``showPerson`` function takes a ``Person`` and returns a ``String``
78- ``showPerson`` works by case analysis on its argument, first matching the constructor ``Person`` and then using string concatenation and object accessors to return its result.
79- ``examplePerson`` is a Person object, made with the ``Person`` constructor and given the String "Bonnie" for the name value and the Number 26 for the age value.
81The generated Javascript looks like this::
83 var Person = function (value) {
84 return { ctor: 'Person', values: [value] };
87 function showPerson(_1) {
88 return _1.values[0].name + ", aged " + numberToString(_1.values[0].age);
91 var examplePerson = Person({
99PureScript might be compared to other AltJS projects such as Roy, Haste, Fay, Elm and GHCJS. Certainly, there is a lot of overlap in terms of syntax, but the goals of PureScript listed above separate it in one or more ways from each of these languages.
101Roy is probably the most similar language on the list, and was a large influence on the development of PureScript. There are however, key differences in the foreign function interface, the type system and the choice of development language (Haskell vs. Javascript)
103Projects such as Haste, Fay and GHCJS aim to use some combination of the GHC compiler itself and/or its intermediate representation, Core, to perform some of the tasks involved in compilation such as parsing and type checking. This usually gives the advantage that tools and libraries can be shared with Haskell, but often at the cost of the size of the generated Javascript. This is the main practical difference between PureScript and these projects.
105Elm also shares a lot in terms of functionality with PureScript. Elm is designed for functional reactive programming, and focusses on tools and language features suitable for that domain, while PureScript focusses on the development of purely functional core application logic. Another difference between PureScript and Elm is PureScript's lack of a runtime system.
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list