2
0

index.html 9.3 KB

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