LiveScript mode

x
 
1
# LiveScript mode for CodeMirror
2
# The following script, prelude.ls, is used to
3
# demonstrate LiveScript mode for CodeMirror.
4
#   https://github.com/gkz/prelude-ls
5
6
export objToFunc = objToFunc = (obj) ->
7
  (key) -> obj[key]
8
9
export each = (f, xs) -->
10
  if typeof! xs is \Object
11
    for , x of xs then f x
12
  else
13
    for x in xs then f x
14
  xs
15
16
export map = (f, xs) -->
17
  f = objToFunc f if typeof! f isnt \Function
18
  type = typeof! xs
19
  if type is \Object
20
    {[key, f x] for key, x of xs}
21
  else
22
    result = [f x for x in xs]
23
    if type is \String then result * '' else result
24
25
export filter = (f, xs) -->
26
  f = objToFunc f if typeof! f isnt \Function
27
  type = typeof! xs
28
  if type is \Object
29
    {[key, x] for key, x of xs when f x}
30
  else
31
    result = [x for x in xs when f x]
32
    if type is \String then result * '' else result
33
34
export reject = (f, xs) -->
35
  f = objToFunc f if typeof! f isnt \Function
36
  type = typeof! xs
37
  if type is \Object
38
    {[key, x] for key, x of xs when not f x}
39
  else
40
    result = [x for x in xs when not f x]
41
    if type is \String then result * '' else result
42
43
export partition = (f, xs) -->
44
  f = objToFunc f if typeof! f isnt \Function
45
  type = typeof! xs
46
  if type is \Object
47
    passed = {}
48
    failed = {}
49
    for key, x of xs
50
      (if f x then passed else failed)[key] = x
51
  else
52
    passed = []
53
    failed = []
54
    for x in xs
55
      (if f x then passed else failed)push x
56
    if type is \String
57
      passed *= ''
58
      failed *= ''
59
  [passed, failed]
60
61
export find = (f, xs) -->
62
  f = objToFunc f if typeof! f isnt \Function
63
  if typeof! xs is \Object
64
    for , x of xs when f x then return x
65
  else
66
    for x in xs when f x then return x
67
  void
68
69
export head = export first = (xs) ->
70
  return void if not xs.length
71
  xs.0
72
73
export tail = (xs) ->
74
  return void if not xs.length
75
  xs.slice 1
76
77
export last = (xs) ->
78
  return void if not xs.length
79
  xs[*-1]
80
81
export initial = (xs) ->
82
  return void if not xs.length
83
  xs.slice 0 xs.length - 1
84
85
export empty = (xs) ->
86
  if typeof! xs is \Object
87
    for x of xs then return false
88
    return yes
89
  not xs.length
90
91
export values = (obj) ->
92
  [x for , x of obj]
93
94
export keys = (obj) ->
95
  [x for x of obj]
96
97
export len = (xs) ->
98
  xs = values xs if typeof! xs is \Object
99
  xs.length
100
101
export cons = (x, xs) -->
102
  if typeof! xs is \String then x + xs else [x] ++ xs
103
104
export append = (xs, ys) -->
105
  if typeof! ys is \String then xs + ys else xs ++ ys
106
107
export join = (sep, xs) -->
108
  xs = values xs if typeof! xs is \Object
109
  xs.join sep
110
111
export reverse = (xs) ->
112
  if typeof! xs is \String
113
  then (xs / '')reverse! * ''
114
  else xs.slice!reverse!
115
116
export fold = export foldl = (f, memo, xs) -->
117
  if typeof! xs is \Object
118
    for , x of xs then memo = f memo, x
119
  else
120
    for x in xs then memo = f memo, x
121
  memo
122
123
export fold1 = export foldl1 = (f, xs) --> fold f, xs.0, xs.slice 1
124
125
export foldr = (f, memo, xs) --> fold f, memo, xs.slice!reverse!
126
127
export foldr1 = (f, xs) -->
128
  xs.=slice!reverse!
129
  fold f, xs.0, xs.slice 1
130
131
export unfoldr = export unfold = (f, b) -->
132
  if (f b)?
133
    [that.0] ++ unfoldr f, that.1
134
  else
135
    []
136
137
export andList = (xs) ->
138
  for x in xs when not x
139
    return false
140
  true
141
142
export orList = (xs) ->
143
  for x in xs when x
144
    return true
145
  false
146
147
export any = (f, xs) -->
148
  f = objToFunc f if typeof! f isnt \Function
149
  for x in xs when f x
150
    return yes
151
  no
152
153
export all = (f, xs) -->
154
  f = objToFunc f if typeof! f isnt \Function
155
  for x in xs when not f x
156
    return no
157
  yes
158
159
export unique = (xs) ->
160
  result = []
161
  if typeof! xs is \Object
162
    for , x of xs when x not in result then result.push x
163
  else
164
    for x   in xs when x not in result then result.push x
165
  if typeof! xs is \String then result * '' else result
166
167
export sort = (xs) ->
168
  xs.concat!sort (x, y) ->
169
    | x > y =>  1
170
    | x < y => -1
171
    | _     =>  0
172
173
export sortBy = (f, xs) -->
174
  return [] unless xs.length
175
  xs.concat!sort f
176
177
export compare = (f, x, y) -->
178
  | (f x) > (f y) =>  1
179
  | (f x) < (f y) => -1
180
  | otherwise     =>  0
181
182
export sum = (xs) ->
183
  result = 0
184
  if typeof! xs is \Object
185
    for , x of xs then result += x
186
  else
187
    for x   in xs then result += x
188
  result
189
190
export product = (xs) ->
191
  result = 1
192
  if typeof! xs is \Object
193
    for , x of xs then result *= x
194
  else
195
    for x   in xs then result *= x
196
  result
197
198
export mean = export average = (xs) -> (sum xs) / len xs
199
200
export concat = (xss) -> fold append, [], xss
201
202
export concatMap = (f, xs) --> fold ((memo, x) -> append memo, f x), [], xs
203
204
export listToObj = (xs) ->
205
  {[x.0, x.1] for x in xs}
206
207
export maximum = (xs) -> fold1 (>?), xs
208
209
export minimum = (xs) -> fold1 (<?), xs
210
211
export scan = export scanl = (f, memo, xs) -->
212
  last = memo
213
  if typeof! xs is \Object
214
  then [memo] ++ [last = f last, x for , x of xs]
215
  else [memo] ++ [last = f last, x for x in xs]
216
217
export scan1 = export scanl1 = (f, xs) --> scan f, xs.0, xs.slice 1
218
219
export scanr = (f, memo, xs) -->
220
  xs.=slice!reverse!
221
  scan f, memo, xs .reverse!
222
223
export scanr1 = (f, xs) -->
224
  xs.=slice!reverse!
225
  scan f, xs.0, xs.slice 1 .reverse!
226
227
export replicate = (n, x) -->
228
  result = []
229
  i = 0
230
  while i < n, ++i then result.push x
231
  result
232
233
export take = (n, xs) -->
234
  | n <= 0
235
    if typeof! xs is \String then '' else []
236
  | not xs.length => xs
237
  | otherwise     => xs.slice 0, n
238
239
export drop = (n, xs) -->
240
  | n <= 0        => xs
241
  | not xs.length => xs
242
  | otherwise     => xs.slice n
243
244
export splitAt = (n, xs) --> [(take n, xs), (drop n, xs)]
245
246
export takeWhile = (p, xs) -->
247
  return xs if not xs.length
248
  p = objToFunc p if typeof! p isnt \Function
249
  result = []
250
  for x in xs
251
    break if not p x
252
    result.push x
253
  if typeof! xs is \String then result * '' else result
254
255
export dropWhile = (p, xs) -->
256
  return xs if not xs.length
257
  p = objToFunc p if typeof! p isnt \Function
258
  i = 0
259
  for x in xs
260
    break if not p x
261
    ++i
262
  drop i, xs
