Open main menu
Posts
Gists
Guilds
Users
Decipher
Docs
Open user menu
Log in
Sign up
Create a new gist
Posts
Gists
Guilds
Users
Decipher
Docs
Files
listLib.src
listLib.src
map.to_list = function(shallow = false)
list = []
for i in self.indexes
if typeof(self[i]) == "map" then
if shallow == true then
list.push([i, self[i]])
else
list.push([i, self[i].to_list])
end if
else
list.push([i, self[i]])
end if
end for
return list
end function
list.to_map = function()
l = self[0:]
map = {}
for i in indexes(l)
if typeof(l[i][1]) == "list" and l[i][1].len == 2 and typeof(l[i][1][0]) == "string" then
map[l[i][0]] = l[i][1].to_map
else
map[l[i][0]] = l[i][1]
end if
end for
return map
end function
list.each = function(func)
result = self[0:] //list copy.
for i in indexes(self)
func(result[i])
end for
end function
map.each = function(func)
list = self.to_list(true)
result = list[0:] //list copy.
for i in indexes(list)
func(result[i][0], result[i][1])
end for
end function
list.map = function(func)
result = self[0:] //list copy.
for i in indexes(self)
result[i] = func(result[i])
end for
return result
end function
list.reject = function(func)
result = self[0:] //list copy.
i = 0
while i < result.len
if func(result[i]) == true then
result.remove(i)
continue
end if
i = i + 1
end while
return result
end function
list.select = function(func)
f = function(o)
return not func(o)
end function
return self.reject(@f)
end function
// do not rename funcc to to func it will infinite loop
list.lsort = function(funcc)
f = function(i)
return {"sort_key": funcc(i), "obj": i}
end function
result = self.map(@f)
result = result.sort("sort_key")
f = function(i)
return i["obj"]
end function
return result.map(@f)
end function
list.flat = function()
result = []
for i in self
if typeof(i) == "list" then
result = result + i.flat
else
result.push(i)
end if
end for
return result
end function
list.compact = function()
r = []
for i in self
if i != null and i != "" then r.push(i)
end for
return r
end function
list.uniq = function()
result = []
for i in self
if result.indexOf(i) == null then result.push(i)
end for
return result
end function
list.has_any = function(value)
for i in self
if i == value then return true
end for
return false
end function
list.min = function()
min = self[0]
for item in self
if item < min then
min = item
end if
end for
return min
end function
list.max = function()
max = self[0]
for item in self
if item > max then
max = item
end if
end for
return max
end function