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
tests.src
listLib.src
to_list = function(map)
Lst = {}
Lst.
to_list = function(map)
list = []
for i in map.indexes
if typeof(map[i]) == "map" then
list.push([i, to_list(map[i])])
list.push([i,
Lst.
to_list(map[i])])
else
list.push([i, map[i]])
end if
end for
return list
end function
to_map = function(list)
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])
else
map[l[i][0]] = l[i][1]
end if
end for
return map
end function
each = function(obj, func)
Lst.
each = function(obj, func)
if typeof(obj) == "map" then
list = to_list(obj)
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
map = function(list, func)
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
reject = function(list, func)
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
select = function(list, func)
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
//TODOOOOOO
end function
tests.src
import_code("/home/
gue
st/listLib
/
list
Lib.src
")
import_code("/home/
me/l
st/listLib
.src")
print(Lst)
//to
list
print("to_list
")
//to list
a = {"a": 1, "b": 2, "c": {"a": 1}, "d": []}
print(to_list(a))
print(
Lst.
to_list(a))
//to map
print("to_map")
a = [["a", 1], ["b", [1,2]], ["c", [["d", []]]]]
print(to_map(a))
print(
Lst.
to_map(a))
//each
print("each")
a = {"a": 1, "b": 2}
b = [1,2,3
,4,5
]
b = [1,2,3]
print_map = function(k,v)
print(k
)
print(
v)
print(k
+ ":" +
v)
end function
each(a, @print_map)
Lst.
each(a, @print_map)
print_list = function(i)
print(i)
end function
each(b, @print_list)
Lst.
each(b, @print_list)
//map
print("map")
double = function(n)
return n * 2
end function
print(map([1,2,3,4], @double))
print(
Lst.
map([1,2,3,4], @double))
//reject
print("reject")
more_5 = function(number)
return number > 5
end function
print(reject([1,2,3,4,5,6,7,8], @more_5))
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(select([1,2,3,4,5,6,7,8], @more_5))
print(
Lst.
select([1,2,3,4,5,6,7,8], @more_5))