Files

tests.src
  • import_code("/home/me/lst/listLib.src")
  • print(Lst)
  • import_code("/home/me/lst.src")
  • //to list
  • print("to_list")
  • print "to_list"
  • a = {"a": 1, "b": 2, "c": {"a": 1}, "d": []}
  • print(Lst.to_list(a))
  • print a.to_list
  • //to map
  • print("to_map")
  • print "to_map"
  • a = [["a", 1], ["b", [1,2]], ["c", [["d", []]]]]
  • print(Lst.to_map(a))
  • print a.to_map
  • //each
  • print("each")
  • print "each"
  • a = {"a": 1, "b": 2}
  • b = [1,2,3]
  • print_map = function(k,v)
  • print(k + ":" + v)
  • end function
  • Lst.each(a, @print_map)
  • a.each(@print_map)
  • print_list = function(i)
  • print(i)
  • end function
  • Lst.each(b, @print_list)
  • b.each(@print_list)
  • //map
  • print("map")
  • print "map"
  • double = function(n)
  • return n * 2
  • end function
  • print(Lst.map([1,2,3,4], @double))
  • print [1,2,3,4].map(@double)
  • //reject
  • print("reject")
  • print "reject"
  • more_5 = function(number)
  • return number > 5
  • end function
  • print(Lst.reject([1,2,3,4,5,6,7,8], @more_5))
  • print [1,2,3,4,5,6,7,8].reject(@more_5)
  • // select
  • print("select")
  • print "select"
  • more_5 = function(number)
  • return number > 5
  • end function
  • print(Lst.select([1,2,3,4,5,6,7,8], @more_5))
  • print [1,2,3,4,5,6,7,8].select(@more_5)
  • //sort
  • print("sort")
  • print "sort"
  • Coin = {}
  • Coin.discount = function()
  • return self.value - 1
  • end function
  • coins = []
  • for i in [5, 10, 4, 2, 11]
  • coin = new Coin
  • coin.value = i
  • coin.name = "coin" + i
  • coins.push(coin)
  • end for
  • f = function(coin)
  • return coin.discount
  • end function
  • sorted_coins = coins.lsort(@f)
  • for c in sorted_coins
  • print c.value
  • end for
  • //flat
  • print "flat"
  • a = [[[2], [[[[3]]], [4]]]]
  • print a.flat
  • //uniq
  • print "uniq"
  • a = [1, 1, 2, 3, 4, 4, 5]
  • print a.uniq
  • //has_any
  • print "has_any"
  • a = [1,2,3,4]
  • print a.has_any(2)
  • print a.has_any(5)
  • //min
  • print "min"
  • a = [1,2,3]
  • print(Lst.lsort(coins, @f))
  • print a.min
  • //max
  • print "max"
  • a = [1,2,3]
  • print a.max
listLib.src
  • Lst = {}
  • Lst.to_list = function(map, shallow = false)
  • map.to_list = function(shallow = false)
  • list = []
  • for i in map.indexes
  • if typeof(map[i]) == "map" then
  • for i in self.indexes
  • if typeof(self[i]) == "map" then
  • if shallow == true then
  • list.push([i, map[i]])
  • list.push([i, self[i]])
  • else
  • list.push([i, Lst.to_list(map[i])])
  • list.push([i, self[i].to_list])
  • end if
  • else
  • list.push([i, map[i]])
  • list.push([i, self[i]])
  • end if
  • end for
  • return list
  • end function
  • Lst.to_map = function(list)
  • l = list[0:]
  • 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]] = Lst.to_map(l[i][1])
  • 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
  • Lst.each = function(obj, func)
  • if typeof(obj) == "map" then
  • list = Lst.to_list(obj, true)
  • else
  • list = obj
  • end if
  • 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)
  • if typeof(obj) == "map" then
  • func(result[i][0], result[i][1])
  • else
  • func(result[i])
  • end if
  • func(result[i][0], result[i][1])
  • end for
  • end function
  • Lst.map = function(list, func)
  • result = list[0:] //list copy.
  • for i in indexes(list)
  • 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
  • Lst.reject = function(list, func)
  • result = list[0:] //list copy.
  • 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
  • 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
  • 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
  • Lst.lsort = function(list, funcc)
  • list.lsort = function(funcc)
  • f = function(i)
  • return Lst.to_map([["sort_key", funcc(i)], ["obj", i]])
  • return {"sort_key": funcc(i), "obj": i}
  • end function
  • result = Lst.map(list, @f)
  • result = self.map(@f)
  • result = result.sort("sort_key")
  • f = function(i)
  • return i["obj"]
  • end function
  • return Lst.map(result, @f)
  • return result.map(@f)
  • end function
  • Lst.flat = function(list)
  • list.flat = function()
  • result = []
  • for i in list
  • for i in self
  • if typeof(i) == "list" then
  • result = result + Lst.flat(i)
  • result = result + i.flat
  • else
  • result.push(i)
  • end if
  • end for
  • return result
  • end function
  • Lst.uniq = function(list)
  • list.uniq = function()
  • result = []
  • for i in list
  • for i in self
  • 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
  • list.has_any = function(value)
  • for i in self
  • if i == value then return true
  • end for
  • return false
  • end function
  • Lst.min = function(list)
  • min = list[0]
  • for item in list
  • list.min = function()
  • min = self[0]
  • for item in self
  • if item < min then
  • min = item
  • end if
  • end for
  • return min
  • end function
  • Lst.max = function(list)
  • max = list[0]
  • for item in list
  • if item < max then
  • list.max = function()
  • max = self[0]
  • for item in self
  • if item > max then
  • max = item
  • end if
  • end for
  • return max
  • end function