LiveScript mode

x
 
1
 # 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) -->
2
            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
3
            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
4
            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
5
            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
6
            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
7
            , 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
8
            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
9
            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!
10
            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
11
            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,
12
            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
13
            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
14
            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
15
            result * '' else result export sort = (xs) -> xs.concat!sort (x, y) -> | x > y => 1 | x
16
            < 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)
17
                < (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
18
                    *= 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
19
                    listToObj = (xs) -> {[x.0, x.1] for x in xs} export maximum = (xs) -> fold1 (>?), xs export minimum = (xs) -> fold1 (
20
                    <?), xs
21
22
export scan = export scanl = (f, memo, xs) -->
23
  last = memo
24
  if typeof! xs is \Object
25
  then [memo] ++ [last = f last, x for , x of xs]
26
  else [memo] ++ [last = f last, x for x in xs]
27
28
export scan1 = export scanl1 = (f, xs) --> scan f, xs.0, xs.slice 1
29
30
export scanr = (f, memo, xs) -->
31
  xs.=slice!reverse!
32
  scan f, memo, xs .reverse!
33
34
export scanr1 = (f, xs) -->
35
  xs.=slice!reverse!
36
  scan f, xs.0, xs.slice 1 .reverse!
37
38
export replicate = (n, x) -->
39
  result = []
40
  i = 0
41
  while i < n, ++i then result.push x
42
  result
43
44
export take = (n, xs) -->
45
  | n <= 0
46
    if typeof! xs is \String then '' else []
47
  | not xs.length => xs
48
  | otherwise     => xs.slice 0, n
49
50
export drop = (n, xs) -->
51
  | n <= 0        => xs
52
  | not xs.length => xs
53
  | otherwise     => xs.slice n
54
55
export splitAt = (n, xs) --> [(take n, xs), (drop n, xs)]
56
57
export takeWhile = (p, xs) -->
58
  return xs if not xs.length
59
  p = objToFunc p if typeof! p isnt \Function
60
  result = []
61
  for x in xs
62
    break if not p x
63
    result.push x
64
  if typeof! xs is \String then result * '' else result
65
66
export dropWhile = (p, xs) -->
67
  return xs if not xs.length
68
  p = objToFunc p if typeof! p isnt \Function
69
  i = 0
70
  for x in xs
71
    break if not p x
72
    ++i
73
  drop i, xs
74
75
export span = (p, xs) --> [(takeWhile p, xs), (dropWhile p, xs)]
76
77
export breakIt = (p, xs) --> span (not) << p, xs
78
79
export zip = (xs, ys) -->
80
  result = []
81
  for zs, i in [xs, ys]
82
    for z, j in zs
83
      result.push [] if i is 0
84
      result[j]?push z
85
  result
86
87
export zipWith = (f,xs, ys) -->
88
  f = objToFunc f if typeof! f isnt \Function
89
  if not xs.length or not ys.length
90
    []
91
  else
92
    [f.apply this, zs for zs in zip.call this, xs, ys]
93
94
export zipAll = (...xss) ->
95
  result = []
96
  for xs, i in xss
97
    for x, j in xs
98
      result.push [] if i is 0
99
      result[j]?push x
100
  result
101
102
export zipAllWith = (f, ...xss) ->
103
  f = objToFunc f if typeof! f isnt \Function
104
  if not xss.0.length or not xss.1.length
105
    []
106
  else
107
    [f.apply this, xs for xs in zipAll.apply this, xss]
108
109
export compose = (...funcs) ->
110
  ->
111
    args = arguments
112
    for f in funcs
113
      args = [f.apply this, args]
114
    args.0
115
116
export curry = (f) ->
117
  curry$ f # using util method curry$ from livescript
118
119
export id = (x) -> x
120
121
export flip = (f, x, y) --> f y, x
122
123
export fix = (f) ->
124
  ( (g, x) -> -> f(g g) ...arguments ) do
125
    (g, x) -> -> f(g g) ...arguments
126
127
export lines = (str) ->
128
  return [] if not str.length
129
  str / \\n
130
131
export unlines = (strs) -> strs * \\n
132
133
export words = (str) ->
134
  return [] if not str.length
135
  str / /[ ]+/
136
137
export unwords = (strs) -> strs * ' '
138
139
export max = (>?)
140
141
export min = (<?)
142
143
export negate = (x) -> -x
144
145
export abs = Math.abs
146
147
export signum = (x) ->
148
  | x < 0     => -1
149
  | x > 0     =>  1
150
  | otherwise =>  0
151
152
export quot = (x, y) --> ~~(x / y)
153
154
export rem = (%)
155
156
export div = (x, y) --> Math.floor x / y
157
158
export mod = (%%)
159
160
export recip = (1 /)
161
162
export pi = Math.PI
163
164
export tau = pi * 2
165
166
export exp = Math.exp
167
168
export sqrt = Math.sqrt
169
170
# changed from log as log is a
171
# common function for logging things
172
export ln = Math.log
173
174
export pow = (^)
175
176
export sin = Math.sin
177
178
export tan = Math.tan
179
180
export cos = Math.cos
181
182
export asin = Math.asin
183
184
export acos = Math.acos
185
186
export atan = Math.atan
187
188
export atan2 = (x, y) --> Math.atan2 x, y
189
190
# sinh
191
# tanh
192
# cosh
193
# asinh
194
# atanh
195
# acosh
196
197
export truncate = (x) -> ~~x
198
199
export round = Math.round
200
201
export ceiling = Math.ceil
202
203
export floor = Math.floor
204
205
export isItNaN = (x) -> x isnt x
206
207
export even = (x) -> x % 2 == 0
208
209
export odd = (x) -> x % 2 != 0
210
211
export gcd = (x, y) -->
212
  x = Math.abs x
213
  y = Math.abs y
214
  until y is 0
215
    z = x % y
216
    x = y
217
    y = z
218
  x
219
220
export lcm = (x, y) -->
221
  Math.abs Math.floor (x / (gcd x, y) * y)
222
223
# meta
224
export installPrelude = !(target) ->
225
  unless target.prelude?isInstalled
226
    target <<< out$ # using out$ generated by livescript
227
    target <<< target.prelude.isInstalled = true
228
229
export prelude = out$
230

MIME types defined: text/x-livescript.

The LiveScript mode was written by Kenneth Bentley.