Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
An example of a MoonScript class
import insert,concat,remove from table
class List
new: (t) =>
@ls = t or {}
add: (item) =>
insert @ls,item
insert: (idx,item) =>
insert @ls,idx,item
remove: (idx) => remove(@ls,idx)
len: => #@ls
__tostring: => '['..(concat @ls,',')..']'
find: (item) =>
for i = 1,#@ls
return i if @ls[i] == item
remove_value: (item) =>
idx = self\find item
self\remove idx if idx
remove_values: (items) =>
for item in *items do self\remove_value(item)
index_by: (indexes) =>
List [@ls[idx] for idx in *indexes]
slice: (i1,i2) =>
List [v for v in *@ls[i1,i2]]
copy: => List [v for v in *@ls]
extend: (list) =>
other = if list.__class == List then list.ls else list
for v in *other do self\add v
self
iter: =>
i,t,n = 0,@ls,#@ls
->
i += 1
if i <= n then t[i]
__concat: (l1,l2) -> l1\copy!\extend l2
__eq: (l1,l2) ->
return false if l1\len! != l2\len!
l1,l2 = l1.ls, l2.ls
for i = 1,#l1
return false if l1[i] != l2[i]
return true
ls = List!
ls\add 10
ls\add 20
ls\remove_value 10
ls\remove_value 15
ls\add 30
ls\remove 1
for v in *{1,2,3} do ls\add v
print ls, ls\len!
print ls\index_by {2,3}
for v in ls\iter!
print v
print (List{'a','b'})..(List{'A','B'})
ls = List {1,2}
add = ls\add
add 3
add 4
assert ls == List {1,2,3,4}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment