Created
August 10, 2013 12:35
-
-
Save stevedonovan/6200264 to your computer and use it in GitHub Desktop.
A Moonscript class, documentation by LDoc,
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
---- | |
-- A list class that wraps a table | |
-- @classmod List | |
import insert,concat,remove from table | |
class List | |
--- constructor passed a table `t`, which can be `nil`. | |
new: (t) => | |
@ls = t or {} | |
--- append to list. | |
add: (item) => | |
insert @ls,item | |
--- insert `item` at `idx` | |
insert: (idx,item) => | |
insert @ls,idx,item | |
--- remove item at `idx` | |
remove: (idx) => remove @ls,idx | |
--- length of list | |
len: => #@ls | |
--- string representation | |
-- @within Metamethods | |
__tostring: => '['..(concat @ls,',')..']' | |
--- return idx of first occurence of `item` | |
find: (item) => | |
for i = 1,#@ls | |
if @ls[i] == item then return i | |
--- remove item by value | |
remove_value: (item) => | |
idx = self\find item | |
self\remove idx if idx | |
--- remove a list of items | |
remove_values: (items) => | |
for item in *items do self\remove_value item | |
--- create a sublist of items indexed by a table `indexes` | |
index_by: (indexes) => | |
List [@ls[idx] for idx in *indexes] | |
--- make a copy of this list | |
copy: => List [v for v in *@ls] | |
--- append items from the table or list `list` | |
extend: (list) => | |
other = if list.__class == List then list.ls else list | |
for v in *other do self\add v | |
self | |
--- concatenate two lists, giving a new list | |
-- @within Metamethods | |
__concat: (l1,l2) -> l1\copy!\extend l2 | |
--- an iterator over all items | |
iter: => | |
i,t,n = 0,@ls,#@ls | |
-> | |
i += 1 | |
if i <= n then t[i] | |
return List |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment