annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1353
8d95711f6615 replace java() with require "java"
Franklin Schmidt <fschmidt@gmail.com>
parents: 1267
diff changeset
1 require "java"
321
7f7708e8fdd4 remove import statement
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents: 320
diff changeset
2 local TableLuan = require "java:luan.modules.TableLuan"
297
899253043270 remove PackageLuan.load_lib()
fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
parents:
diff changeset
3
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 796
diff changeset
4 local Table = {}
503
92c3d22745b8 make _ENV optional
Franklin Schmidt <fschmidt@gmail.com>
parents: 497
diff changeset
5
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 796
diff changeset
6 Table.clear = TableLuan.clear
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 796
diff changeset
7 Table.concat = TableLuan.concat
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 796
diff changeset
8 Table.copy = TableLuan.copy
1095
2443152dc2f1 add Luan.hash_code() and Table.hash_value()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1088
diff changeset
9 Table.hash_value = TableLuan.hash_value
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 796
diff changeset
10 Table.insert = TableLuan.insert
1100
ad6b3b9fef40 add Table.is_empty() and Table.size()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1095
diff changeset
11 Table.is_empty = TableLuan.is_empty
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 796
diff changeset
12 Table.pack = TableLuan.pack
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 796
diff changeset
13 Table.remove = TableLuan.remove
1100
ad6b3b9fef40 add Table.is_empty() and Table.size()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1095
diff changeset
14 Table.size = TableLuan.size
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 796
diff changeset
15 Table.sort = TableLuan.sort
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 796
diff changeset
16 Table.unpack = TableLuan.unpack
497
55f9f74f1e55 Http.request is now pure luan
Franklin Schmidt <fschmidt@gmail.com>
parents: 407
diff changeset
17
1261
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
18
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
19 local Luan = require "luan:Luan.luan"
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
20 local error = Luan.error
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
21 local type = Luan.type or error()
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
22 local pairs = Luan.pairs or error()
1267
9fa8b8389578 add LuanTable.luan;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1262
diff changeset
23 local toTable = TableLuan.toTable or error()
1464
465b4a0dae4a empty list vs map
Franklin Schmidt <fschmidt@gmail.com>
parents: 1353
diff changeset
24 local copy = Table.copy or error()
1261
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
25
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
26 function Table.java_to_table_shallow(obj)
1262
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
27 local rtn = toTable(obj)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
28 if rtn ~= nil then
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
29 return rtn
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
30 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
31 local tp = type(obj)
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
32 if tp == "java" then
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
33 tp = obj.getClass().getName()
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
34 end
Franklin Schmidt <fschmidt@gmail.com>
parents: 1261
diff changeset
35 error("can't convert type "..tp.." to table")
1261
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
36 end
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
37
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
38 local to_luan, deepen
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
39
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
40 function to_luan(obj,java_to_table_shallow)
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
41 return type(obj)=="java" and deepen(java_to_table_shallow(obj),java_to_table_shallow) or obj
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
42 end
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
43
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
44 function deepen(tbl,java_to_table_shallow)
1464
465b4a0dae4a empty list vs map
Franklin Schmidt <fschmidt@gmail.com>
parents: 1353
diff changeset
45 for key, value in pairs(copy(tbl)) do
1261
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
46 key = to_luan(key,java_to_table_shallow)
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
47 value = to_luan(value,java_to_table_shallow)
1464
465b4a0dae4a empty list vs map
Franklin Schmidt <fschmidt@gmail.com>
parents: 1353
diff changeset
48 tbl[key] = value
1261
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
49 end
1464
465b4a0dae4a empty list vs map
Franklin Schmidt <fschmidt@gmail.com>
parents: 1353
diff changeset
50 return tbl
1261
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
51 end
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
52
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
53 function Table.java_to_table_deep(obj,java_to_table_shallow)
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
54 java_to_table_shallow = java_to_table_shallow or Table.java_to_table_shallow
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
55 return deepen(java_to_table_shallow(obj),java_to_table_shallow)
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
56 end
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
57
198d6af7330a rename Luan.to_table to Table.java_to_table_shallow and Luan.to_luan to Table.java_to_table_deep
Franklin Schmidt <fschmidt@gmail.com>
parents: 1260
diff changeset
58
1088
bae2d0c2576c change module naming convention
Franklin Schmidt <fschmidt@gmail.com>
parents: 796
diff changeset
59 return Table