index.html 9.6 KB

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