diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2019-01-30 15:19:38 -0800 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2019-01-30 15:19:43 -0800 |
| commit | b714a55ec09fa4c7b2053e5b9a29ad7d69b2486a (patch) | |
| tree | bd98dc4b7b0c19bbfcdac65dcd81d923e4e5694e | |
| parent | bc41cc3a244bf47679d1c47d17006a2aa7356bc8 (diff) | |
Added 'reversed' method for lists.
| -rw-r--r-- | containers.lua | 11 | ||||
| -rw-r--r-- | containers.moon | 3 |
2 files changed, 14 insertions, 0 deletions
diff --git a/containers.lua b/containers.lua index 8f34e73..e706831 100644 --- a/containers.lua +++ b/containers.lua @@ -225,6 +225,17 @@ local _list_mt = { end return _accum_0 end)()) + end, + reversed = function(self) + return List((function() + local _accum_0 = { } + local _len_0 = 1 + for i = #self, 1, -1 do + _accum_0[_len_0] = self[i] + _len_0 = _len_0 + 1 + end + return _accum_0 + end)()) end }, __newindex = function(self, k, v) diff --git a/containers.moon b/containers.moon index 2a9ee4b..6fa9ecb 100644 --- a/containers.moon +++ b/containers.moon @@ -88,6 +88,9 @@ _list_mt = start = (n+1-start) if start < 0 stop = (n+1-stop) if stop < 0 return List[@[i] for i=start,stop] + + reversed: => List[@[i] for i=#@,1,-1] + -- TODO: remove this safety check to get better performance? __newindex: (k,v)=> assert type(k) == 'number', "List indices must be numbers" |
