Files

tests.src
  • import_code("/home/me/lst/listLib.src")
  • print(Lst)
  • //to list
  • print("to_list")
  • a = {"a": 1, "b": 2, "c": {"a": 1}, "d": []}
  • print(Lst.to_list(a))
  • //to map
  • print("to_map")
  • a = [["a", 1], ["b", [1,2]], ["c", [["d", []]]]]
  • print(Lst.to_map(a))
  • //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)
  • print_list = function(i)
  • print(i)
  • end function
  • Lst.each(b, @print_list)
  • //map
  • print("map")
  • double = function(n)
  • return n * 2
  • end function
  • print(Lst.map([1,2,3,4], @double))
  • //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))
  • // 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))
  • //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
  • print(Lst.lsort(coins, @f))
listLib.src
  • Lst = {}
  • Lst.to_list = function(map)
  • list = []
  • for i in map.indexes
  • if typeof(map[i]) == "map" then
  • list.push([i, Lst.to_list(map[i])])
  • 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]] = to_map(l[i][1])
  • 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)
  • 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
  • Lst.map_sort = function(map, func)
  • map_copy = Lst.to_map(Lst.to_list(map))
  • result
  • // 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")
  • //TODOOOOOO
  • end function
  • f = function(i)
  • return i["obj"]
  • end function
  • return Lst.map(result, @f)
  • end function