Debian Codegraph @

examples/prism-moonscript.html

raw symbols SCIP
1 <h2>Full example</h2>
2 <pre><code>class List
3 new: (t) =>
4 if t then return t
5
6 append: table.insert
7 join: table.concat
8
9 map: (f,...) => List [f x,... for x in *self]
10
11 -- apply a function on a list in-place
12 apply: (f,...) =>
13 for i = 1,#@ do @[i] = f @[i],...
14 self
15
16 clone: => @slice 1
17
18 slice: (i1,i2=#@) =>
19 -- workaround for MS slice bug
20 if i2 &lt; 0 then i2 = #@ + i2 + 1
21 List [x for x in *self[i1,i2]]
22
23 extend: (other) =>
24 i = #self + 1
25 for o in *other
26 self[i] = o
27 i += 1
28 self
29
30 partition: (pred,...) =>
31 res = {}
32 for x in *@
33 k = pred x,...
34 if not res[k] then res[k] = List!
35 res[k]\append x
36 res
37
38 lpartition: (n,npred,...) =>
39 res = List[List{} for i = 1,n]
40 for x in *@
41 k = npred x,...
42 if k >= 1 and k &lt;= n
43 res[k]\append x
44 res
45
46 __concat: (l1,l2) ->
47 List.clone(l1)\extend l2
48
49 __tostring: =>
50 tmp = @slice(1,10)\apply tostring
51 if #@ > 10 then tmp\append '...'
52 "["..tmp\join(',').."]"
53
54 -- hack to modify class so its constructor may return a new self
55 patch = (klass) -> getmetatable(klass).__call = (cls,...) ->
56 self = setmetatable {}, cls.__base
57 newself = cls.__init self, ...
58 if newself
59 self = setmetatable newself, cls.__base
60 self
61
62 patch List
63
64 return List</code></pre>