| 5 |
lars |
1 |
<!doctype html>
|
|
|
2 |
<title>CodeMirror: LiveScript mode</title>
|
|
|
3 |
<meta charset="utf-8" />
|
|
|
4 |
<link rel=stylesheet href="../../doc/docs.css">
|
|
|
5 |
<link rel="stylesheet" href="../../lib/codemirror.css">
|
|
|
6 |
<link rel="stylesheet" href="../../theme/solarized.css">
|
|
|
7 |
<script src="../../lib/codemirror.js"></script>
|
|
|
8 |
<script src="livescript.js"></script>
|
|
|
9 |
<style>
|
|
|
10 |
.CodeMirror {
|
|
|
11 |
font-size: 80%;
|
|
|
12 |
border-top: 1px solid silver;
|
|
|
13 |
border-bottom: 1px solid silver;
|
|
|
14 |
}
|
|
|
15 |
</style>
|
|
|
16 |
<div id=nav>
|
|
|
17 |
<a href="http://codemirror.net">
|
|
|
18 |
<h1>CodeMirror</h1>
|
|
|
19 |
<img id=logo src="../../doc/logo.png">
|
|
|
20 |
</a>
|
|
|
21 |
<ul>
|
|
|
22 |
<li>
|
|
|
23 |
<a href="../../index.html">Home</a>
|
|
|
24 |
<li>
|
|
|
25 |
<a href="../../doc/manual.html">Manual</a>
|
|
|
26 |
<li>
|
|
|
27 |
<a href="https://github.com/codemirror/codemirror">Code</a>
|
|
|
28 |
</ul>
|
|
|
29 |
<ul>
|
|
|
30 |
<li>
|
|
|
31 |
<a href="../index.html">Language modes</a>
|
|
|
32 |
<li>
|
|
|
33 |
<a class=active href="#">LiveScript</a>
|
|
|
34 |
</ul>
|
|
|
35 |
</div>
|
|
|
36 |
<article>
|
|
|
37 |
<h2>LiveScript mode</h2>
|
|
|
38 |
<form>
|
|
|
39 |
<textarea id="code" name="code"> # LiveScript mode for CodeMirror # The following script, prelude.ls, is used to # demonstrate LiveScript mode for CodeMirror. # https://github.com/gkz/prelude-ls export objToFunc = objToFunc = (obj) -> (key) -> obj[key] export each = (f, xs) -->
|
|
|
40 |
if typeof! xs is \Object for , x of xs then f x else for x in xs then f x xs export map = (f, xs) --> f = objToFunc f if typeof! f isnt \Function type = typeof! xs if type is \Object {[key, f x] for key, x of xs} else result = [f x for x in
|
|
|
41 |
xs] if type is \String then result * '' else result export filter = (f, xs) --> f = objToFunc f if typeof! f isnt \Function type = typeof! xs if type is \Object {[key, x] for key, x of xs when f x} else result = [x for x in xs when f x] if
|
|
|
42 |
type is \String then result * '' else result export reject = (f, xs) --> f = objToFunc f if typeof! f isnt \Function type = typeof! xs if type is \Object {[key, x] for key, x of xs when not f x} else result = [x for x in xs when not f x] if
|
|
|
43 |
type is \String then result * '' else result export partition = (f, xs) --> f = objToFunc f if typeof! f isnt \Function type = typeof! xs if type is \Object passed = {} failed = {} for key, x of xs (if f x then passed else failed)[key] = x
|
|
|
44 |
else passed = [] failed = [] for x in xs (if f x then passed else failed)push x if type is \String passed *= '' failed *= '' [passed, failed] export find = (f, xs) --> f = objToFunc f if typeof! f isnt \Function if typeof! xs is \Object for
|
|
|
45 |
, x of xs when f x then return x else for x in xs when f x then return x void export head = export first = (xs) -> return void if not xs.length xs.0 export tail = (xs) -> return void if not xs.length xs.slice 1 export last = (xs) -> return
|
|
|
46 |
void if not xs.length xs[*-1] export initial = (xs) -> return void if not xs.length xs.slice 0 xs.length - 1 export empty = (xs) -> if typeof! xs is \Object for x of xs then return false return yes not xs.length export values = (obj) -> [x
|
|
|
47 |
for , x of obj] export keys = (obj) -> [x for x of obj] export len = (xs) -> xs = values xs if typeof! xs is \Object xs.length export cons = (x, xs) --> if typeof! xs is \String then x + xs else [x] ++ xs export append = (xs, ys) --> if typeof!
|
|
|
48 |
ys is \String then xs + ys else xs ++ ys export join = (sep, xs) --> xs = values xs if typeof! xs is \Object xs.join sep export reverse = (xs) -> if typeof! xs is \String then (xs / '')reverse! * '' else xs.slice!reverse! export fold = export
|
|
|
49 |
foldl = (f, memo, xs) --> if typeof! xs is \Object for , x of xs then memo = f memo, x else for x in xs then memo = f memo, x memo export fold1 = export foldl1 = (f, xs) --> fold f, xs.0, xs.slice 1 export foldr = (f, memo, xs) --> fold f,
|
|
|
50 |
memo, xs.slice!reverse! export foldr1 = (f, xs) --> xs.=slice!reverse! fold f, xs.0, xs.slice 1 export unfoldr = export unfold = (f, b) --> if (f b)? [that.0] ++ unfoldr f, that.1 else [] export andList = (xs) -> for x in xs when not x return
|
|
|
51 |
false true export orList = (xs) -> for x in xs when x return true false export any = (f, xs) --> f = objToFunc f if typeof! f isnt \Function for x in xs when f x return yes no export all = (f, xs) --> f = objToFunc f if typeof! f isnt \Function
|
|
|
52 |
for x in xs when not f x return no yes export unique = (xs) -> result = [] if typeof! xs is \Object for , x of xs when x not in result then result.push x else for x in xs when x not in result then result.push x if typeof! xs is \String then
|
|
|
53 |
result * '' else result export sort = (xs) -> xs.concat!sort (x, y) -> | x > y => 1 | x
|
|
|
54 |
< y=> -1 | _ => 0 export sortBy = (f, xs) --> return [] unless xs.length xs.concat!sort f export compare = (f, x, y) --> | (f x) > (f y) => 1 | (f x)
|
|
|
55 |
< (f y)=> -1 | otherwise => 0 export sum = (xs) -> result = 0 if typeof! xs is \Object for , x of xs then result += x else for x in xs then result += x result export product = (xs) -> result = 1 if typeof! xs is \Object for , x of xs then result
|
|
|
56 |
*= x else for x in xs then result *= x result export mean = export average = (xs) -> (sum xs) / len xs export concat = (xss) -> fold append, [], xss export concatMap = (f, xs) --> fold ((memo, x) -> append memo, f x), [], xs export
|
|
|
57 |
listToObj = (xs) -> {[x.0, x.1] for x in xs} export maximum = (xs) -> fold1 (>?), xs export minimum = (xs) -> fold1 (
|
|
|
58 |
<?), xs
|
|
|
59 |
|
|
|
60 |
export scan = export scanl = (f, memo, xs) -->
|
|
|
61 |
last = memo
|
|
|
62 |
if typeof! xs is \Object
|
|
|
63 |
then [memo] ++ [last = f last, x for , x of xs]
|
|
|
64 |
else [memo] ++ [last = f last, x for x in xs]
|
|
|
65 |
|
|
|
66 |
export scan1 = export scanl1 = (f, xs) --> scan f, xs.0, xs.slice 1
|
|
|
67 |
|
|
|
68 |
export scanr = (f, memo, xs) -->
|
|
|
69 |
xs.=slice!reverse!
|
|
|
70 |
scan f, memo, xs .reverse!
|
|
|
71 |
|
|
|
72 |
export scanr1 = (f, xs) -->
|
|
|
73 |
xs.=slice!reverse!
|
|
|
74 |
scan f, xs.0, xs.slice 1 .reverse!
|
|
|
75 |
|
|
|
76 |
export replicate = (n, x) -->
|
|
|
77 |
result = []
|
|
|
78 |
i = 0
|
|
|
79 |
while i < n, ++i then result.push x
|
|
|
80 |
result
|
|
|
81 |
|
|
|
82 |
export take = (n, xs) -->
|
|
|
83 |
| n <= 0
|
|
|
84 |
if typeof! xs is \String then '' else []
|
|
|
85 |
| not xs.length => xs
|
|
|
86 |
| otherwise => xs.slice 0, n
|
|
|
87 |
|
|
|
88 |
export drop = (n, xs) -->
|
|
|
89 |
| n <= 0 => xs
|
|
|
90 |
| not xs.length => xs
|
|
|
91 |
| otherwise => xs.slice n
|
|
|
92 |
|
|
|
93 |
export splitAt = (n, xs) --> [(take n, xs), (drop n, xs)]
|
|
|
94 |
|
|
|
95 |
export takeWhile = (p, xs) -->
|
|
|
96 |
return xs if not xs.length
|
|
|
97 |
p = objToFunc p if typeof! p isnt \Function
|
|
|
98 |
result = []
|
|
|
99 |
for x in xs
|
|
|
100 |
break if not p x
|
|
|
101 |
result.push x
|
|
|
102 |
if typeof! xs is \String then result * '' else result
|
|
|
103 |
|
|
|
104 |
export dropWhile = (p, xs) -->
|
|
|
105 |
return xs if not xs.length
|
|
|
106 |
p = objToFunc p if typeof! p isnt \Function
|
|
|
107 |
i = 0
|
|
|
108 |
for x in xs
|
|
|
109 |
break if not p x
|
|
|
110 |
++i
|
|
|
111 |
drop i, xs
|
|
|
112 |
|
|
|
113 |
export span = (p, xs) --> [(takeWhile p, xs), (dropWhile p, xs)]
|
|
|
114 |
|
|
|
115 |
export breakIt = (p, xs) --> span (not) << p, xs
|
|
|
116 |
|
|
|
117 |
export zip = (xs, ys) -->
|
|
|
118 |
result = []
|
|
|
119 |
for zs, i in [xs, ys]
|
|
|
120 |
for z, j in zs
|
|
|
121 |
result.push [] if i is 0
|
|
|
122 |
result[j]?push z
|
|
|
123 |
result
|
|
|
124 |
|
|
|
125 |
export zipWith = (f,xs, ys) -->
|
|
|
126 |
f = objToFunc f if typeof! f isnt \Function
|
|
|
127 |
if not xs.length or not ys.length
|
|
|
128 |
[]
|
|
|
129 |
else
|
|
|
130 |
[f.apply this, zs for zs in zip.call this, xs, ys]
|
|
|
131 |
|
|
|
132 |
export zipAll = (...xss) ->
|
|
|
133 |
result = []
|
|
|
134 |
for xs, i in xss
|
|
|
135 |
for x, j in xs
|
|
|
136 |
result.push [] if i is 0
|
|
|
137 |
result[j]?push x
|
|
|
138 |
result
|
|
|
139 |
|
|
|
140 |
export zipAllWith = (f, ...xss) ->
|
|
|
141 |
f = objToFunc f if typeof! f isnt \Function
|
|
|
142 |
if not xss.0.length or not xss.1.length
|
|
|
143 |
[]
|
|
|
144 |
else
|
|
|
145 |
[f.apply this, xs for xs in zipAll.apply this, xss]
|
|
|
146 |
|
|
|
147 |
export compose = (...funcs) ->
|
|
|
148 |
->
|
|
|
149 |
args = arguments
|
|
|
150 |
for f in funcs
|
|
|
151 |
args = [f.apply this, args]
|
|
|
152 |
args.0
|
|
|
153 |
|
|
|
154 |
export curry = (f) ->
|
|
|
155 |
curry$ f # using util method curry$ from livescript
|
|
|
156 |
|
|
|
157 |
export id = (x) -> x
|
|
|
158 |
|
|
|
159 |
export flip = (f, x, y) --> f y, x
|
|
|
160 |
|
|
|
161 |
export fix = (f) ->
|
|
|
162 |
( (g, x) -> -> f(g g) ...arguments ) do
|
|
|
163 |
(g, x) -> -> f(g g) ...arguments
|
|
|
164 |
|
|
|
165 |
export lines = (str) ->
|
|
|
166 |
return [] if not str.length
|
|
|
167 |
str / \\n
|
|
|
168 |
|
|
|
169 |
export unlines = (strs) -> strs * \\n
|
|
|
170 |
|
|
|
171 |
export words = (str) ->
|
|
|
172 |
return [] if not str.length
|
|
|
173 |
str / /[ ]+/
|
|
|
174 |
|
|
|
175 |
export unwords = (strs) -> strs * ' '
|
|
|
176 |
|
|
|
177 |
export max = (>?)
|
|
|
178 |
|
|
|
179 |
export min = (<?)
|
|
|
180 |
|
|
|
181 |
export negate = (x) -> -x
|
|
|
182 |
|
|
|
183 |
export abs = Math.abs
|
|
|
184 |
|
|
|
185 |
export signum = (x) ->
|
|
|
186 |
| x < 0 => -1
|
|
|
187 |
| x > 0 => 1
|
|
|
188 |
| otherwise => 0
|
|
|
189 |
|
|
|
190 |
export quot = (x, y) --> ~~(x / y)
|
|
|
191 |
|
|
|
192 |
export rem = (%)
|
|
|
193 |
|
|
|
194 |
export div = (x, y) --> Math.floor x / y
|
|
|
195 |
|
|
|
196 |
export mod = (%%)
|
|
|
197 |
|
|
|
198 |
export recip = (1 /)
|
|
|
199 |
|
|
|
200 |
export pi = Math.PI
|
|
|
201 |
|
|
|
202 |
export tau = pi * 2
|
|
|
203 |
|
|
|
204 |
export exp = Math.exp
|
|
|
205 |
|
|
|
206 |
export sqrt = Math.sqrt
|
|
|
207 |
|
|
|
208 |
# changed from log as log is a
|
|
|
209 |
# common function for logging things
|
|
|
210 |
export ln = Math.log
|
|
|
211 |
|
|
|
212 |
export pow = (^)
|
|
|
213 |
|
|
|
214 |
export sin = Math.sin
|
|
|
215 |
|
|
|
216 |
export tan = Math.tan
|
|
|
217 |
|
|
|
218 |
export cos = Math.cos
|
|
|
219 |
|
|
|
220 |
export asin = Math.asin
|
|
|
221 |
|
|
|
222 |
export acos = Math.acos
|
|
|
223 |
|
|
|
224 |
export atan = Math.atan
|
|
|
225 |
|
|
|
226 |
export atan2 = (x, y) --> Math.atan2 x, y
|
|
|
227 |
|
|
|
228 |
# sinh
|
|
|
229 |
# tanh
|
|
|
230 |
# cosh
|
|
|
231 |
# asinh
|
|
|
232 |
# atanh
|
|
|
233 |
# acosh
|
|
|
234 |
|
|
|
235 |
export truncate = (x) -> ~~x
|
|
|
236 |
|
|
|
237 |
export round = Math.round
|
|
|
238 |
|
|
|
239 |
export ceiling = Math.ceil
|
|
|
240 |
|
|
|
241 |
export floor = Math.floor
|
|
|
242 |
|
|
|
243 |
export isItNaN = (x) -> x isnt x
|
|
|
244 |
|
|
|
245 |
export even = (x) -> x % 2 == 0
|
|
|
246 |
|
|
|
247 |
export odd = (x) -> x % 2 != 0
|
|
|
248 |
|
|
|
249 |
export gcd = (x, y) -->
|
|
|
250 |
x = Math.abs x
|
|
|
251 |
y = Math.abs y
|
|
|
252 |
until y is 0
|
|
|
253 |
z = x % y
|
|
|
254 |
x = y
|
|
|
255 |
y = z
|
|
|
256 |
x
|
|
|
257 |
|
|
|
258 |
export lcm = (x, y) -->
|
|
|
259 |
Math.abs Math.floor (x / (gcd x, y) * y)
|
|
|
260 |
|
|
|
261 |
# meta
|
|
|
262 |
export installPrelude = !(target) ->
|
|
|
263 |
unless target.prelude?isInstalled
|
|
|
264 |
target <<< out$ # using out$ generated by livescript
|
|
|
265 |
target <<< target.prelude.isInstalled = true
|
|
|
266 |
|
|
|
267 |
export prelude = out$
|
|
|
268 |
</textarea></form>
|
|
|
269 |
<script>
|
|
|
270 |
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
|
|
|
271 |
theme: "solarized light",
|
|
|
272 |
lineNumbers: true
|
|
|
273 |
});
|
|
|
274 |
</script>
|
|
|
275 |
|
|
|
276 |
<p><strong>MIME types defined:</strong> <code>text/x-livescript</code>.</p>
|
|
|
277 |
|
|
|
278 |
<p>The LiveScript mode was written by Kenneth Bentley.</p>
|
|
|
279 |
|
|
|
280 |
</article>
|