Files
listLib.src
- Lst = {}
Lst.to_list = function(map)
-
- Lst.to_list = function(map, shallow = false)
- list = []
-
- for i in map.indexes
- if typeof(map[i]) == "map" then
list.push([i, Lst.to_list(map[i])])
- if shallow == true then
- list.push([i, map[i]])
- else
- list.push([i, Lst.to_list(map[i])])
- end if
- else
- list.push([i, map[i]])
- end if
- end for
-
- return list
- end function
-
- Lst.to_map = function(list)
- l = list[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]] = Lst.to_map(l[i][1])
- else
- map[l[i][0]] = l[i][1]
- end if
- end for
-
- return map
- end function
-
- Lst.each = function(obj, func)
- if typeof(obj) == "map" then
list = Lst.to_list(obj)
- list = Lst.to_list(obj, true)
- else
- list = obj
- end if
- result = list[0:] //list copy.
- for i in indexes(list)
- if typeof(obj) == "map" then
- func(result[i][0], result[i][1])
- else
- func(result[i])
- end if
- end for
- end function
-
- Lst.map = function(list, func)
- result = list[0:] //list copy.
- for i in indexes(list)
- result[i] = func(result[i])
- end for
- return result
- end function
-
- Lst.reject = function(list, func)
- result = list[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
-
- Lst.select = function(list, func)
- result = list[0:] //list copy.
-
- i = 0
- while i < result.len
- if func(result[i]) == false then
- result.remove(i)
- continue
- end if
- i = i + 1
- end while
-
- return result
- end function
-
- // do not rename funcc to to func it will infinite loop
- Lst.lsort = function(list, funcc)
- f = function(i)
- return Lst.to_map([["sort_key", funcc(i)], ["obj", i]])
- end function
- result = Lst.map(list, @f)
- result = result.sort("sort_key")
-
- f = function(i)
- return i["obj"]
- end function
- return Lst.map(result, @f)
- end function
-
- Lst.flat = function(list)
- result = []
- for i in list
- if typeof(i) == "list" then
- result = result + Lst.flat(i)
- else
- result.push(i)
- end if
- end for
-
- return result
- end function
-
- Lst.uniq = function(list)
- result = []
- for i in list
- if result.indexOf(i) == null then result.push(i)
- end for
- return result
- end function
-
-
- Lst.has_any = function(list, value)
- for i in list
- if i == value then return true
- end for
- return false
- end function