263
264
export span = (p, xs) --> [(takeWhile p, xs), (dropWhile p, xs)]
265
266
export breakIt = (p, xs) --> span (not) << p, xs
267
268
export zip = (xs, ys) -->
269
  result = []
270
  for zs, i in [xs, ys]
271
    for z, j in zs
272
      result.push [] if i is 0
273
      result[j]?push z
274
  result
275
276
export zipWith = (f,xs, ys) -->
277
  f = objToFunc f if typeof! f isnt \Function
278
  if not xs.length or not ys.length
279
    []
280
  else
281
    [f.apply this, zs for zs in zip.call this, xs, ys]
282
283
export zipAll = (...xss) ->
284
  result = []
285
  for xs, i in xss
286
    for x, j in xs
287
      result.push [] if i is 0
288
      result[j]?push x
289
  result
290
291
export zipAllWith = (f, ...xss) ->
292
  f = objToFunc f if typeof! f isnt \Function
293
  if not xss.0.length or not xss.1.length
294
    []
295
  else
296
    [f.apply this, xs for xs in zipAll.apply this, xss]
297
298
export compose = (...funcs) ->
299
  ->
300
    args = arguments
301
    for f in funcs
302
      args = [f.apply this, args]
303
    args.0
304
305
export curry = (f) ->
306
  curry$ f # using util method curry$ from livescript
307
308
export id = (x) -> x
309
310
export flip = (f, x, y) --> f y, x
311
312
export fix = (f) ->
313
  ( (g, x) -> -> f(g g) ...arguments ) do
314
    (g, x) -> -> f(g g) ...arguments
315
316
export lines = (str) ->
317
  return [] if not str.length
318
  str / \\n
319
320
export unlines = (strs) -> strs * \\n
321
322
export words = (str) ->
323
  return [] if not str.length
324
  str / /[ ]+/
325
326
export unwords = (strs) -> strs * ' '
327
328
export max = (>?)
329
330
export min = (<?)
331
332
export negate = (x) -> -x
333
334
export abs = Math.abs
335
336
export signum = (x) ->
337
  | x < 0     => -1
338
  | x > 0     =>  1
339
  | otherwise =>  0
340
341
export quot = (x, y) --> ~~(x / y)
342
343
export rem = (%)
344
345
export div = (x, y) --> Math.floor x / y
346
347
export mod = (%%)
348
349
export recip = (1 /)
350
351
export pi = Math.PI
352
353
export tau = pi * 2
354
355
export exp = Math.exp
356
357
export sqrt = Math.sqrt
358
359
# changed from log as log is a
360
# common function for logging things
361
export ln = Math.log
362
363
export pow = (^)
364
365
export sin = Math.sin
366
367
export tan = Math.tan
368
369
export cos = Math.cos
370
371
export asin = Math.asin
372
373
export acos = Math.acos
374
375
export atan = Math.atan
376
377
export atan2 = (x, y) --> Math.atan2 x, y
378
379
# sinh
380
# tanh
381
# cosh
382
# asinh
383
# atanh
384
# acosh
385
386
export truncate = (x) -> ~~x
387
388
export round = Math.round
389
390
export ceiling = Math.ceil
391
392
export floor = Math.floor
393
394
export isItNaN = (x) -> x isnt x
395
396
export even = (x) -> x % 2 == 0
397
398
export odd = (x) -> x % 2 != 0
399
400
export gcd = (x, y) -->
401
  x = Math.abs x
402
  y = Math.abs y
403
  until y is 0
404
    z = x % y
405
    x = y
406
    y = z
407
  x
408
409
export lcm = (x, y) -->
410
  Math.abs Math.floor (x / (gcd x, y) * y)
411
412
# meta
413
export installPrelude = !(target) ->
414
  unless target.prelude?isInstalled
415
    target <<< out$ # using out$ generated by livescript
416
    target <<< target.prelude.isInstalled = true
417
418
export prelude = out$
419

MIME types defined: text/x-livescript.

The LiveScript mode was written by Kenneth Bentley.