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