4 <title>CodeMirror: LiveScript mode</title>
5 <link rel="stylesheet" href="../../lib/codemirror.css">
6 <script src="../../lib/codemirror.js"></script>
7 <script src="livescript.js"></script>
8 <style>.CodeMirror {font-size: 80%;border-top: 1px solid silver; border-bottom: 1px solid silver;}</style>
9 <link rel="stylesheet" href="../../doc/docs.css">
10 <link rel="stylesheet" href="../../theme/solarized.css">
13 <h1>CodeMirror: LiveScript mode</h1>
14 <form><textarea id="code" name="code">
15 # LiveScript mode for CodeMirror
16 # The following script, prelude.ls, is used to
17 # demonstrate LiveScript mode for CodeMirror.
18 # https://github.com/gkz/prelude-ls
20 export objToFunc = objToFunc = (obj) ->
23 export each = (f, xs) -->
24 if typeof! xs is \Object
25 for , x of xs then f x
30 export map = (f, xs) -->
31 f = objToFunc f if typeof! f isnt \Function
34 {[key, f x] for key, x of xs}
36 result = [f x for x in xs]
37 if type is \String then result * '' else result
39 export filter = (f, xs) -->
40 f = objToFunc f if typeof! f isnt \Function
43 {[key, x] for key, x of xs when f x}
45 result = [x for x in xs when f x]
46 if type is \String then result * '' else result
48 export reject = (f, xs) -->
49 f = objToFunc f if typeof! f isnt \Function
52 {[key, x] for key, x of xs when not f x}
54 result = [x for x in xs when not f x]
55 if type is \String then result * '' else result
57 export partition = (f, xs) -->
58 f = objToFunc f if typeof! f isnt \Function
64 (if f x then passed else failed)[key] = x
69 (if f x then passed else failed)push x
75 export find = (f, xs) -->
76 f = objToFunc f if typeof! f isnt \Function
77 if typeof! xs is \Object
78 for , x of xs when f x then return x
80 for x in xs when f x then return x
83 export head = export first = (xs) ->
84 return void if not xs.length
88 return void if not xs.length
92 return void if not xs.length
95 export initial = (xs) ->
96 return void if not xs.length
97 xs.slice 0 xs.length - 1
99 export empty = (xs) ->
100 if typeof! xs is \Object
101 for x of xs then return false
105 export values = (obj) ->
108 export keys = (obj) ->
112 xs = values xs if typeof! xs is \Object
115 export cons = (x, xs) -->
116 if typeof! xs is \String then x + xs else [x] ++ xs
118 export append = (xs, ys) -->
119 if typeof! ys is \String then xs + ys else xs ++ ys
121 export join = (sep, xs) -->
122 xs = values xs if typeof! xs is \Object
125 export reverse = (xs) ->
126 if typeof! xs is \String
127 then (xs / '')reverse! * ''
128 else xs.slice!reverse!
130 export fold = export foldl = (f, memo, xs) -->
131 if typeof! xs is \Object
132 for , x of xs then memo = f memo, x
134 for x in xs then memo = f memo, x
137 export fold1 = export foldl1 = (f, xs) --> fold f, xs.0, xs.slice 1
139 export foldr = (f, memo, xs) --> fold f, memo, xs.slice!reverse!
141 export foldr1 = (f, xs) -->
143 fold f, xs.0, xs.slice 1
145 export unfoldr = export unfold = (f, b) -->
147 [that.0] ++ unfoldr f, that.1
151 export andList = (xs) ->
152 for x in xs when not x
156 export orList = (xs) ->
161 export any = (f, xs) -->
162 f = objToFunc f if typeof! f isnt \Function
167 export all = (f, xs) -->
168 f = objToFunc f if typeof! f isnt \Function
169 for x in xs when not f x
173 export unique = (xs) ->
175 if typeof! xs is \Object
176 for , x of xs when x not in result then result.push x
178 for x in xs when x not in result then result.push x
179 if typeof! xs is \String then result * '' else result
181 export sort = (xs) ->
182 xs.concat!sort (x, y) ->
187 export sortBy = (f, xs) -->
188 return [] unless xs.length
191 export compare = (f, x, y) -->
193 | (f x) < (f y) => -1
198 if typeof! xs is \Object
199 for , x of xs then result += x
201 for x in xs then result += x
204 export product = (xs) ->
206 if typeof! xs is \Object
207 for , x of xs then result *= x
209 for x in xs then result *= x
212 export mean = export average = (xs) -> (sum xs) / len xs
214 export concat = (xss) -> fold append, [], xss
216 export concatMap = (f, xs) --> fold ((memo, x) -> append memo, f x), [], xs
218 export listToObj = (xs) ->
219 {[x.0, x.1] for x in xs}
221 export maximum = (xs) -> fold1 (>?), xs
223 export minimum = (xs) -> fold1 (<?), xs
225 export scan = export scanl = (f, memo, xs) -->
227 if typeof! xs is \Object
228 then [memo] ++ [last = f last, x for , x of xs]
229 else [memo] ++ [last = f last, x for x in xs]
231 export scan1 = export scanl1 = (f, xs) --> scan f, xs.0, xs.slice 1
233 export scanr = (f, memo, xs) -->
235 scan f, memo, xs .reverse!
237 export scanr1 = (f, xs) -->
239 scan f, xs.0, xs.slice 1 .reverse!
241 export replicate = (n, x) -->
244 while i < n, ++i then result.push x
247 export take = (n, xs) -->
249 if typeof! xs is \String then '' else []
250 | not xs.length => xs
251 | otherwise => xs.slice 0, n
253 export drop = (n, xs) -->
255 | not xs.length => xs
256 | otherwise => xs.slice n
258 export splitAt = (n, xs) --> [(take n, xs), (drop n, xs)]
260 export takeWhile = (p, xs) -->
261 return xs if not xs.length
262 p = objToFunc p if typeof! p isnt \Function
267 if typeof! xs is \String then result * '' else result
269 export dropWhile = (p, xs) -->
270 return xs if not xs.length
271 p = objToFunc p if typeof! p isnt \Function
278 export span = (p, xs) --> [(takeWhile p, xs), (dropWhile p, xs)]
280 export breakIt = (p, xs) --> span (not) << p, xs
282 export zip = (xs, ys) -->
284 for zs, i in [xs, ys]
286 result.push [] if i is 0
290 export zipWith = (f,xs, ys) -->
291 f = objToFunc f if typeof! f isnt \Function
292 if not xs.length or not ys.length
295 [f.apply this, zs for zs in zip.call this, xs, ys]
297 export zipAll = (...xss) ->
301 result.push [] if i is 0
305 export zipAllWith = (f, ...xss) ->
306 f = objToFunc f if typeof! f isnt \Function
307 if not xss.0.length or not xss.1.length
310 [f.apply this, xs for xs in zipAll.apply this, xss]
312 export compose = (...funcs) ->
316 args = [f.apply this, args]
319 export curry = (f) ->
320 curry$ f # using util method curry$ from livescript
324 export flip = (f, x, y) --> f y, x
327 ( (g, x) -> -> f(g g) ...arguments ) do
328 (g, x) -> -> f(g g) ...arguments
330 export lines = (str) ->
331 return [] if not str.length
334 export unlines = (strs) -> strs * \\n
336 export words = (str) ->
337 return [] if not str.length
340 export unwords = (strs) -> strs * ' '
346 export negate = (x) -> -x
348 export abs = Math.abs
350 export signum = (x) ->
355 export quot = (x, y) --> ~~(x / y)
359 export div = (x, y) --> Math.floor x / y
369 export exp = Math.exp
371 export sqrt = Math.sqrt
373 # changed from log as log is a
374 # common function for logging things
379 export sin = Math.sin
381 export tan = Math.tan
383 export cos = Math.cos
385 export asin = Math.asin
387 export acos = Math.acos
389 export atan = Math.atan
391 export atan2 = (x, y) --> Math.atan2 x, y
400 export truncate = (x) -> ~~x
402 export round = Math.round
404 export ceiling = Math.ceil
406 export floor = Math.floor
408 export isItNaN = (x) -> x isnt x
410 export even = (x) -> x % 2 == 0
412 export odd = (x) -> x % 2 != 0
414 export gcd = (x, y) -->
423 export lcm = (x, y) -->
424 Math.abs Math.floor (x / (gcd x, y) * y)
427 export installPrelude = !(target) ->
428 unless target.prelude?isInstalled
429 target <<< out$ # using out$ generated by livescript
430 target <<< target.prelude.isInstalled = true
432 export prelude = out$
435 var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
436 theme: "solarized light",
441 <p><strong>MIME types defined:</strong> <code>text/x-livescript</code>.</p>
443 <p>The LiveScript mode was written by Kenneth Bentley (<a href="LICENSE">license</a>).</p>