comparison src/luan/modules/Table.luan @ 1464:465b4a0dae4a

empty list vs map
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 03 Apr 2020 10:04:52 -0600
parents 8d95711f6615
children fa066aaa068c
comparison
equal deleted inserted replaced
1463:fb003c4003dd 1464:465b4a0dae4a
19 local Luan = require "luan:Luan.luan" 19 local Luan = require "luan:Luan.luan"
20 local error = Luan.error 20 local error = Luan.error
21 local type = Luan.type or error() 21 local type = Luan.type or error()
22 local pairs = Luan.pairs or error() 22 local pairs = Luan.pairs or error()
23 local toTable = TableLuan.toTable or error() 23 local toTable = TableLuan.toTable or error()
24 local copy = Table.copy or error()
24 25
25 function Table.java_to_table_shallow(obj) 26 function Table.java_to_table_shallow(obj)
26 local rtn = toTable(obj) 27 local rtn = toTable(obj)
27 if rtn ~= nil then 28 if rtn ~= nil then
28 return rtn 29 return rtn
39 function to_luan(obj,java_to_table_shallow) 40 function to_luan(obj,java_to_table_shallow)
40 return type(obj)=="java" and deepen(java_to_table_shallow(obj),java_to_table_shallow) or obj 41 return type(obj)=="java" and deepen(java_to_table_shallow(obj),java_to_table_shallow) or obj
41 end 42 end
42 43
43 function deepen(tbl,java_to_table_shallow) 44 function deepen(tbl,java_to_table_shallow)
44 local rtn = {} 45 for key, value in pairs(copy(tbl)) do
45 for key, value in pairs(tbl) do
46 key = to_luan(key,java_to_table_shallow) 46 key = to_luan(key,java_to_table_shallow)
47 value = to_luan(value,java_to_table_shallow) 47 value = to_luan(value,java_to_table_shallow)
48 rtn[key] = value 48 tbl[key] = value
49 end 49 end
50 return rtn 50 return tbl
51 end 51 end
52 52
53 function Table.java_to_table_deep(obj,java_to_table_shallow) 53 function Table.java_to_table_deep(obj,java_to_table_shallow)
54 java_to_table_shallow = java_to_table_shallow or Table.java_to_table_shallow 54 java_to_table_shallow = java_to_table_shallow or Table.java_to_table_shallow
55 return deepen(java_to_table_shallow(obj),java_to_table_shallow) 55 return deepen(java_to_table_shallow(obj),java_to_table_shallow)