You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _site/download/index.html
+16-3Lines changed: 16 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -65,15 +65,28 @@ <h2>Homebrew</h2>
65
65
66
66
<h2>Stack</h2>
67
67
68
-
<p><ahref="https://www.haskell.org/ghc/">GHC</a> 7.10.1 or newer is required to compile from source. The easiest way is to use <ahref="https://github.com/commercialhaskell/stack">Stack</a>:</p>
68
+
<p><ahref="https://www.haskell.org/ghc/">GHC</a> 7.10.1 or newer is required to compile from source. The easiest way is to use <ahref="https://github.com/commercialhaskell/stack">Stack</a>.</p>
69
+
70
+
<p>Update the Hackage package index:</p>
69
71
<pre>
70
-
stack install purescript --resolver=nightly
71
-
</pre>
72
+
stack update</pre>
73
+
74
+
<p>Download the latest source distribution and place it into a new directory in the current directory:</p>
75
+
<pre>
76
+
stack unpack purescript</pre>
77
+
78
+
<p>Compile and install PureScript:</p>
79
+
<pre>
80
+
cd purescript-x.y.z # (replace x.y.z with whichever version you just downloaded)
81
+
stack install</pre>
72
82
73
83
<p>This will copy the compiler and utilities into <code>~/.local/bin</code>.</p>
74
84
75
85
<p>If you don't have Stack installed, there are install instructions <ahref="https://github.com/commercialhaskell/stack/blob/master/doc/install_and_upgrade.md">here</a>.</p>
76
86
87
+
<p>You can also install a specific version by specifying it in the <code>stack unpack</code> step. For example, to install 0.8.5, use:</p>
<p>This scheme also applies to names of infix operators:</p>
73
-
<divclass="sourceCode"><preclass="sourceCode haskell"><codeclass="sourceCode haskell">(<spanclass="fu">%</span>) a b <spanclass="fu">=</span><spanclass="fu">...</span></code></pre></div>
<h4id="calling-javascript-from-purescript">Calling Javascript from PureScript</h4>
77
75
<p>Javascript values and functions can be used from PureScript by using the FFI. The problem becomes how to choose suitable types for values originating in Javascript.</p>
78
76
<p>The general rule regarding types is that you can enforce as little or as much type safety as you like when using the FFI, but you should be careful to avoid common pitfalls when dealing with Javascript values, like the possibility of null or undefined values being returned from a Javascript function. Functions defined in the Prelude and core libraries tend to err on the side of type safety where possible.</p>
79
77
<h4id="foreign-modules">Foreign Modules</h4>
80
78
<p>In PureScript, JavaScript code is wrapped using a <em>foreign module</em>. A foreign module is just a CommonJS module which is associated with a PureScript module. Foreign modules are required to adhere to certain conventions:</p>
81
79
<ul>
82
-
<li>The module must contain a comment of the form <code>// module ModuleName</code>, which associates the foreign module with its companion PureScript module.</li>
80
+
<li>The name of the foreign module must be the same as its companion PureScript module, with its extension changed to <code>.js</code>. This associates the foreign module with the PureScript module.</li>
83
81
<li>All exports must be of the form <code>exports.name = value;</code>, specified at the top level.</li>
84
82
</ul>
85
83
<p>Here is an example, where we export a function which computes interest amounts from a foreign module:</p>
<p>This file should be saved as <code>src/Interest.js</code>. The corresponding PureScript module <code>Interest</code> will be saved in <code>src/Interest.purs</code> (these filenames are merely conventions, but are used by certain tools, such as the Pulp build tool), and will look like this:</p>
89
+
<p>This file should be saved as <code>src/Interest.js</code>. The corresponding PureScript module <code>Interest</code> will be saved in <code>src/Interest.purs</code>, and will look like this:</p>
94
90
<preclass="purescript"><code>module Interest where
95
91
96
92
foreign import calculateInterest :: Number -> Number</code></pre>
@@ -100,23 +96,21 @@ <h4 id="functions-of-multiple-arguments">Functions of Multiple Arguments</h4>
100
96
<p>Suppose we wanted to modify our <code>calculateInterest</code> function to take a second argument:</p>
<p>A correct <code>foreign import</code> declaration now should use a foreign type whose runtime representation correctly handles functions of multiple arguments. The <code>purescript-functions</code> package provides a collection of such types for function arities from 0 to 10:</p>
109
103
<preclass="purescript"><code>module Interest where
110
104
105
+
import Data.Function (Fn2)
106
+
111
107
foreign import calculateInterest :: Fn2 Number Number Number</code></pre>
112
108
<p>Here, the <code>Fn2</code> type constructor is used to wrap Javascript functions of two arguments. We can write a curried wrapper function in PureScript which will allow partial application:</p>
113
109
<preclass="purescript"><code>calculateInterestCurried :: Number -> Number -> Number
<p>An alternative is to use curried functions in the native module, using multiple nested functions, each with a single argument, as the runtime representation of the function type constructor <code>(->)</code> dictates:</p>
<spanclass="ot">inOrder ::</span> forall a<spanclass="fu">.</span>(<spanclass="dt">Ord</span> a)<spanclass="ot">=></span> a <spanclass="ot">-></span> a <spanclass="ot">-></span><spanclass="dt">Tuple</span> a a
129
+
<spanclass="ot">inOrder ::</span> forall a<spanclass="fu">.</span><spanclass="dt">Ord</span> a <spanclass="ot">=></span> a <spanclass="ot">-></span> a <spanclass="ot">-></span><spanclass="dt">Tuple</span> a a
<p>PureScript’s generics are supported by the <code>purescript-generics</code> library, and in particular, the <code>Data.Generic.Generic</code> type class:</p>
44
44
<divclass="sourceCode"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><spanclass="kw">class</span><spanclass="dt">Generic</span> a <spanclass="kw">where</span>
45
45
<spanclass="ot"> toSignature ::</span><spanclass="dt">Proxy</span> a <spanclass="ot">-></span><spanclass="dt">GenericSignature</span>
46
-
<spanclass="ot"> toSpine ::</span> a <spanclass="ot">-></span><spanclass="dt">GenericSpine</span>
<divclass="sourceCode"><preclass="sourceCode haskell"><codeclass="sourceCode haskell"><spanclass="ot">readGeneric ::</span> forall a<spanclass="fu">.</span><spanclass="dt">Generic</span> a <spanclass="ot">=></span><spanclass="dt">Options</span><spanclass="ot">-></span><spanclass="dt">Foreign</span><spanclass="ot">-></span><spanclass="dt">F</span> a</code></pre></div>
89
89
<p>The <code>Options</code> type here is based on the options record from Haskell’s <code>aeson</code> library. For our purposes, the default options will work, but we need to turn on the <code>unwrapNewtypes</code> option, so that our <code>newtype</code> constructor gets ignored during serialization:</p>
Copy file name to clipboardExpand all lines: _site/learn/getting-started/index.html
+20-22Lines changed: 20 additions & 22 deletions
Original file line number
Diff line number
Diff line change
@@ -36,7 +36,7 @@ <h2>Menu</h2>
36
36
<main>
37
37
<sectionclass="article">
38
38
<h2>Getting Started with PureScript</h2>
39
-
<divclass="meta">By Phil Freeman, published on December 13, 2015</div>
39
+
<divclass="meta">By Phil Freeman, published on May 24, 2016</div>
40
40
41
41
<p>Welcome to the PureScript community blog! In this first post, I’m going to walk through the basics of getting set up to use the PureScript compiler <code>psc</code>, and its interactive mode <code>psci</code>.</p>
42
42
<p>I’ll start with the installation of the compiler and Pulp build tool, and then go through the basic usage of <code>psci</code>, working towards a solution of problem 1 from <ahref="http://projecteuler.net/problem=1">Project Euler</a>.</p>
<p>PSCi is the interactive mode of PureScript. It is useful for working with pure computations, and for testing ideas.</p>
75
75
<p>Open PSCi by typing <code>pulp psci</code> at the command line. Pulp will create a file in your directory called <code>.psci</code>, which contains instructions to PSCi to load your modules and dependencies. If you invoke the PSCi executable directly, you would need to load these files by hand.</p>
<p>We will use a selection of these commands during this tutorial.</p>
102
94
<p>Start by pressing the Tab key to use the autocompletion feature. You will see a collection of names of functions from the Prelude which are available to use.</p>
103
-
<p>To see the type of one of these values, use the <code>:type</code> command, followed by a space, followed by the name of the value:</p>
104
-
<pre><code>> :type Prelude.map
95
+
<p>To see the type of one of these values, first import the appropriate module using the <code>import</code> command. Then, use the <code>:type</code> command, followed by a space, followed by the name of the value:</p>
96
+
<pre><code>> import Prelude
97
+
> :type map
105
98
forall a b f. (Prelude.Functor f) => (a -> b) -> f a -> f b
106
-
> :type Data.List.zip
99
+
100
+
> import Data.List
101
+
> :type zip
107
102
forall a b. Data.List.List a -> Data.List.List b -> Data.List.List (Data.Tuple.Tuple a b)</code></pre>
108
-
<p>We will be using some of the functions from the <code>Prelude</code> and <code>Data.List</code> modules, so import those by using the <code>import</code> keyword:</p>
103
+
<p>We will be using some of the functions from the <code>Prelude</code> and <code>Data.List</code> modules, so make sure you have imported those by using the <code>import</code> keyword:</p>
109
104
<pre><code>import Prelude
110
105
import Data.List</code></pre>
111
106
<p>Note that using <code>Tab</code> to autocomplete names can be a useful time-saving device in <code>psci</code>.</p>
0 commit comments