forked from purescript/purescript.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
170 lines (139 loc) · 6.83 KB
/
index.html
File metadata and controls
170 lines (139 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>PureScript</title>
<link rel="stylesheet" href="css/style.css"/>
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:400,300,700"/>
</head>
<body class="home">
<script>document.body.className = "home hasJS";</script>
<header>
<div class="wrap">
<h1><a href="/">PureScript</a></h1>
<nav>
<h2>Menu</h2>
<ul>
<li><a href="/" class="active">Home</a></li>
<li><a href="download/">Download</a></li>
<li><a href="learn/">Learn</a></li>
<li><a href="projects/">Projects</a></li>
</ul>
</nav>
</div>
</header>
<main>
<section class="intro">
<h2>PureScript is a small strongly typed programming language that compiles to JavaScript.</h2>
<div class="link">
<a href="download/">Download</a>
</div>
</section>
<section class="example">
<h3>Examples</h3>
<div class="current example">
<h4>Modifying the DOM</h4>
<p>PureScript’s expressive type system and lightweight syntax make it simple to define <a href="https://leanpub.com/purescript/read#leanpub-auto-domain-specific-languages">domain-specific languages</a>, which can be used to solve problems like templating the DOM. Bindings also exist for libraries such as React and Angular.js.</p>
<pre>
<span class="kr">import</span> <span class="nn">Data.DOM.Free</span>
<span class="nf">doc</span> = div [ _class <span class="kt">:=</span> <span class="s">"image"</span> ] $ <span class="kr">do</span>
elem $ img [ src <span class="kt">:=</span> <span class="s">"logo.jpg"</span>
, width <span class="kt">:=</span> <span class="mi">100</span>
, height <span class="kt">:=</span> <span class="mi">200</span>
]
text <span class="s">"Functional programming for the web!"</span></pre>
</div>
<div class="example">
<h4>HTML5 Canvas</h4>
<p>Higher-order functions allow the developer to write fluent, expressive code. Here, higher-order functions are being used to capture some common patterns when <a href="https://leanpub.com/purescript/read#leanpub-auto-canvas-graphics">working with HTML5 canvas</a>, such as saving and restoring the context and drawing closed paths.</p>
<pre>
<span class="kr">import</span> <span class="nn">Control.Apply</span>
<span class="kr">import</span> <span class="nn">Graphics.Canvas</span> (<span class="nf">getCanvasElementById</span>, <span class="nf">getContext2D</span>)
<span class="kr">import</span> <span class="nn">Graphics.Canvas.Free</span>
<span class="nf">closed</span> path = beginPath *> path <* closePath
<span class="nf">filled</span> shape = shape <* fill
<span class="nf">withContext</span> shape = save *> shape <* restore
<span class="nf">scene</span> = withContext <span class="kr">do</span>
setFillStyle <span class="s">"#FF0000"</span>
filled $ closed <span class="kr">do</span>
moveTo <span class="mi">0</span> <span class="mi">0</span>
lineTo <span class="mi">50</span> <span class="mi">0</span>
lineTo <span class="mi">25</span> <span class="mi">50</span>
<span class="nf">main</span> = <span class="kr">do</span>
canvas <- getCanvasElementById <span class="s">"canvas"</span>
context <- getContext2D canvas
runGraphics context scene</pre>
</div>
<div class="example">
<h4>Callback Hell</h4>
<p>The problem of <a href="https://leanpub.com/purescript/read#leanpub-auto-callback-hell">callback hell</a> can be solved by using PureScript’s type system to capture complex control flow as functions in a safe way. Here, the continuation monad is used to hide the boilerplate code associated with handling callbacks.</p>
<pre>
<span class="kr">import</span> <span class="nn">Control.Parallel</span>
<span class="kr">data</span> <span class="kt">Model</span> = <span class="kt">Model</span> [<span class="kt">Product</span>] [<span class="kt">ProductCategory</span>]
<span class="nf">loadModel</span> = <span class="kr">do</span>
model <- runParallel $
<span class="kt">Model</span> <$> inParallel (get <span class="s">"/products/popular/"</span>)
<*> inParallel (get <span class="s">"/categories/all"</span>)
<span class="nf">view</span> (<span class="kt">Model</span> ps cs) = <span class="kr">do</span>
renderProducts ps
renderCategories cs
<span class="nf">main</span> = loadModel `runContT` view</pre>
</div>
<div class="example">
<h4>Generative Testing</h4>
<p>PureScript provides a form of ad-hoc polymorphism in the form of type classes, inspired by Haskell. Type classes are used in the QuickCheck and StrongCheck libraries to support <a href="https://leanpub.com/purescript/read#leanpub-auto-generative-testing">generative testing</a>, which separates test definitions from the generation of test cases.</p>
<pre>
<span class="kr">import</span> <span class="nn">Test.QuickCheck</span>
<span class="nf">main</span> = <span class="kr">do</span>
quickCheck $ <span class="nf">\</span>xs ys ->
isSorted $ merge (sort xs) (sort ys)
quickCheck $ <span class="nf">\</span>xs ys ->
xs `isSubarrayOf` merge xs ys</pre>
</div>
</section>
<section class="features">
<h3>Features</h3>
<ul>
<li>Algebraic data types</li>
<li>Pattern matching</li>
<li>Type inference</li>
<li>Type classes</li>
<li>Higher kinded types</li>
<li>Rank-N types</li>
<li>Extensible records</li>
<li>Extensible effects</li>
<li>Modules</li>
<li>Simple FFI</li>
<li>No runtime system</li>
<li>Human-readable output</li>
</ul>
</section>
</main>
<footer>
<nav>
<ul>
<li><a href="/" class="active">Home</a></li>
<li><a href="download/">Download</a></li>
<li><a href="learn/">Learn</a></li>
<li><a href="projects/">Projects</a></li>
</ul>
<ul class="external">
<li class="github"><a href="https://github.com/purescript/purescript" title="GitHub">GitHub</a></li>
<li class="twitter"><a href="https://twitter.com/purescript" title="Twitter">Twitter</a></li>
</ul>
</nav>
</footer>
<script src="js/lib/zepto-1.1.4.min.js"></script>
<script src="js/home.js"></script>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-56579671-1', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>