Skip to content
stevedonovan edited this page Feb 17, 2013 · 1 revision

This is a small Python-style list class in Moonscript.

We want to wrap existing tables as lists, so this class uses the patch hack to allow the constructor to return a new self.

classListnew:(t)=>if t thenreturn t
append:table.insertjoin:table.concatmap:(f,...)=>List[f x,...for x in*self]-- apply a function on a list in-placeapply:(f,...)=>for i =1,#@ do @[i]= f @[i],...selfclone:=>@slice1slice:(i1,i2=#@)=>-- workaround for MS slice bugif i2 <0then i2 =#@ + i2 +1List[x for x in*self[i1,i2]]extend:(other)=>
i =#self+1for o in*other
self[i]= o
i +=1selfpartition:(pred,...)=>
res ={}for x in*@
k = pred x,...ifnot 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 >=1and k <= n
res[k]\append x
res
__concat:(l1,l2)->List.clone(l1)\extend l2
__tostring:=>
tmp =@slice(1,10)\apply tostringif#@ >10then 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 ListreturnList

Clone this wiki locally