| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 | <!doctype html><html>  <head>    <title>CodeMirror: LiveScript mode</title>    <link rel="stylesheet" href="../../lib/codemirror.css">    <script src="../../lib/codemirror.js"></script>    <script src="livescript.js"></script>    <style>.CodeMirror {font-size: 80%;border-top: 1px solid silver; border-bottom: 1px solid silver;}</style>    <link rel="stylesheet" href="../../doc/docs.css">    <link rel="stylesheet" href="../../theme/solarized.css">  </head>  <body>    <h1>CodeMirror: LiveScript mode</h1>    <form><textarea id="code" name="code"># LiveScript mode for CodeMirror# The following script, prelude.ls, is used to# demonstrate LiveScript mode for CodeMirror.#   https://github.com/gkz/prelude-lsexport objToFunc = objToFunc = (obj) ->  (key) -> obj[key]export each = (f, xs) -->  if typeof! xs is \Object    for , x of xs then f x  else    for x in xs then f x  xsexport 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 xs]    if type is \String then result * '' else resultexport 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 type is \String then result * '' else resultexport 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 type is \String then result * '' else resultexport 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  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 , x of xs when f x then return x  else    for x in xs when f x then return x  voidexport head = export first = (xs) ->  return void if not xs.length  xs.0export tail = (xs) ->  return void if not xs.length  xs.slice 1export last = (xs) ->  return void if not xs.length  xs[*-1]export initial = (xs) ->  return void if not xs.length  xs.slice 0 xs.length - 1export empty = (xs) ->  if typeof! xs is \Object    for x of xs then return false    return yes  not xs.lengthexport values = (obj) ->  [x for , x of obj]export keys = (obj) ->  [x for x of obj]export len = (xs) ->  xs = values xs if typeof! xs is \Object  xs.lengthexport cons = (x, xs) -->  if typeof! xs is \String then x + xs else [x] ++ xsexport append = (xs, ys) -->  if typeof! ys is \String then xs + ys else xs ++ ysexport join = (sep, xs) -->  xs = values xs if typeof! xs is \Object  xs.join sepexport reverse = (xs) ->  if typeof! xs is \String  then (xs / '')reverse! * ''  else xs.slice!reverse!export fold = export 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  memoexport fold1 = export foldl1 = (f, xs) --> fold f, xs.0, xs.slice 1export foldr = (f, memo, xs) --> fold f, memo, xs.slice!reverse!export foldr1 = (f, xs) -->  xs.=slice!reverse!  fold f, xs.0, xs.slice 1export 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 false  trueexport orList = (xs) ->  for x in xs when x    return true  falseexport any = (f, xs) -->  f = objToFunc f if typeof! f isnt \Function  for x in xs when f x    return yes  noexport all = (f, xs) -->  f = objToFunc f if typeof! f isnt \Function  for x in xs when not f x    return no  yesexport 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 result * '' else resultexport sort = (xs) ->  xs.concat!sort (x, y) ->    | x > y =>  1    | x < y => -1    | _     =>  0export sortBy = (f, xs) -->  return [] unless xs.length  xs.concat!sort fexport compare = (f, x, y) -->  | (f x) > (f y) =>  1  | (f x) < (f y) => -1  | otherwise     =>  0export sum = (xs) ->  result = 0  if typeof! xs is \Object    for , x of xs then result += x  else    for x   in xs then result += x  resultexport product = (xs) ->  result = 1  if typeof! xs is \Object    for , x of xs then result *= x  else    for x   in xs then result *= x  resultexport mean = export average = (xs) -> (sum xs) / len xsexport concat = (xss) -> fold append, [], xssexport concatMap = (f, xs) --> fold ((memo, x) -> append memo, f x), [], xsexport listToObj = (xs) ->  {[x.0, x.1] for x in xs}export maximum = (xs) -> fold1 (>?), xsexport minimum = (xs) -> fold1 (<?), xsexport scan = export scanl = (f, memo, xs) -->  last = memo  if typeof! xs is \Object  then [memo] ++ [last = f last, x for , x of xs]  else [memo] ++ [last = f last, x for x in xs]export scan1 = export scanl1 = (f, xs) --> scan f, xs.0, xs.slice 1export scanr = (f, memo, xs) -->  xs.=slice!reverse!  scan f, memo, xs .reverse!export scanr1 = (f, xs) -->  xs.=slice!reverse!  scan f, xs.0, xs.slice 1 .reverse!export replicate = (n, x) -->  result = []  i = 0  while i < n, ++i then result.push x  resultexport take = (n, xs) -->  | n <= 0    if typeof! xs is \String then '' else []  | not xs.length => xs  | otherwise     => xs.slice 0, nexport drop = (n, xs) -->  | n <= 0        => xs  | not xs.length => xs  | otherwise     => xs.slice nexport splitAt = (n, xs) --> [(take n, xs), (drop n, xs)]export takeWhile = (p, xs) -->  return xs if not xs.length  p = objToFunc p if typeof! p isnt \Function  result = []  for x in xs    break if not p x    result.push x  if typeof! xs is \String then result * '' else resultexport dropWhile = (p, xs) -->  return xs if not xs.length  p = objToFunc p if typeof! p isnt \Function  i = 0  for x in xs    break if not p x    ++i  drop i, xsexport span = (p, xs) --> [(takeWhile p, xs), (dropWhile p, xs)]export breakIt = (p, xs) --> span (not) << p, xsexport zip = (xs, ys) -->  result = []  for zs, i in [xs, ys]    for z, j in zs      result.push [] if i is 0      result[j]?push z  resultexport zipWith = (f,xs, ys) -->  f = objToFunc f if typeof! f isnt \Function  if not xs.length or not ys.length    []  else    [f.apply this, zs for zs in zip.call this, xs, ys]export zipAll = (...xss) ->  result = []  for xs, i in xss    for x, j in xs      result.push [] if i is 0      result[j]?push x  resultexport zipAllWith = (f, ...xss) ->  f = objToFunc f if typeof! f isnt \Function  if not xss.0.length or not xss.1.length    []  else    [f.apply this, xs for xs in zipAll.apply this, xss]export compose = (...funcs) ->  ->    args = arguments    for f in funcs      args = [f.apply this, args]    args.0export curry = (f) ->  curry$ f # using util method curry$ from livescriptexport id = (x) -> xexport flip = (f, x, y) --> f y, xexport fix = (f) ->  ( (g, x) -> -> f(g g) ...arguments ) do    (g, x) -> -> f(g g) ...argumentsexport lines = (str) ->  return [] if not str.length  str / \\nexport unlines = (strs) -> strs * \\nexport words = (str) ->  return [] if not str.length  str / /[ ]+/export unwords = (strs) -> strs * ' 'export max = (>?)export min = (<?)export negate = (x) -> -xexport abs = Math.absexport signum = (x) ->  | x < 0     => -1  | x > 0     =>  1  | otherwise =>  0export quot = (x, y) --> ~~(x / y)export rem = (%)export div = (x, y) --> Math.floor x / yexport mod = (%%)export recip = (1 /)export pi = Math.PIexport tau = pi * 2export exp = Math.expexport sqrt = Math.sqrt# changed from log as log is a# common function for logging thingsexport ln = Math.logexport pow = (^)export sin = Math.sinexport tan = Math.tanexport cos = Math.cosexport asin = Math.asinexport acos = Math.acosexport atan = Math.atanexport atan2 = (x, y) --> Math.atan2 x, y# sinh# tanh# cosh# asinh# atanh# acoshexport truncate = (x) -> ~~xexport round = Math.roundexport ceiling = Math.ceilexport floor = Math.floorexport isItNaN = (x) -> x isnt xexport even = (x) -> x % 2 == 0export odd = (x) -> x % 2 != 0export gcd = (x, y) -->  x = Math.abs x  y = Math.abs y  until y is 0    z = x % y    x = y    y = z  xexport lcm = (x, y) -->  Math.abs Math.floor (x / (gcd x, y) * y)# metaexport installPrelude = !(target) ->  unless target.prelude?isInstalled    target <<< out$ # using out$ generated by livescript    target <<< target.prelude.isInstalled = trueexport prelude = out$</textarea></form>    <script>      var editor = CodeMirror.fromTextArea(document.getElementById("code"), {        theme: "solarized light",        lineNumbers: true      });    </script>    <p><strong>MIME types defined:</strong> <code>text/x-livescript</code>.</p>    <p>The LiveScript mode was written by Kenneth Bentley (<a href="LICENSE">license</a>).</p>  </body></html>
 |